A reusable Office document template render engine for DOCX, XLSX, and PPTX.
Project description
Stencil
Stencil is a Python render engine for reusable Office document templates.
It is designed for workflows where a .docx, .xlsx, or .pptx template contains placeholders, loops, and conditionals, and application data fills the template to produce a finished Office document or PDF.
stencil render invoice.docx data.json --output invoice.pdf
The project started with DOCX because it was the fastest path to a useful internal tool. XLSX and PPTX now use separate Office XML engines behind the same public API.
Status
Pre-alpha internal Office render package.
What exists now:
uv-compatible Python package metadata- Python
>=3.12 stencilimport packagerender()API for DOCX, PPTX, and XLSX templates- PPTX text substitutions and repeated-slide loops
stencil renderCLI command for JSON data- PDF output through LibreOffice conversion for DOCX, PPTX, and XLSX templates
- optional FastAPI service wrapper for trusted internal callers
- template authoring guidance for supported DOCX, PPTX, and XLSX features
- XLSX cell substitutions and row loops
- versioned sample templates covered by expected-output tests
- DOCX-first roadmap in local ignored docs
What does not exist yet:
- distributed queues, untrusted template sandboxing, or hosted multi-tenant operations
Naming
The product, import package, and CLI command are named stencil.
The PyPI distribution name is currently office-stencil because stencil is already taken on PyPI by an old package.
[project]
name = "office-stencil"
[project.scripts]
stencil = "stencil.cli:app"
API
from stencil import render
document = render(
template_path="invoice.docx",
data={
"customer": "Acme",
"line_items": [
{"description": "Implementation", "amount": 1200},
],
},
output_format="docx",
)
DOCX templates can currently render to docx or pdf. PPTX templates can render to pptx or pdf. XLSX templates can render to xlsx or pdf. The local Python API is the stable package boundary for the internal tool while the project remains pre-alpha.
CLI
stencil render invoice.docx data.json --output invoice.docx
stencil render invoice.docx data.json --output invoice.pdf
stencil render deck.pptx data.json --output deck.pptx
stencil render deck.pptx data.json --output deck.pdf
stencil render workbook.xlsx data.json --output workbook.xlsx
stencil render workbook.xlsx data.json --output workbook.pdf
The CLI reads a top-level JSON object from the data file and writes the rendered document bytes to the output path.
PDF output requires LibreOffice to be installed and available as soffice or libreoffice.
Optional Service
The Python API and CLI are the primary package boundary. When another internal system needs HTTP access, install the optional API dependencies and run the trusted service wrapper:
uv sync --extra api --dev
uv run uvicorn 'stencil.api:create_app' --factory
The service exposes:
GET /healthzfor process healthGET /metricsfor in-memory render and failure countersPOST /renderwithtemplate_path,data, and optionaloutput_format
For deployments that serve a known template directory, pass a template root from application code:
from stencil.api import create_app
app = create_app(template_root="/srv/stencil/templates")
The service is still trusted-internal infrastructure. It logs render outcomes and keeps per-process counters, but it does not sandbox untrusted templates or add a distributed queue.
Template Authoring
Stencil templates are regular Word documents with Jinja-style tags in editable text. Design the document visually in Word or LibreOffice first, then replace only dynamic values with placeholders.
Supported DOCX features:
- variable replacement, such as
{{ customer.name }} - conditionals, such as
{% if invoice.note %}...{% endif %} - loops over lists, such as
{% for item in line_items %}...{% endfor %} - nested object and list access supported by Jinja2
- formatting inherited from the placeholder text in the template
Supported XLSX features:
- variable replacement in text cells, such as
{{ customer.name }} - typed whole-cell expressions, such as
{{ invoice.total }} - row loops with one or more body rows between
{% for item in line_items %}and{% endfor %} - style, number-format, row-height, and multiple-sheet preservation for rendered cells
- relative formula translation inside cloned loop rows
Supported PPTX features:
- variable replacement in slide text, such as
{{ customer.name }} - conditionals inside a text paragraph, such as
{% if project.on_track %}...{% endif %} - repeated slides with marker slides containing
{% for item in items %}and{% endfor %} - cloned-slide relationship, presentation-order, and content-type entries for repeated slides
Authoring rules:
- Keep one complete Jinja tag in one editable text run when possible. If Word splits a tag across runs, retype the whole tag in one pass.
- Keep styles, table borders, headers, footers, and layout in the DOCX template, not in JSON data.
- For XLSX row loops, put the loop start and end tags in their own cells on their own rows.
- For PPTX repeated-slide loops, put the loop start and end tags on their own marker slides.
- Use a top-level JSON object for CLI data.
- Use UTF-8 JSON and pass plain values, lists, and objects.
- Keep templates trusted. Stencil does not sandbox untrusted Office templates.
Known limitations:
- DOCX output formats are
docxandpdf; PPTX output formats arepptxandpdf; XLSX output formats arexlsxandpdf. - PDF output requires LibreOffice.
- PPTX chart/data rewriting, speaker-note rewriting, XLSX chart rewriting, complex formula-range adjustment, hosted APIs, Redis, Celery, and sandboxing are intentionally outside the current package boundary.
- Inline image replacement is not part of the packaged internal-tool API yet.
Troubleshooting:
Template file does not exist: check the template path passed to the API or CLI.Unsupported template format: use a.docx,.xlsx, or.pptxtemplate.Unsupported output format: usedocx,xlsx, orpdf, or choose an output path with one of those suffixes.Failed to render DOCX template: check Jinja syntax and confirm every referenced field exists in the JSON object.Failed to render PPTX template: check Jinja syntax, repeated-slide marker slides, and confirm every referenced field exists in the JSON object.Failed to render XLSX template: check Jinja syntax, loop rows, and confirm every referenced field exists in the JSON object.PDF conversion requires LibreOffice: install LibreOffice and confirmsofficeorlibreofficeis onPATH.LibreOffice PDF conversion timed out: open the rendered document manually and simplify or repair the template.
Example
See examples/ for a runnable DOCX example with:
examples/templates/invoice.docxexamples/data/invoice.jsonexamples/templates/styled-status-report.docxexamples/data/status-report.jsonexamples/templates/pptx-status.pptxexamples/data/pptx-status.json
These examples are versioned with the package and covered by tests that check the rendered user-visible text.
uv sync --dev
uv run stencil render examples/templates/invoice.docx examples/data/invoice.json --output examples/out/invoice.docx
uv run stencil render examples/templates/invoice.docx examples/data/invoice.json --output examples/out/invoice.pdf
uv run stencil render examples/templates/styled-status-report.docx examples/data/status-report.json --output examples/out/styled-status-report.docx
uv run stencil render examples/templates/pptx-status.pptx examples/data/pptx-status.json --output examples/out/pptx-status.pptx
Roadmap
- Foundation and proof of shape
- DOCX MVP
- PDF conversion and reliability
- Internal package polish
- XLSX engine
- PPTX engine
- Optional service layer and operations
All roadmap phases now have a first internal implementation. Future work should be driven by specific production needs, such as persistent metrics export, queue-backed workloads, sandboxing for untrusted templates, image replacement, or richer chart/formula rewrites.
The detailed local roadmap lives in docs/, which is intentionally ignored by git because it is personal planning material.
Dependencies
Runtime dependencies:
jinja2: template expressions, conditionals, and loopsdocxtpl: DOCX rendering for the first production milestonelxml: Office XML parsing and manipulationopenpyxl: XLSX workbook loading, editing, and serializationtyper: CLI framework
Optional API dependencies:
fastapi: optional HTTP service layeruvicorn[standard]: ASGI server for the optional API
Optional worker dependencies:
rq: lightweight Redis-backed queue optioncelery: larger distributed worker optionredis: queue/backend client
Development dependencies:
pytest: test runnerpytest-cov: coverage reportingruff: linting and import sortingmypy: static type checking
External system dependency for PDF output:
- LibreOffice /
soffice: converts rendered Office documents to PDF
Local Development
This repo uses uv, but dependencies have not been installed yet.
Install dependencies when ready:
uv sync --dev
Run tests:
uv run pytest
Run linting:
uv run ruff check .
Run type checking:
uv run mypy
Publishing
The repo includes a GitHub Actions workflow at .github/workflows/publish.yml.
It publishes to PyPI when changes land on main:
- Check out the repo.
- Install
uv. - Set up Python 3.12.
- Install dependencies with
uv sync --dev. - Run tests with
uv run pytest. - Run linting with
uv run ruff check .. - Build the package with
uv build. - Read the matching release notes from
CHANGELOG.md. - Create a GitHub Release tagged from the package version, such as
v0.1.0. - Attach the built wheel and source distribution to the release.
- Publish with the
PYPI_API_TOKENGitHub Actions secret.
One-time PyPI token setup:
- Create or use the PyPI project named
office-stencil. - Create a PyPI API token. Prefer a project-scoped token for
office-stencilafter the first release exists. - Add the token to GitHub as an Actions secret named
PYPI_API_TOKEN. - The publish workflow uses
user: __token__andpassword: ${{ secrets.PYPI_API_TOKEN }}.
PyPI and GitHub releases both require every upload to have a new version. Before merging a feature branch into main, update the version in pyproject.toml and add a matching section to CHANGELOG.md. The runtime stencil.__version__ is read from installed package metadata, so there is no second version constant to keep in sync. The workflow creates the GitHub tag from that version and uses the matching changelog section as the release notes.
Dependabot
The repo includes .github/dependabot.yml.
Dependabot checks weekly for:
uvdependency updates frompyproject.toml- GitHub Actions updates from
.github/workflows/
Dependabot will open pull requests. Those PRs should go through the same CI and branch-protection rules as feature branches.
Design Notes
Office files are zipped XML packages, but each format has different failure modes.
- DOCX can split template tags across Word text runs.
- XLSX often stores text in shared strings, so naive replacement can mutate unrelated cells.
- PPTX repeated slides require cloning slide XML, relationships, presentation ordering, and content type entries.
- PDF conversion through LibreOffice needs timeouts, retries, cleanup, and worker isolation.
Stencil should stay milestone-driven: keep the reliable Office engines focused, then use real needs to decide when service infrastructure is worth the extra complexity.
License
MIT. See LICENSE.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file office_stencil-0.5.0.tar.gz.
File metadata
- Download URL: office_stencil-0.5.0.tar.gz
- Upload date:
- Size: 174.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a5bea19ec2a26da162b7383b182a3c7ec2847c713a5f29cdbfb9f4e406b2ec8
|
|
| MD5 |
2d31bcb578a603be6aa5149df307d1ae
|
|
| BLAKE2b-256 |
4c46d64159fd9b5003fcaa6abc20cd090d95d7429aa92bed11f5baa076c96c49
|
File details
Details for the file office_stencil-0.5.0-py3-none-any.whl.
File metadata
- Download URL: office_stencil-0.5.0-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ef59c1f7047b602e7a875344a6fb4b28f85c9a1b131f47f32a70c4bfec4486e
|
|
| MD5 |
a1d58753c62b1a0590f9c31353e032c8
|
|
| BLAKE2b-256 |
c2cc8a0105ecb568c0eefa3c48ab6432c9fd4e49a9a03916324a5213f775f8dd
|