Skip to main content

Generate documents from templates.

Project description

https://img.shields.io/pypi/v/document-template.svg https://img.shields.io/pypi/pyversions/document-template.svg https://img.shields.io/pypi/l/document-template.svg https://img.shields.io/pypi/wheel/document-template.svg

安装方法

使用 pip 安装

pip install document-template

使用方法

参考 test.pytest.html :

test.html:

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>#{title}</title>
</head>
<body>
<h1>#{head}</h1>
<a href="#{url}">#{url}</a>
<br>
Test.......
<hr>
<span style="font-size: larger;font-weight: bold">#{large_font}</span>
<br>
为真时显示:#{bool:show_span}<span>显示的内容</span>#{bool:show_span}
;;分割;;#{bool:show_span}show_span is True#{bool:show_span}
<br>
#{copy:start}多行文字,替换局部内容:#{contents} 和 #{another_contents}<br>
#{copy:end}
</body>
</html>
test.py:

import sys

from document_template import DocumentTemplate

__author__ = 'liying'

if sys.version_info < (3, 0):
    reload(sys)
    sys.setdefaultencoding('utf-8')

if __name__ == '__main__':
    id_dict = {"title": "标题", "head": "正文标题", "url": "https://github.com/liying2008", "large_font": "大号字体"}
    id_dict['show_span'] = True

    # Multi-line copy supports string, list and tuple
    # id_dict['contents'] = 'ABCDEFG'
    # id_dict['another_contents'] = '1234567'
    id_dict['contents'] = ('A', 'B', 'C', 'D', 'E', 'F', 'G')
    id_dict['another_contents'] = ['1', '2', '3', '4', '5', '6', '7']
    dt = DocumentTemplate()
    dt.load("test.html", encoding='utf-8')
    dt.set_identifier_dict(id_dict)
    dt.save_document("new_test.html")

指令说明

  • 普通变量:#{var} 定义普通模板变量;

  • bool指令:#{bool:var}text1#{bool:var} 通过变量 var 控制 text1 是否显示;

  • copy指令:#{copy:start}text1#{collection_var}#{copy:end} 循环遍历 collection_var ,将其值填充到内容中。

注意事项

  • 不支持 copy 指令 内使用 copy 指令bool 指令

更新内容

  • copy 指令不再默认换行,取消 linefeed 设置;

  • 取消成对出现的 bool 指令 须在同一行的限制;

  • 取消成对出现的 copy 指令 须在同一行的限制;

  • 支持 bool 指令 内使用 copy 指令

  • 添加了自定义异常。

LICENSE

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

document-template-1.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

document_template-1.0.0-py2.py3-none-any.whl (6.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file document-template-1.0.0.tar.gz.

File metadata

  • Download URL: document-template-1.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/2.7.18

File hashes

Hashes for document-template-1.0.0.tar.gz
Algorithm Hash digest
SHA256 4f4f51bd2dc22f78908b0a0281e51928c6d59a97b10938258c3b63b36afecc3c
MD5 0dd4c6ab376a0605532083d124658959
BLAKE2b-256 a0a4055e51dc692ecbfe3b0aedf058b4ca56dfe05e3997844e379b8203c06674

See more details on using hashes here.

File details

Details for the file document_template-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: document_template-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/2.7.18

File hashes

Hashes for document_template-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 22af32913615dd69a9f8b07ca16f8493ccb6b672a19b7e9d167a8ee33dbc5536
MD5 3b7e9a665f3d758119123cf2bb12e934
BLAKE2b-256 05dcaad51d2e1c910aaba87bcda84a56c566a7bbbf2dcec847401652911f234b

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