Skip to main content

No project description provided

Project description

python-odt-template

PyPI - Version PyPI - Python Version


[!IMPORTANT] This package currently contains minimal features and is a work-in-progress

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.

[!IMPORTANT] For now, you can get more detailed information at the Secretary project's readme at https://github.com/christopher-ramirez/secretary.

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.

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

python_odt_template-0.5.0.tar.gz (272.0 kB view details)

Uploaded Source

Built Distribution

python_odt_template-0.5.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file python_odt_template-0.5.0.tar.gz.

File metadata

  • Download URL: python_odt_template-0.5.0.tar.gz
  • Upload date:
  • Size: 272.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.26.0

File hashes

Hashes for python_odt_template-0.5.0.tar.gz
Algorithm Hash digest
SHA256 cedd284d8cbbb182f95d7a8074fb754a99f4a7842d720096ea9cce3b73c6aa76
MD5 3cef913440f7645a2879eb85382024bd
BLAKE2b-256 c22464038ee94fc1ad651ca01ed714770c9d186e3cef531c2508244e5b1925ff

See more details on using hashes here.

File details

Details for the file python_odt_template-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_odt_template-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43d7d7bdbb9ee631020fd66f63513f87c6b71f40f3d3d59f787a710a9286b850
MD5 0c5e6d819b0d2f41b3f7d105f80b41ac
BLAKE2b-256 6d075ba4d3fb37249847c2081e2375e1b352a163d2cba8d3e76cfa60b9cb0f3e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page