Create a Data Entry Form for MS Word in 10 Minutes using Python | PySimpleGUI & docxtpl Tutorial
Introduction
In this post, I will guide you through building a data entry form from scratch that generates a Word document using Python. This form will allow users to input data such as client and vendor names and calculate fields like today’s date plus one week and a non-refundable fee of 20% of the total amount. Once the user fills out the form and presses the ‘Create Contract’ button, a new Word document will be generated with the placeholders replaced and the calculations done.
Simple Example docxtpl
To start, I will create a simple Word document named ‘my word template’. In this template, I will write: “Hello, my name is” followed by a placeholder in curly brackets. This placeholder will be manipulated using Python with the help of the docxtpl library.
Install this library by running pip install docxtpl
in your command prompt or terminal. Once installed, I will create a blank Python file and import the necessary dependencies. This includes importing Path from pathlib and DocxTemplate from docxtpl.
Next, I will specify the path to the Word document and create a dictionary that maps the placeholder “NAME” to a value, say “Sven”. After rendering the document, executing the script will produce a new document with the placeholder replaced.
Real Life Example docxtpl
Now, let’s take this a step further by adding some calculations to our script. I will modify the Word document to include fields for the client and vendor names, an amount, and then calculate the non-refundable fee (20% of the amount) and today’s date plus one week. The user will only need to input the amount.
I will implement this by creating variables for the placeholders and using the DateTime module to calculate the date. The dictionary will be updated accordingly to include the calculated values.
Simple Example PySimpleGUI
Next, I will introduce the graphical user interface (GUI) using PySimpleGUI. This library simplifies the process of creating GUIs. Install it by running pip install PySimpleGUI
.
To create a simple GUI, I will set up a layout with two input fields and two buttons. The layout is structured as a list of lists, where each inner list represents a row in the GUI.
After initializing the window, I will set up an event loop to listen for user interactions. When the user clicks the ‘Read’ button, the values from the input fields will be printed to the console.
Building the Final Application
Now, I will combine the GUI with the docxtpl module to create the final application. I will modify the layout to include fields for the client name and other required inputs, ensuring the keys match those in the Word template.
After setting up the GUI, I will import the necessary modules and initialize the DocxTemplate object. The calculated fields will be added to the dictionary, and the document will be rendered and saved in the current directory.
Converting py to exe
Finally, to share this application with others who may not have Python installed, I will convert the Python script into a standalone executable file using psgcompiler. Install it with pip install psgcompiler
. This tool provides a straightforward graphical interface to convert Python scripts into binary files.
After selecting the Python script to convert, I will leave the default settings and initiate the conversion. Once completed, the executable file will appear next to the original script.
Conclusion
In this tutorial, I demonstrated how to create a data entry form for MS Word using Python, docxtpl, and PySimpleGUI. We covered how to gather user input, perform calculations, and generate a Word document with placeholder substitutions. Additionally, we converted the script into an executable file for easy distribution. If you have any questions, feel free to ask in the comments!