This article provides a step-by-step guide on how to automate the process of filling in Word templates using Python, Openpyxl, and Docx packages, based on Excel input.
Abstract
The article titled "Automate Filling Templates With Python" aims to help users perform important tasks instead of repetitive ones. It introduces a method to fill in a predefined Word template based on Excel input and automate the process to create multiple templates simultaneously. The article covers the introduction to the topic, loading necessary packages, getting the data, creating the Word template and Excel file, loading files in Python, and writing a loop in Python to fill in the templates. The guide is part of a three-part series on building your own application in Python.
Bullet points
The article teaches users how to fill in a Word template based on Excel input and automate the process.
The guide uses Openpyxl and Docx Python packages for working with Excel files and Word documents.
Users need to save the provided Excel and Word templates to their local folder.
The Word template defines input parameters in the format {{PARAMETER}}, without spaces.
The Excel file contains the same parameters as defined in the Word file, with data for each specific parameter in separate columns.
The article provides code snippets for loading files in Python, defining a dictionary for parameter names, and writing a loop to fill in the Word templates.
The full code is available in a Google Colab file.
The article is part of a three-part series on building your own application in Python.
The word template defines the input parameters in the following format:
{{PARAMETER}}
It is crucial there are no spaces in your parameters:
{{Company_Name}} is OK while {{Company Name}} will give errors
Word template
Create the Excel File
In the first column, the same parameters are shown as defined in your word file. These should have exactly the same naming.
In the other columns, you define the input data for that specific parameter. Each column will ensure that an additional template will be filled in.
In our example, two reports will be generated (B and C):
Column B defines the data for Meta Platforms
Column C defines the data for 123company
Excel
Load the Files in Python
First, you load the files in Python as shown in the code snippet below.
Then, you define the dictionary, to_fill_in, where you define the parameter names.
Step-by-step code explanation:
r “C:\Users\Documents\1_Work\Scripts\TemplateAssistant\004-Test”
Change this to the folder location of where you have saved the downloaded word document and excel file.
load_workbook(workbook_path):
Load in the excel workbook using the workbook_path defined earlier on.
DocxTemplate(template_path)
Create a docx template for the word document downloaded earlier.
workbook[“Input”]
You specify the sheet_name of the excel sheet necessary.
to_fill_in
This is an empty dictionary with the keys matching the first column of the excel file. For now, you assign empty values (None) and these will be filled in using the excel file in the next step.
Write a Loop in Python
You now want to use Python to loop over all the parameters and their respective data.
Subsequently, you use Python to fill in the word templates and save them using the company name attribute. Here’s the code:
Step-by-step code explanation:
column = 2
This is the column that will be read in the first iteration. Since the first column defines the input parameters, the second column is the column that contains actual data.
while column ≤ worksheet.max_column
While the current column is not the maximum column (or last column) we should keep iterating/creating templates that are filled in.
col_index = chr(column+64)
Convert the column numbers to character (64 + 2 = B)
for key in to_fill_in: …
For each parameter defined in the dictionary: Assign the value of the excel sheet for the current col_index and row_index.
template.render(to_fill_in)
Use the render function to fill in the word template based on the dictionary created.
template.save(filled_path)
You save the created template on the specified path with the name containing the parameter “Company_Name.”
Congratulations
You have now coded your first template filler program. You can experiment with different templates and add more parameters.