Skip to main content

docxtpl-ng

PyPI Downloads

Use a .docx file as a Jinja2 template.

Design the document in Word — styles, tables, headers, page layout, the lot — write {{ customer.name }} where the data goes, and render it from Python.

from docxtpl import Template

tpl = Template("invoice.docx")
tpl.render({"customer": customer, "lines": lines, "total": total})
tpl.save("invoice-2026-0042.docx")

Status

Alpha. The whole feature set is implemented and tested — 269 tests, with rendered documents validated against the ISO/IEC 29500 schemas and compared against what docxtpl itself produces — but it has not been run over a large corpus of real templates or used in anger. The 0. major says the same thing: expect to find things, and expect the odd rough edge to be filed off in a way that changes behaviour. Follow the milestones for progress.

Installing

pip install docxtpl-ng

or, with uv:

uv add docxtpl-ng

Note: the importable package is docxtpl, not docxtpl_ng — use import docxtpl. docxtpl-ng and docxtpl therefore cannot be installed side by side.

Python 3.9 through 3.14. The only runtime dependencies are python-docx-ng for the document model and Jinja2 for the templating.

Two APIs

docxtpl-ng exports two public surfaces over one engine.

The modern API — Template, Text, Fragment, Image, Preformatted — is typed throughout and keyword-only where an argument is easy to misplace. Use it for new code.

The compatibility API — DocxTemplate, RichText, R, Subdoc, InlineImage, Listing — matches the names and signatures published by the docxtpl distribution. The package this distribution installs is called docxtpl as well, so an existing project migrates by swapping the dependency and changing nothing else:

-docxtpl>=0.16
+docxtpl-ng>=0.1.0
from docxtpl import DocxTemplate, RichText, InlineImage  # unchanged

The two cannot be installed side by side — they claim the same import name — so remove docxtpl first.

Your .docx templates need no changes at all: the tag dialect — {{ var }}, {%p %}, {%tr %}, {%tc %}, {%r %}, {% colspan %}, {% cellbg %}, {% hm %}, {% vm %} — is reproduced exactly. The compatibility surface is supported indefinitely and is not deprecated.

Relationship to docxtpl

docxtpl (repository python-docx-template) is the project that established this idea, and it deserves the credit for it. docxtpl-ng is an independent implementation, not a fork: it shares no code with it, and it is MIT licensed rather than LGPL-2.1.

It exists because docxtpl depends on the python-docx distribution, which cannot be installed alongside python-docx-ng, and because the LGPL made absorbing it into the -ng suite impossible. Rewriting was the way to get a templating layer that is part of the suite, permissively licensed, and built on the typed element tree rather than on regular expressions over serialised XML.

How it works

Four phases, of which only one sees text:

Phase What it does
1. Coalesce Merge adjacent runs Word split mid-tag, so {{ cust+omer }} is one tag again
2. Compile Hoist {%tr %}/{%p %} control tags onto the element they name; replace table directives with sentinels; serialise once
3. Render Jinja2, with XML autoescaping
4. Rebuild Re-parse into typed elements, expand \n/\t/\f into w:br/w:tab, resolve merges and shading against the real table

Structural decisions are made where the schema is known, so a rendered document is well-formed by construction. See docs/dev/architecture.md.

Contributing

uv sync
uv run pytest -q
uv run ruff check .

Requires uv.

Licence

MIT. See LICENSE.

Metadata

Release files for docxtpl-ng 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for docxtpl-ng 0.2.0
File Size Uploaded Provenance
docxtpl_ng-0.2.0.tar.gz 121.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for docxtpl-ng 0.2.0
File Interpreter ABI Platform
docxtpl_ng-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 203.7 kB

Release files / docxtpl_ng-0.2.0.tar.gz

Download URL docxtpl_ng-0.2.0.tar.gz
Size 121.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8fb338ea2ceb7e2cd8e33dd64fd9cb50a1cd31b64538837a22de26839e1bb306
BLAKE2b-256 checksum
How to use checksums
ac2e880ec2e7128b87ab1f2b20369879d0a72ff4c96a4d4cf032435703c90708
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

Release files / docxtpl_ng-0.2.0-py3-none-any.whl

Download URL docxtpl_ng-0.2.0-py3-none-any.whl
Size 82.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee7da6466c8b4d068449f344faee6761811fde2ad48153c7d5f7b1cd8ebc8422
BLAKE2b-256 checksum
How to use checksums
4fa79ce2f6a9d0aeef5ed10fdb2fe1b8b73708482e8a0f12ea57fe69b5843e56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

Security for docxtpl-ng 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for docxtpl_ng-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page