Link to file with images: https://docs.google.com/document/d/1lPALVh4FBZro047m8NvhSo1KGV6kDcthynq_Fh3-xmU/edit?usp=sharing
Introduction:
The following project was created to assist others in creating printable certificates. It contains a single script that reads the contents of a csv file, replaces a few variables in an html template and creates a copy of that html file with a new name. Then the script converts these html files into pdfs using a tool called WKHTMLtoPDF. The pdf conversion is optional and can be commented out at the end of the script if needed.
Setup Python environment:
- see online instructions for Python installation, be safe use a Windows sandbox and or Python virtual environment.
HTML to PDF convertor:
HTML to PDF conversion is optional but requires a playwrite headless chrome to be installed. If not already installed, script will install on first run.
Pseudo Code.
| Take excel spreadsheet information and produce a series of pdfs certificates.�- Read in excel data through exported csv file.�- Read in each column and each row.�- Read in HTML template with variables.�- Replace variables with csv file data.�- Save the HTML file as new.�- Convert HTML to PDF.�- Repeat until done. |
|---|
Data Preparation in XLSX for CSV
Header Rows
2 columns are needed: "Presenters", "Presentation Title" as header rows.
Presenters Examples
Should be separated by a comma if more than 1. If two or more separate by “and”
One presenter: Format: Edwin Locke Two presenters: Format: Edwin Locke and Sophia Bell
Many presenters: Edwin Locke, Sophia Bell, Miles Hawthorne, Clara Penrose and Isaac Tilling
Presentation Titles
Should be made free of special characters. An effort was made to substitute this in the script as the clean_file_title(title) function. This will crash the script. Non utf-8 characters will cause issues.
Modules used and reasoning for using them.
csv. Read in csv.
os. Makes the directory “certs” in project folder, creates directory for html files.
shutil. Does what the os module doesn’t do, removes certs folder upon a re-run.
re. Pattern match/replace in html file.
subprocess. Used to run WKHTMLtoPDF.
Make folders:
“certificates" folder, inside certificates folder make "dev" folder. Place "Certificate_01.html" in dev folder.
Setup certificates folder:
Replace “names_and_presentations.csv” with new names and presentations
Creating Certificates:
Navigate to the folder in a windows terminal,In a terminal type: “Python Certificates.py” without quotes
Result:
- Folder called “certs” is created in “Certificates” folder
- The screen will output a list of html files being saved in “certs” folder
- HTML files are converted in place in the “certs” folder.
If you want to skip the PDF conversion comment out the code with “#” You can then print out individual HTML files. In the print options choose the following options to print the background graphics. Print via a web browser with the following options: