alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[884] How to generate automated Word documents by Python

ref: python-docx

ref: How to Generate Automated Word Documents with Python

ref: Automating Word Documents from Excel Using Python | ‘docxtpl’ Tutorial

ref: docxtpl快速上手使用,数据填入以及循环写入表格

ref: 探究Python中的文档自动化工具——docxtpl

ref: Python 第三方库之 docxtpl (处理word文档)

ref: docxtpl user manual


To install using pip:

pip install docxtpl

Usage:

from docxtpl import DocxTemplate
doc = DocxTemplate("my_word_template.docx")
context = {'company_name': "World company"}
doc.render(context)
doc.save("generated_doc.docx")

Example:

"test.docx"

Hello, my name is {{name}}.
I am {{age}} years old now.
I like playing {{sport_01}}, {{sport_02}}, and {{sport_03}}.

python script

doc = DocxTemplate('test.docx')
context = {'name': 'Alex',
'age': 24,
'sport_01': 'badminton',
'sport_02': 'football',
'sport_03': 'basketball'}
doc.render(context)
doc.save('test_rendered.docx')

"test_rendered.docx"

Hello, my name is Alex.
I am 24 years old now.
I like playing badminton, football, and basketball.

This implementation can be found in Automating Word Documents from Excel Using Python | ‘docxtpl’ Tutorial.

About the generation of tables, please check my next blog. Cheers. 

posted on   McDelfino  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-09-22 【480】Keras实操
点击右上角即可分享
微信分享提示