Skip to main content

A python module to generate xls/x files from a xls/x template

Project description

xltpl

A python module to generate xls/x files from a xls/x template.

How it works

xltpl.writer uses xlrd to read xls files, and uses xlwt to write xls files.
xltpl.writerx uses openpyxl to read and write xlsx files.
When xltpl reads a xls/x 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 xls/x file.

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

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

{% row 'A1:F4, 0' %}
{% cell '0,0' %}{% endcell %}
{% cell '0,1' %}{% endcell %}
{% cell '0,2' %}{% endcell %}
{% cell '0,3' %}{% endcell %}
{% cell '0,4' %}{% endcell %}
{% cell '0,5' %}{% endcell %}
{% for row in rows %}
{% row 'A2:F2, 3' %}
{% cell '1,0' %}{% endcell %}
{% cell '1,1' %}{% endcell %}
{% cell '1,2' %}{% endcell %}
{% cell '1,3' %}{% endcell %}
{% cell '1,4' %}{% endcell %}
{% cell '1,5' %}{% endcell %}
{% row 'A3:B3, 4' %}
{% cell '2,0' %}{% sec '2,0,0' %}{{loop.index}} {% endsec %}{% endcell %}
{% cell '2,1' %}{% sec '2,1,0' %}{{row[0]}}{% endsec %}{% endcell %}
{% for item in row[1] %}
{% row 'C3, 6' %}
{% cell '2,2' %}{% sec '2,2,0' %}{{item[0]}}{% endsec %}{% endcell %}
{% for mac in item[1] %}
{% row 'D3:E3, 6' %}
{% cell '2,3' %}{% sec '2,3,0' %}{{ mac }}{% endsec %}{% endcell %}
{% cell '2,4' %}{% endcell %}
{% endfor %}
{% endfor %}
{% row 'F3, 4' %}
{% cell '2,5' %}{% endcell %}
{% row 'A4:F4, 3' %}
{% cell '3,0' %}{% endcell %}
{% cell '3,1' %}{% endcell %}
{% cell '3,2' %}{% endcell %}
{% cell '3,3' %}{% endcell %}
{% cell '3,4' %}{% endcell %}
{% cell '3,5' %}{% endcell %}
{% endfor %}
{% row 'A1:F4, 0' %}

xltpl 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 xltpl

How to use

  • To use xltpl, you need to be familiar with the syntax of jinja2 template.
  • Get a pre-written xls/x file as the template.
  • Insert variables in the cells, such as :
{{name}}
  • Insert control statements in the notes(comments) of cells, uses beforerow, beforecell or aftercell to seperate them :
    (v0.4) You can use 'cell{{A1}}beforerow{% for item in items %}aftercell{% endfor %}' to specify them.
beforerow{% for item in items %}
beforerow{% endfor %}
  • (v0.4) Use 'range' to specify some regions, using ';;' to seperate them:
range{{D3:E3}}beforerange{% for mac in item[1] %}afterrange{% endfor %};;
range{{C3:E3}}beforerange{% for item in row[1] %}afterrange{% endfor %};;
range{{A2:F4}}beforerange{% for row in rows %}afterrange{% endfor %}
  • Run the code
from xltpl.writer import BookWriter
writer = BookWriter('example.xls')
person_info = {'name': u'Hello Wizard'}
items = ['1', '1', '1', '1', '1', '1', '1', '1', ]
person_info['items'] = items
payloads = [person_info]
writer.render_book(payloads)
writer.save('result.xls')

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, xltpl uses this repo to support rich text reading and writing.

xlrd

xlrd does not extract print settings.
This repo does.

xlwt

xlwt always sets the default font to 'Arial'.
Excel measures column width units based on the default font.
This repo does not.

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

xltpl-0.7.tar.gz (23.7 kB view details)

Uploaded Source

Built Distribution

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

xltpl-0.7-py2.py3-none-any.whl (27.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file xltpl-0.7.tar.gz.

File metadata

  • Download URL: xltpl-0.7.tar.gz
  • Upload date:
  • Size: 23.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for xltpl-0.7.tar.gz
Algorithm Hash digest
SHA256 b7fe135bc4fb4f95f8fe60669da0a97f02ce9a224827d875844be20aacd51fd3
MD5 d69073e6c8c479f8a6f08f35a4d7139f
BLAKE2b-256 3b2d2de823127b8053957e7e714944a24cd091a5d1ed6a73fc7d9cd4133bdf4b

See more details on using hashes here.

File details

Details for the file xltpl-0.7-py2.py3-none-any.whl.

File metadata

  • Download URL: xltpl-0.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.5

File hashes

Hashes for xltpl-0.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 29ad7f21acac4c905278e7e815259f5f3f2e8ec53bee5e15ecfcb53e52e816e1
MD5 78b6fca1bcc5ea61e0f89163f48f9208
BLAKE2b-256 2f4deaf9d606c08b4625af9e9d9f37e168190a53d7e71a9c6ad08e6f3bc6a403

See more details on using hashes here.

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