Skip to main content

Use xltpl.

xlsxtpl

A python module to generate xlsx files from a xlsx template.

How it works

xlsxtpl uses openpyxl to read and write .xlsx files, uses jinja2 as its template engine.
When xlsxtpl reads a .xlsx file, it creates a tree for each worksheet.
Then, it translates the tree to a jinja2 template with custom tags.
When the template is rendered, jinja2 extensions of cumtom tags call corresponding tree nodes to write the .xlsx file.

Syntax

xlsxtpl uses jinja2 as its template engine, follows the syntax of jinja2 template.

Each worksheet is translated to a jinja2 template with custom tags.

...
...
{% row 45 %}
{% cell 46 %}{% endcell %}
{% cell 47 %}{% endcell %}
{% cell 48 %}{{address}}  {%xv v%}{% endcell %}
{% cell 49 %}{% endcell %}
{% cell 50 %}{% endcell %}
{% cell 51 %}{% endcell %}
{% cell 52 %}{% endcell %}
{% cell 53 %}{% endcell %}
{% row 54 %}
{% cell 55 %}{% endcell %}
{% cell 56 %}{% sec 0 %}{{name}}{% endsec %}{% sec 1 %}{{address}}{% endsec %}{% endcell %}
...
...
{% for item in items %}
{% row 64 %}
{% cell 65 %}{% endcell %}
{% cell 66 %}{% endcell %}
{% cell 67 %}{% endcell %}
{% cell 68 %}{% endcell %}
{% cell 69 %}{% endcell %}
{% cell 70 %}{% endcell %}
{% cell 71 %}{% endcell %}
{% cell 72 %}{% endcell %}
{% endfor %}
...
...

xlsxtpl added 4 custom tags: row, cell, sec, and xv.
row, cell, sec are used internally, used for row, cell and rich text.
xv is used to define a variable.
When a cell contains only a xv tag, this cell will be set to the type of the object returned from the variable evaluation.
For example, if a cell contains only {%xv amt %}, and amt is a number, then this cell will be set to Number type, displaying with the style set on the cell.
If there is another tag, it is equivalent to {{amt}}, will be converted to a string.

Installtion

pip install xlsxtpl

How to use

See examples.

Notes

Rich text

Openpyxl does not preserve the rich text it read. A temporary workaround for rich text is provided in this repo (2.6). For now, xlsxtpl uses this repo to support rich text reading and writing.

Metadata

Release files for xlsxtpl 0.3.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 xlsxtpl 0.3.1
File Size Uploaded Provenance
xlsxtpl-0.3.1.tar.gz 3.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for xlsxtpl 0.3.1
File Interpreter ABI Platform
xlsxtpl-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.9 kB

Release files / xlsxtpl-0.3.1.tar.gz

Download URL xlsxtpl-0.3.1.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
906ea1e30ca4c78621e2ff561580da77a2f1d72037365017a5ac79d518fefbbc
BLAKE2b-256 checksum
How to use checksums
34a0443adea637c6430f8ac8ffbbc737e11d05f0ba3c23b04d5ef188435df78b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/3.7.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

Release files / xlsxtpl-0.3.1-py3-none-any.whl

Download URL xlsxtpl-0.3.1-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a942dbb7c743b2142ac7a58d3288080bbf194c0cf30bb4c58a245eabae5f643
BLAKE2b-256 checksum
How to use checksums
2dea0b85ef03f0e68bfc0011d235bbb1e2b18709f3e8b414cb1e520fe548bd67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/3.7.0 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.5

Security for xlsxtpl 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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