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.2.tar.gz (9.1 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.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: docxcomtpl-0.1.2.tar.gz
  • Upload date:
  • Size: 9.1 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.2.tar.gz
Algorithm Hash digest
SHA256 505cb978be37def5a1ac8649b8c91fcc80684f60f086a9344878bf04d8388472
MD5 0a40304b36961fe65f2e5496e5e481d1
BLAKE2b-256 69efb4b5b3d498453ec712147b3801c11e6510bedc0720ea1f25251bf9b318a7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: docxcomtpl-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a5cb154fc9fc120078fba62b685f02202c36bf2d56cb423e17116d56f437bbf8
MD5 4f01ebf26ef870ab9c8ac0dea5f6c824
BLAKE2b-256 c29539bf0a9470677da2aacad964809a29fedc062f0b6b088eb9ad85f233bbef

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