python-odt-template


Namepython-odt-template JSON
Version 0.5.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2024-06-27 11:35:32
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords django jinja2 libreoffice odt open-document-text
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # python-odt-template

[![PyPI - Version](https://img.shields.io/pypi/v/python-odt-template.svg)](https://pypi.org/project/python-odt-template)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/python-odt-template.svg)](https://pypi.org/project/python-odt-template)

-----

> [!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

- [python-odt-template](#python-odt-template)
  - [Table of Contents](#table-of-contents)
  - [Installation](#installation)
  - [Usage](#usage)
    - [Jinja2](#jinja2)
    - [Django](#django)
  - [Alternatives](#alternatives)
  - [Credits](#credits)
  - [License](#license)

## Installation

```console
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

```python
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

```python
# 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

- [python-docx-template](https://github.com/elapouya/python-docx-template)

## Credits

Thanks to [secretary](https://github.com/christopher-ramirez/secretary) for the enormous amount of integration work on Jinja2 and ODT.

## License

`python-odt-template` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "python-odt-template",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "django, jinja2, libreoffice, odt, open-document-text",
    "author": null,
    "author_email": "Tobi DEGNON <tobidegnon@proton.me>",
    "download_url": "https://files.pythonhosted.org/packages/c2/24/64038ee94fc1ad651ca01ed714770c9d186e3cef531c2508244e5b1925ff/python_odt_template-0.5.0.tar.gz",
    "platform": null,
    "description": "# python-odt-template\n\n[![PyPI - Version](https://img.shields.io/pypi/v/python-odt-template.svg)](https://pypi.org/project/python-odt-template)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/python-odt-template.svg)](https://pypi.org/project/python-odt-template)\n\n-----\n\n> [!IMPORTANT]\n> This package currently contains minimal features and is a work-in-progress\n\nRender ODT files using Jinja2 or the Django templates Language, with support for converting documents to PDF via the LibreOffice CLI. \n\n## Table of Contents\n\n- [python-odt-template](#python-odt-template)\n  - [Table of Contents](#table-of-contents)\n  - [Installation](#installation)\n  - [Usage](#usage)\n    - [Jinja2](#jinja2)\n    - [Django](#django)\n  - [Alternatives](#alternatives)\n  - [Credits](#credits)\n  - [License](#license)\n\n## Installation\n\n```console\npip install python-odt-template\n```\n\n## Usage\n\n`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.\n\nAdditionally, `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.\n\n> [!IMPORTANT]\n> For now, you can get more detailed information at the Secretary project's readme at https://github.com/christopher-ramirez/secretary.\n\n\n### Jinja2\n\n```python\nfrom python_odt_template import ODTTemplate\nfrom python_odt_template.jinja import enable_markdown\nfrom python_odt_template.jinja import get_odt_renderer\nfrom python_odt_template.libreoffice import libreoffice\n\nodt_renderer = get_odt_renderer(media_path=\"inputs\")\n\nwith ODTTemplate(\"inputs/simple_template.odt\") as template:\n    odt_renderer.render(\n        template,\n        context={\"document\": document, \"countries\": countries},\n    )\n    template.pack(\"simple_template_rendered.odt\")\n    libreoffice.convert(\"simple_template_rendered.odt\", \"outputs\")\n```\n\n### Django\n\n```python\n# settings.py\n\n# Add at least one staticfiles dirs, this is what the imgae filter will use to find images\nSTATICFILES_DIRS = [BASE_DIR / \"example\" / \"static\"]\n\n# Add the image filter to the builtins templates config\nTEMPLATES = [\n    {\n        \"BACKEND\": \"django.template.backends.django.DjangoTemplates\",\n        ...\n        \"OPTIONS\": {\n           ...\n            \"builtins\": [\"python_odt_template.django\"],\n        },\n    },\n]\n\n\n# views.py\nfrom python_odt_template import ODTTemplate\nfrom python_odt_template.django import get_odt_renderer\nfrom python_odt_template.libreoffice import convert_to_pdf\n\n\nodt_renderer = get_odt_renderer()\n\n\ndef render_odt(request):\n    with ODTTemplate(\"template.odt\") as template:\n        odt_renderer.render(\n            template,\n            {\"image\": \"writer.png\"},\n        )\n        template.pack(\"template_rendered.odt\")\n        libreoffice.convert(\"template_rendered.odt\", \"outputs\")\n    return FileResponse(\n        open(\"outputs/template_rendered.pdf\", \"rb\"), as_attachment=True, filename=\"template_rendered.pdf\"\n    )\n```\n\n## Alternatives\n\n- [python-docx-template](https://github.com/elapouya/python-docx-template)\n\n## Credits\n\nThanks to [secretary](https://github.com/christopher-ramirez/secretary) for the enormous amount of integration work on Jinja2 and ODT.\n\n## License\n\n`python-odt-template` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": null,
    "version": "0.5.0",
    "project_urls": {
        "Documentation": "https://github.com/Tobi-De/python-odt-template#readme",
        "Issues": "https://github.com/Tobi-De/python-odt-template/issues",
        "Source": "https://github.com/Tobi-De/python-odt-template"
    },
    "split_keywords": [
        "django",
        " jinja2",
        " libreoffice",
        " odt",
        " open-document-text"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "6d075ba4d3fb37249847c2081e2375e1b352a163d2cba8d3e76cfa60b9cb0f3e",
                "md5": "0c5e6d819b0d2f41b3f7d105f80b41ac",
                "sha256": "43d7d7bdbb9ee631020fd66f63513f87c6b71f40f3d3d59f787a710a9286b850"
            },
            "downloads": -1,
            "filename": "python_odt_template-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0c5e6d819b0d2f41b3f7d105f80b41ac",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 16794,
            "upload_time": "2024-06-27T11:35:34",
            "upload_time_iso_8601": "2024-06-27T11:35:34.552522Z",
            "url": "https://files.pythonhosted.org/packages/6d/07/5ba4d3fb37249847c2081e2375e1b352a163d2cba8d3e76cfa60b9cb0f3e/python_odt_template-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c22464038ee94fc1ad651ca01ed714770c9d186e3cef531c2508244e5b1925ff",
                "md5": "3cef913440f7645a2879eb85382024bd",
                "sha256": "cedd284d8cbbb182f95d7a8074fb754a99f4a7842d720096ea9cce3b73c6aa76"
            },
            "downloads": -1,
            "filename": "python_odt_template-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3cef913440f7645a2879eb85382024bd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 271999,
            "upload_time": "2024-06-27T11:35:32",
            "upload_time_iso_8601": "2024-06-27T11:35:32.255165Z",
            "url": "https://files.pythonhosted.org/packages/c2/24/64038ee94fc1ad651ca01ed714770c9d186e3cef531c2508244e5b1925ff/python_odt_template-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-27 11:35:32",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Tobi-De",
    "github_project": "python-odt-template#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "python-odt-template"
}
        
Elapsed time: 0.37157s