Skip to main content

python-odt-template

PyPI - Version PyPI - Python Version


Render ODT files using Jinja2 or the Django templates Language, with support for converting documents to PDF via the LibreOffice CLI.

Table of Contents

Installation

pip install python-odt-template

Usage

python-odt-template supports basic tags and control flow from Django or Jinja2, enabling variable printing and simple logic. However, advanced features like extends, include, and block are not supported. Directly mixing tags with text may lead to invalid ODT templates. Instead, we recommend using LibreOffice Writer's visual fields for dynamic content insertion. To do this, navigate to Insert > Fields > Other... (or press Ctrl+F2), select the Functions tab, choose Input field, and insert your code in the dialog that appears. This method supports simple control flow for dynamic content.

Additionally, python-odt-template introduces an image tag for both Jinja2 and Django, allowing image insertion by replacing a placeholder image in your document. Use the tag (e.g., {{ company_logo|image }}) and provide the corresponding image path in the context (company_logo). For Django, the image path is resolved using the first entry in STATICFILES_DIRS. For Jinja2, specify a media_path when creating the renderer to set the base path for images.

Jinja2

from python_odt_template import ODTTemplate
from python_odt_template.jinja import enable_markdown
from python_odt_template.jinja import get_odt_renderer
from python_odt_template.libreoffice import libreoffice

odt_renderer = get_odt_renderer(media_path="inputs")

with ODTTemplate("inputs/simple_template.odt") as template:
    odt_renderer.render(
        template,
        context={"document": document, "countries": countries},
    )
    template.pack("simple_template_rendered.odt")
    libreoffice.convert("simple_template_rendered.odt", "outputs")

Django

# settings.py

# Add at least one staticfiles dirs, this is what the imgae filter will use to find images
STATICFILES_DIRS = [BASE_DIR / "example" / "static"]

# Add the image filter to the builtins templates config
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        ...
        "OPTIONS": {
           ...
            "builtins": ["python_odt_template.django"],
        },
    },
]


# views.py
from python_odt_template import ODTTemplate
from python_odt_template.django import get_odt_renderer
from python_odt_template.libreoffice import convert_to_pdf


odt_renderer = get_odt_renderer()


def render_odt(request):
    with ODTTemplate("template.odt") as template:
        odt_renderer.render(
            template,
            {"image": "writer.png"},
        )
        template.pack("template_rendered.odt")
        libreoffice.convert("template_rendered.odt", "outputs")
    return FileResponse(
        open("outputs/template_rendered.pdf", "rb"), as_attachment=True, filename="template_rendered.pdf"
    )

Alternatives

Credits

Thanks to secretary for the enormous amount of integration work on Jinja2 and ODT.

License

python-odt-template is distributed under the terms of the MIT license.

Metadata

Release files for python-odt-template 0.5.1

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

Source distribution (sdist)

Source distribution for python-odt-template 0.5.1
File Size Uploaded
python_odt_template-0.5.1.tar.gz 272.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-odt-template 0.5.1
File Interpreter ABI Platform
python_odt_template-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 289.0 kB

Release files / python_odt_template-0.5.1.tar.gz

Download URL python_odt_template-0.5.1.tar.gz
Size 272.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4da7835f5b9a6dc8a79a77415c369e654ed623cba9af79972e0503388e0fff18
BLAKE2b-256 checksum
How to use checksums
728c7f4db38a1fd3a4736b574c3ca56b9cc5c1b20cb477f54e73b20ae2a82795
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.26.0

Release files / python_odt_template-0.5.1-py3-none-any.whl

Download URL python_odt_template-0.5.1-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77c8808996f9931c3ae333b609122acdb0931741d4ba72c75e8b2d2c6aa1caab
BLAKE2b-256 checksum
How to use checksums
f083978d94295edc6ce0949b54b75849c1542a3c06627cc5ec9b9c878af0afb5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.26.0

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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