Skip to main content

Create Microsoft word documents from templates

Project description

DOCX Renderer

This package lets you run your Microsoft Word documents like a script. You can insert placeholders in the document and use either a Python function or an equivalent command-line tool to convert it into an output rendered document.

Documentation

Installation

pip install docx-renderer

Usage

Below is a simple example.

from docx_renderer import DOCXRenderer
p = DOCXRenderer("template.docx")

someval = "world!"
def mymethod(abc):
    return f"{abc} " * 5

p.render(
    "output.docx", 
    {
        "variable": someval, "mymethod": mymethod, "myimage": "is_it_worth.png"
    }
)

This will replace placeholders in the template document with the provided values.

Before

Before

After

After

You can define some functions within the document itself by writing Python code in the comments section. The variables and functions in this code can be used in the main document.

For example: write the following in one of the comments in the document.

```python
def myfunc(input):
    return input * 42
```

Now you can, for example, add the placeholder {{{myfunc(42)}}} in your document.

If the template document is a self-contained Python script (i.e., it does not require variable values and function definitions to be passed from outside), you can generate the output document directly from the command line using the following command.

docx-renderer input_template.docx output_file.docx

Placeholders

You can have placeholders for text, images, or tables. Placeholders can be added as regular text. All placeholders should be enclosed within a pair of triple braces ({{{ and }}}).

Text

Any placeholder which can be evaluated into a string can act as a text placeholder.

For example: {{{"hello " * 10/2}}} or {{{abs(-2)}}}

Image

If you have added :image() as a suffix to the Python statement, the renderer will try to convert the value of the Python statement to a file location and insert an image from that file location.

For example: {{{"c:\\temp\\myimage.png":image()}}}

Table

Tables are similar to images, but instead of a string, the Python statement should evaluate to a list of lists. Then you can add :table() as a suffix, and it will be converted to a table inside the document.

For example: {{{[["col1", "col2", "col3"],[1, 2, 3]]:table()}}} will render to

col1 col2 col3
1 2 3

You can create nested tables as well (if you put a variable placeholder inside an existing table which evaluates to a list of lists with :table() plugin)

Hyperlinks

You can use placeholders with hyperlinks. These hyperlinks inturn can contain other placeholders which should evaluate to an external link. Please note that target placeholders dont support any plugins.

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

docx_renderer-0.3.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

docx_renderer-0.3.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file docx_renderer-0.3.0.tar.gz.

File metadata

  • Download URL: docx_renderer-0.3.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for docx_renderer-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e06ffe8c96be9bcd14621016465624b23d9b73fae0b6f9a6c800571e7aae7a35
MD5 285e2a4713265f15eb70020e6bd895e1
BLAKE2b-256 fffc6b96f059771bff91609cd51ab9535f89bcf319392f5f7869b357efce81bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for docx_renderer-0.3.0.tar.gz:

Publisher: python-publish.yml on idling-mind/docx_renderer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file docx_renderer-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: docx_renderer-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for docx_renderer-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed6553db1b7963052561916f63e1575a43b5c95d2700ff5c3cdbe87177c7c09
MD5 b77f822169481986dc47cafc72f4e0dd
BLAKE2b-256 69f997aca0fc9c12275ff82aa3638c709f982707823dada45baceeffe0535975

See more details on using hashes here.

Provenance

The following attestation bundles were made for docx_renderer-0.3.0-py3-none-any.whl:

Publisher: python-publish.yml on idling-mind/docx_renderer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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