Skip to main content

Docform

Python License: LGPL-2.1

Fill document templates with data.

Design the document in Word with whatever layout, styles, headers and images you need, and put Jinja2 tags where the data goes. Docform renders it into a finished .docx, so you can generate contracts, invoices, reports, certificates and letters in bulk from Python.

Install

pip install docform

# with subdocument support
pip install "docform[subdoc]"

Quickstart

In Word, write a template like this and save it as invoice_template.docx:

Invoice for {{ customer }}

{%tr for item in items %}
| {{ item.name }} | {{ item.qty }} |
{%tr endfor %}

Total: {{ total }}

Then render it:

from docform import DocxTemplate

doc = DocxTemplate("invoice_template.docx")
doc.render({
    "customer": "Acme",
    "items": [{"name": "Widget", "qty": 3}, {"name": "Gadget", "qty": 1}],
    "total": "$120.00",
})
doc.save("invoice_acme.docx")

Everything you set up in Word, like fonts, logos and page layout, stays intact. Only the tags are replaced.

How it works

flowchart LR
    T["Word template<br/>(.docx with Jinja2 tags)"] --> R{{"Docform<br/>render()"}}
    D["Your data<br/>(dict, JSON, database rows)"] --> R
    R --> O["Finished document<br/>(.docx)"]

Example

This invoice comes from examples/invoice. The template on the left is a normal Word file. Running python render.py turns it into the document on the right.

Template (invoice_template.docx) Rendered (invoice_acme.docx)
Invoice template with Jinja2 tags Rendered invoice with customer data and four line items

A few things to notice in the template:

  • {%tr for item in items %} and {%tr endfor %} sit in their own table rows. Docform repeats the row between them once per item and removes the tag rows.
  • {%p if paid %} works the same way for paragraphs, so the payment note switches between "PAID" and a reminder.
  • Formatting on a tag, like the bold blue {{ total }}, carries over to the value.

Features

  • Full Jinja2 syntax: variables, conditions, loops, filters and custom filters
  • Paragraph, table row, table cell and run level tags ({%p %}, {%tr %}, {%tc %}, {%r %}) that remove the surrounding Word element so loops don't leave empty rows behind
  • Rich text with colors, fonts, bold, italic, links and right-to-left text
  • Inline images, plus replacing pictures and embedded objects already in the template
  • Subdocuments: build parts with python-docx or merge in existing .docx files
  • Headers, footers, footnotes and document properties
  • Cell background colors and horizontal and vertical cell merges
  • Listing undeclared template variables before rendering
  • A command line interface for JSON-driven rendering

Command line

python -m docform template.docx data.json output.docx

Use -o to overwrite an existing output file and -q for quiet mode.

Rendering untrusted templates

Templates run Jinja2 code. If your users can upload their own templates, render them with a sandboxed environment:

from jinja2.sandbox import SandboxedEnvironment

doc.render(context, jinja_env=SandboxedEnvironment(autoescape=True))

Formats

Word (.docx) is the only format today. Each format lives in its own module, so new ones can be added without breaking existing code:

from docform.docx import Template

Documentation

The full template syntax and API reference are in docs/. examples/ has complete, runnable examples, and tests/ has a small script for every feature.

Adopters

Using Docform in production or in an open source project? Add yourself to ADOPTERS.md.

Citing

If you use Docform in research, please cite it. GitHub's "Cite this repository" button reads CITATION.cff.

Credits

Docform started as a fork of python-docx-template by Eric Lapouyade and builds on python-docx and Jinja2.

License

LGPL-2.1.

Metadata

Release files for docform 0.21.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 docform 0.21.0
File Size Uploaded Provenance Actions
docform-0.21.0.tar.gz 2.0 MB With provenance Details for docform-0.21.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for docform 0.21.0
File Interpreter ABI Platform Actions
docform-0.21.0-py3-none-any.whl Python 3 none any Details for docform-0.21.0-py3-none-any.whl

Total release size: 2.1 MB

Release files / docform-0.21.0.tar.gz

Download URL docform-0.21.0.tar.gz
Size 2.0 MB
Tags Source
SHA-256 checksum
How to use checksums
994f687bee8ca98759815ca3f61505c547a8a98ccd3a63a7c88788fb4feb9ad7
BLAKE2b-256 checksum
How to use checksums
580e45121cb92d86c4e20bb5347eed462021f0f33b29128d984ac4d91b8f8071
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Release files / docform-0.21.0-py3-none-any.whl

Download URL docform-0.21.0-py3-none-any.whl
Size 29.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3516af0aef1a8a75234433a4fbae22dbf3cd4772ea1fdc7e8308523cefe0fe85
BLAKE2b-256 checksum
How to use checksums
2be9ac10e5b4f2a2520e67ce573925916282e173d3ebaf6235a7340c4ee543d5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 15, 2026.

Transparency log

Security for docform 0.21.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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for docform-0.21.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 Nov 15, 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.21.0 This release

2 release files Full provenance

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