Skip to main content

A Python library that adds COM automation support for docx-template library

Project description

docxcomtpl

docxcomtpl is a Python library for generating DOCX documents using templates. It is based upon the python-docx-template library, that generates DOCX documents using Jinja-like templating syntax. It generates documents using pure Python code, which provides good performance and reliability, but limits the possibilities.

The docxcomtpl library extends the functionality of the original library by adding support for COM automation.

This allows you to generate more complex documents using the full range of Microsoft Word features.


Features

  • Direct generation of DOCX documents with text and images — provides good performance and reliability.
  • Jinja-like Placeholder Syntax — easy-to-use templating for text and images.
  • COM Automation Support — integrate COM calls to extend document generation capabilities, at the cost of performance.
  • MIT Licensed — free and open-source with a permissive license.

Template Syntax

See https://docxtpl.readthedocs.io/en/latest/#jinja2-like-syntax for the detailed documentation of the template syntax.

DOCX generation with COM Automation

To generate a document, create an instance of DocxComTemplate class, passing the path to the template DOCX file. Then call the generate method with the data dictionary and the output file path.

To insert data using COM, put inserter fucntion instead of the value in the data. Inserter functions take single argument which is Word.Range object, and insert desired data into it.

Module docxcomtpl.com_utilities provides some useful inserters.

from docxcomtpl import DocxComTemplate
from docxcomtpl.com_utilities import table_inserter

# Load the template
template = DocxComTemplate("template.docx")

# Define the data for the template
data = {
    "header": "My Document",
    "table": table_inserter([["Col1", "Col2"], ["Row1", "Row2"]])
}
# Render the template with data
template.generate(data, "output.docx")

The template and the resulting document are shown below:

COM Template and result

Complete list of inserter functions, available in docxcomtpl.com_utilities module:

Function Description
`table_inserter(data:List[List[str]]) Inserts a table with the given data. Each sublist represents a row.
image_inserter(picture_path:str) Inserts an image from the specified path using COM. Supports more formats than the DOCX mode
document_inserter(document_path:str) Inserts content of another document, can be DOCX, RTF or anything supported by Word.
anchor_inserter(text:str, anchor:str) Inserts an anchor with the given text and name.
heading_inserter(text:str, level:int=1) Inserts a heading with the given text and level. Level 1 is the highest level.

COM Post-Processing

WHen generating documents using COM-assisted mode (DocxComTemplate), you can use post-processing to modify the document after it has been generated. To do this, specify the postprocess argument when calling the DocxComTemplate.generate method. Library docxcomtpl.com_utilities provides an example post-processing function that updates document creation date and table of content:

from docxcomtpl.docxcom_template import DocxComTemplate
from docxcomtpl.com_utilities import update_document_toc

template = DocxComTemplate("template.docx")
data = ...
template.generate(
    data,
    "output.docx",
    postprocess=update_document_toc
)

Post-processing function must take single argument which is the Word.Document object.

Requirements

  • Python 3.7 or higher
  • pywin32 package for COM automation
  • Microsoft Word installed (optional, for DOCX + COM mode)

Installation

You can install docxcomtpl using pip:

pip install docxcomtpl

License

This project is licensed under the MIT License - see the LICENSE.MIT file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

docxcomtpl-0.1.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

docxcomtpl-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file docxcomtpl-0.1.1.tar.gz.

File metadata

  • Download URL: docxcomtpl-0.1.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for docxcomtpl-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8eb0c78432a980ebe5c77dfcbf3e87e55c38fef7d25c8126860713cb42f86ccc
MD5 2611f92fa6bf1315bc79b261c0e11a5d
BLAKE2b-256 e39fb8c09f431f2dd9b1e1494622c661abf7a09f5d264dee280f1bf0c7be632a

See more details on using hashes here.

File details

Details for the file docxcomtpl-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: docxcomtpl-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for docxcomtpl-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4dfd52f68b32a1b1662046a4d48645c2e12cd8b36f74595ae1b72fd2a70dc1e6
MD5 2997e518489685a224f050ba420cfe7d
BLAKE2b-256 c5411fe91967bdad2d57aee87ef9479d26a4ea06138a0bc5f02e88570a95158d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page