Skip to main content

Incremental document rendering for docx templates

Project description

Python uv Jinja

TemplaterX

This is a Python library for incremental document rendering, built on top of docxtpl.

Unlike traditional template rendering engines that require the full context upfront, TemplaterX allows you to render documents progressively, applying partial contexts over multiple render calls.

Key Concept

Render documents incrementally, only when data is available.

Each call applies only the data that can be safely rendered at that moment. Unresolved placeholders and control blocks remain intact until all required variables are present.

Features

  • Incremental Rendering;

  • Lower Memory Usage:

    • Avoids building large in-memory contexts.
  • Control Block Safety:

    • Control blocks delimited with {% ... %} are rendered only when all internal variables are available;
    • Otherwise, all its placeholders are preserved.
  • docxtpl based:

    • Uses standard docxtpl syntax;
    • Fully compatible with existing .docx templates (see Running Tests section).

Installation

pip install templaterx

Basic Usage

from templaterx import TemplaterX

tplx = TemplaterX("template.docx")

tplx.render({"name": "John Doe"})
tplx.render({"role": "Some role"})
tplx.render({"salary": 10000})

tplx.save("output.docx")

Template Syntax

TemplaterX does not introduce a new template syntax.

All templates follow the same syntax and rules defined by docxtpl, which is itself based on Jinja2.

For a detailed reference, please see the following docs:

Control Block Rule

A control block is rendered only if all variables used inside it are present in the context.

If any required variable is missing, placeholders remain unchanged.

Incremental vs Monolithic Rendering

Traditional template engines such as docxtpl operate using a monolithic rendering model: the entire context must be fully constructed and loaded into memory before rendering can begin.

This approach works well for small or medium-sized documents, but it introduces significant memory pressure when dealing with:

  • Large collections;

  • Data coming from multiple sources;

  • Streaming or paginated datasets.

Use Cases

  • Large document generation;

  • Streaming data pipelines;

  • Reports built from multiple data sources;

  • Memory-constrained environments;

  • Staged or conditional document assembly.

Development & Contributing

Contributions, feedback, and experimentation are welcome!

In short, this project is closely tied to understanding how different execution strategies affect memory usage, composability, and data flow.

If you are interested in improving the project, exploring new use cases, or validating its behavior in different environments, the sections below should help you get started.


Running Tests

TemplaterX uses pytest for automated testing.

# To run the full test suite locally
uv run pytest

In order to ensure compatibility and feature parity, the test suite was largely derived from the original docxtpl tests, with small adaptations to use pytest and to account for the incremental rendering model.

The tests were executed using the same .docx templates provided by the original author of docxtpl.


Benchmarks

Disclaimer

Any benchmark comparing docxtpl and TemplaterX should be interpreted as a comparison between rendering models, not as a direct, absolute comparison between libraries.

  • docxtpl enforces a monolithic render by design;
  • TemplaterX enables an incremental render by design.

Benchmarks that show lower peak memory usage for TemplaterX are demonstrating the impact of incremental rendering, not merely an optimization of the underlying template engine.

The primary design goal of TemplaterX is to enable progressive rendering as data becomes available, which is useful when fetching data from external sources such as databases, APIs, or streaming pipelines.

Lower peak memory usage is a positive and expected consequence of this execution model, rather than its sole motivation.


Running Memory Benchmark

The project includes a small memory benchmark intended to illustrate architectural differences between rendering strategies.

In a nutshell, we are comparing:

  • A monolithic rendering model, where the full context must be loaded before rendering;
  • An incremental rendering model, where data can be rendered progressively as it becomes available.

To run the memory benchmark:

uv run python -m benchmarks.memory [--lists-number N] [--list-size N]

Where:

  • --lists-number: number of large collections rendered;
  • --list-size: number of items per collection.

Example:

uv run python -m benchmarks.memory --lists-number 5 --list-size 10000

The benchmark reports peak Python memory usage observed during rendering. Results may vary depending on the runtime environment and dataset size, and should be interpreted as a qualitative comparison that highlights trade-offs between execution models.

Benchmarks are exploratory and are primarily meant to support design discussions and informed decision-making.


If you have ideas, suggestions, or alternative approaches to incremental document rendering, feel free to open an issue or start a discussion.

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

templaterx-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

templaterx-0.1.0-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file templaterx-0.1.0.tar.gz.

File metadata

  • Download URL: templaterx-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for templaterx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a1b93d65866201f5640c2e8b160243fbb8e9fde03f37ab2690470aaef61a4d19
MD5 08b6dbf2cb4c325b5b4b0a01d829238f
BLAKE2b-256 7bea629a985cc8b667def08d26324914f5f016c3a4166b151731582a6c7e0796

See more details on using hashes here.

File details

Details for the file templaterx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: templaterx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.20 {"installer":{"name":"uv","version":"0.9.20","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for templaterx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 138658ef71cf1b22422d47ab61eaebde68a086a8c28bff49a78d81446166ba16
MD5 2df17b3a9fdea068dd03b813c866e96d
BLAKE2b-256 5c575c6e7cf3520f5127a66b21243495997ae48a4952fb5555853d99ae2c8f1c

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