document-template
Python解析文档模版
安装方法
使用 pip 安装
pip install document-template
使用方法
- 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>
<h1>网站标题</h1>
<hr>
<span style="font-size: larger;font-weight: bold">#{large_font}</span>
<br>
show_span 为真时显示:
#{bool:show_span}<span>show_span is True</span>#{bool:show_span}
show_span 为假时显示:
#{bool:!show_span}<span>show_span is False</span>#{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}text#{bool:var} 通过变量 var 是否为 True 控制 text 是否显示,或者 #{bool:!var}text#{bool:!var} 通过变量 var 是否为 False 控制 text 是否显示;
copy指令:#{copy:start}text#{collection_var}#{copy:end} 循环遍历 collection_var ,将其值填充到内容中。
注意事项
不支持 copy 指令 内使用 copy 指令 或 bool 指令 。
LICENSE
Metadata
Release files for document-template 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| document-template-1.1.0.tar.gz | 7.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| document_template-1.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 14.4 kB
Release files / document-template-1.1.0.tar.gz
| Download URL | document-template-1.1.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a79dfc958bf3833b7d852ffa302fc7ec4e9b9bf1fc74a273cad2cc4dbffa9c6
|
|
BLAKE2b-256 checksum How to use checksums |
d83356aa6c63a7912b8750f9a0db2a71e4c5b64288046596bbd43b611a4283a4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.0
|
Release files / document_template-1.1.0-py2.py3-none-any.whl
| Download URL | document_template-1.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 6.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
847bb699812903880cf00a80d621467f31e602198daa016fd655ec635e346686
|
|
BLAKE2b-256 checksum How to use checksums |
8a7ec0d50788e2c10c7fe85053bb7269b490a455605880c165ac2e723c63a5c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.0
|
Security for document-template 1.1.0
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.