Python Generate Word Document from Template

Are you looking for a way to create Word documents from templates? You’ve come to the right place! In this blog post, we’ll explore the best solution for generating Word documents from templates using different types of data sources in Python. Learn how to leverage predefined templates to generate Word documents in DOCX format by populating placeholders using various data sources, such as XML, JSON, or CSV.

This article covers the following topics:

  1. Python Library to Generate Word Documents from Templates
  2. Create Word Documents from Template using XML Data
  3. Generate Word Documents from Template using JSON Data
  4. Generate Word Documents with CSV Data in Python
  5. Python LINQ Reporting Engine
  6. Free Resources

Python Library to Generate Word Documents from Templates

We will use the LINQ Reporting Engine of Aspose.Words for the Python API for generating Word documents from templates. This engine supports different tags for text, images, lists, tables, hyperlinks, and bookmarks. We populate Word documents that feature these tags and fill them with data from XML, JSON, or CSV data sources. So let’s begin generating the Word documents from the templates using Python.

Installing Aspose.Words for Python

You can download the package or install the API from PyPI using the following pip command in the console:

> pip install aspose-words

Create Word Documents from Template using XML Data in Python

For generating the Word document from a template using the XML data source, we need a template file and XML source data. We’ll create a Word document (DOC/DOCX) with the following placeholders as the content of the template document:

Name: <<[Name]>>, Age: <<[Age]>>, Date of Birth:
<<[Birth]:"dd.MM.yyyy">>

Children:
<<foreach [in Child]>><<[Child_Text]>>
<</foreach>>"""

Here is the XML data source that we’ll use in this example:

Now, we can easily generate a Word document from an XML data source by following the steps below:

  1. Load the Word template file using the Document class.
  2. Create an instance of the XmlDataSource class and initialize it with the XML file’s path.
  3. Instantiate ReportingEngine class.
  4. Call the ReportingEngine.BuildReport() method with the template, data source, and data source name.
  5. Save the generated Word document using the Document.Save() method.

The following code sample shows how to generate a Word document from an XML data source in Python.

Output

Generate Word Documents from Template using XML Data in Python

Generate Word Documents from Template using JSON Data in Python

For generating a Word document using a JSON data source, we’ll use the following Word template:

<<foreach [in managers]>>Manager: <<[Name]>>
Contracts:
<<foreach [in Contract]>>- <<[Client.Name]>> ($<<[Price]>>)
<</foreach>>
<</foreach>>

The following would be the JSON data source that we’ll use to populate the template:

We can easily generate the Word document from a JSON data source by following the steps mentioned earlier. However, we will use the JsonDataSource class for loading and using the JSON data source in step 2.

The following code sample shows how to generate a Word document from the template using JSON in Python.

Output

Generate Word Documents from Template using JSON Data in Python

Generate Word Documents with CSV Data in Python

For generating the Word document from CSV, we’ll use the following Word template:

<<foreach [in persons]>>Name: <<[Column1]>>, Age: <<[Column2]>>, Date of Birth: <<[Column3]:"dd.MM.yyyy">>
<</foreach>>
Average age: <<[persons.Average(p => p.Column2)]>>

The template will be populated with the following CSV data:

Now, we can generate the Word document from the CSV data source by following the steps mentioned earlier. However, we will use the CsvDataSource class for loading and using the CSV data source in step 2.

The following code sample shows how to generate a Word document from a CSV data source in Python.

Output

Generate Word Documents with CSV Data in Python

Get a Free License

You can get a free temporary license and generate Word documents without evaluation limitations.

Python LINQ Reporting Engine - Read More

The LINQ Reporting Engine supports a wide range of tags for generating fully-featured Word documents dynamically in Python. You can read more about these tags, along with their syntax, in this article.

Python Generate Word Document from Template – Free Resources

Besides generating Word documents from templates, learn more about creating, manipulating, and converting Word documents, and explore various other features of the library using the resources below:

Conclusion

In this article, we have learned how to generate Word documents from templates in Python. We have also seen how to use XML, JSON, and CSV data sources to generate Word documents. By leveraging Aspose.Words for Python LINQ Reporting Engine, you can easily generate various types of Word documents. In case of any ambiguity, please feel free to contact us on our free support forum.