python——word模板中填值

word模板填值

1、手动做好模板——my_test.docx的内容:

我的名字叫做{{ name }}。

2、python代码:

# test_fill_word.py
# python -m pip install docxtpl

from docxtpl import DocxTemplate

tpl = DocxTemplate('my_test.docx')

context = {
   'name': 'lovecjy'
}

tpl.render(context)
tpl.save('my_test1.docx')

运行上述代码,会在脚本所在目录生成填好数据word——my_test1.docx。

以上。

原文地址:https://www.cnblogs.com/lovebkj/p/13730536.html