RPA for Python RPA Scripts
How to run these scripts
Assuming you have Python & RPA for Python installed you can execute the scripts by pasting the code into a .py
file and running it from a terminal with the command:
py scriptname.py
Sample RPA Test Page
The web scripts reference a custom RPA Test Page
This script runs in headless mode (no browser is launched or harmed during the execution of this script).
The script workflow runs as follows:
-
A web page is accessed
-
A table on the page is extracted to CSV
-
The CSV is converted to HTML
-
The HTML Table is written to an HTML template
-
The template is converted into a PDF with Puppeteer
Script 1: Convert Web Page CSV Data to HTML & then PDF
import rpa as r
# Pandas is a Python library
# that can carry out various file conversion/data analysis functions
import pandas
# use init() to start TagUI, it auto downloads TagUI on first run
r.init(headless_mode = True)
# r.init((headless_mode = True)(Turbo_mode = True))
# go to url
r.url('https://verdapress.uk/services/test-bed/')
# get web page table data as csv - always useful
r.table(1, 'python_table.csv')
# launches Excel as the default csv editor
# r.run("C:\\tagui\\vpPython\\workbench\\python_table.csv")
# convert csv to html with pandas
# https://www.codespeedy.com/converting-csv-to-html-table-in-python/
# ensure you have pip install pandas
r.wait (2)
file = pandas.read_csv("python_table.csv")
file.to_html("python_table.html")
# load template code into index.html
r.write(r.load('template.html'), 'index.html')
# load table into index.html
r.write(r.load('python_table.html'), 'index.html')
r.write('<hr />' , 'index.html')
# close html tags and write page
r.write('</body></html>', 'index.html')
# convert index.html to pdf with Puppeteer
# run node js file from python
r.run("py run_node_puppeteer.py")
# close all
r.close()
Note
The other 2 scripts on this page are called from script1.py
Script 2: Run Node js from RPA from Python
from subprocess import check_output
p = check_output(['node', './pdfpage.js'])
print (p)
Script 3: Puppeteer File to PDF
// Use the File system & convert file to PDF
const puppeteer = require('puppeteer');
// Node.js file system module fs
// fs allows you to work with the file system on your computer.
// To include the File System module, use the require() method:
const fs = require('fs');
// https://www.w3schools.com/nodejs/nodejs_filesystem.asp
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// set your html as the pages content from file system
const html = fs.readFileSync(`${__dirname}/index.html`, 'utf8')
await page.setContent(html, {
waitUntil: 'domcontentloaded'
})
await page.pdf({ path: 'vpFSTable.pdf', format: 'a4', landscape: true, });
await browser.close();
})();
Video: Full Script
Hint
Download project scripts
RPA for Python Reference
If you require support or help check out our services page.