This Python script automates the generation of documents (in both .docx and .pdf formats) from a CSV file containing data. It uses the docxtpl library to fill in a template document with data from the CSV file, and then converts the filled-in document into a PDF format using docx2pdf.
- Python 3.x
pandaslibrary (pip install pandas)docxtpllibrary (pip install docxtpl)docx2pdflibrary (pip install docx2pdf)- Windows OS
-
Prepare your data: Ensure your data is in a CSV format (
data.csv) and contains the necessary fields. -
Prepare the template document: Create a Word document (
certificate.docx) with placeholders that will be replaced by the data from the CSV file. Placeholders should be in double curly braces, e.g.,{{ name }}. -
Run the script: Execute the Python script
generate_documents.py. The script will read data fromdata.csv, fill in the template documentcertificate.docx, convert it into both.docxand.pdfformats, and save them in the current directory.
- The CSV file should contain the necessary fields required to populate the template document.
- Ensure that column names in the CSV file match the placeholders in the template document.
- Customize the CSV file according to your data needs.
- This script assumes that each row in the CSV file corresponds to a single document to be generated.
- The script will create a separate document for each row in the CSV file, numbered sequentially.