Skip to main content

batch docx generation

Project description

Letterpress

Letterpress取意于活字印刷,是帮需要批量生成Word的朋友写的一个小工具。

每一套信息需要生成大量文件,Word自带的邮件生成也帮不了他。

Installation

python -m pip install letterpress

Usage

假设你有一个Word文件,起名为template.docx,其中的内容是这样的:

标题:{{ topic }}
正文: {{ content }}

这些内容可以在正文,可以在表格,甚至可以在页眉页脚或者嵌套的表格中。

当然,也可以在页眉页脚嵌套的表格中。

这些内容也可以是任何格式,任何大小,甚至大括号与变量名都不需要统一格式。

唯一需要注意的是,需要用两个大括号和一个空格包围你的变量名。

然后运行下面的Python代码:

from letterpress import TemplateDocument

vm = {
    'topic': 'my-topic',
    'content': 'my-content',
}
template = TemplateDocument('template.docx', vm.keys())
template.parse_document()
template.replace_and_save(vm, 'outcome.docx')

vm = {
    'topic': 'my-another-topic',
    'content': 'my-another-content',
}
template.replace_and_save(vm, 'another-outcome.docx')

就根据给定的内容生成了两个Word文件。

FAQ

Q: Word文件无法读取,显示检测到不是Word文件。 A: python-docx读取的方式不适用于老版本Word,升级到docx就好了。

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

letterpress-0.0.1.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

letterpress-0.0.1-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page