首页 > 编程语言 >[884] How to generate automated Word documents by Python

[884] How to generate automated Word documents by Python

时间:2023-09-22 12:22:23浏览次数:38  
标签:docx documents Word 884 Python doc docxtpl sport ref

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. 

标签:docx,documents,Word,884,Python,doc,docxtpl,sport,ref
From: https://www.cnblogs.com/alex-bn-lee/p/17722036.html

相关文章

  • 完美解决TypeError: ‘encoding’ is an invalid keyword argument for this function
    完美解决TypeError:‘encoding’isaninvalidkeywordargumentforthisfunction文章目录报错问题解决方法声明报错问题之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。问题描述如下:TypeError:‘encoding’isaninvalid......
  • 如何实现web在线编辑微软office(xls,word)?
    很多人知道,在web浏览器中是无法直接预览Excel、world文档等文件的,让开发者很头疼。谷歌Chrome经过开发团队不断优化,凭借运行界面简单,打开速度最快及扩展插件众多,Chrome已经成为了世界上最受欢迎的浏览器。不过有一点非常可惜,由于微软Office不是开源程序,所以Chrome一直无法直接打......
  • 【WPF】PasswordBox汇总
    一、回车事件写法1:绑定:TextPassWord.KeyDown+=TextPassWord_KeyDown;privatevoidTextPassWord_KeyDown(objectsender,KeyEventArgse){if(e.Key==Key.Enter){TextErr.Text=null;......
  • 丢失root密码,且give root password for maintenance的解决方法
    前天遇到开机进入maintenance模式,且不知道root密码,经过多次尝试后终于重置了root密码,将系统恢复。现象1、找不到root密码;2、无法进入系统,提示:giverootpasswordformaintenance3、无法进入单用户模式解决过程那应该怎么做才能修改root密码呢?----方法是在原来添加single的地方添加......
  • PHP获取Word文档 docx文件内容
    1.下载库composerrequirephpoffice/phpword2.写代码2.1引入点击查看代码usePhpOffice\PhpWord\IOFactory;usePhpOffice\PhpWord\Element\TextRun;usePhpOffice\PhpWord\Element\Text;usePhpOffice\PhpWord\Element\Paragraph;2.2点击查看代码/......
  • 转jacob操作word和excel
    jacob简介参考资料:jacob的官方文档:http://danadler.com/jacob/微软的javasdk文档:http://f2.grp.yahoofs.com/v1/IIwuQ7rQyGCn-b-f4biL9n_xAwI_Oh3ieuWVDkLfPpaKBmRUjeUzAtCy5oljSMgzdV6OZMRRgrEwU1_RtIXS-h0h7O9W4RB7aUs48A/sdkdocs.zip.aahttp://f1.grp.yahoofs.com/v1/IIwuQzpwMr......
  • 解决此问题:07版WORD文档中的超链接、页码等变成了{HYPERLINK"网址"}、{PAGE}/{NUMPAGE
    ......
  • destoon上做纯js实现html指定页面导出word
    因为最近做了范文网站需要,所以要下载为word文档,如果php进行处理,很吃后台服务器,所以想用前端进行实现。查询github发现,确实有这方面的插件。js导出word文档所需要的两个插件:FileSaver.jsjquery.wordexport.js首先引入:<!--生成word!--><scriptsrc="https://cdn.bootcss......
  • 【Sword系列】第七届全国残疾人职业技能大赛样题-网络安全-这个厉害了
    (文章目录)前言把明文转换为16进制编码的步骤如下:将明文中的每个字符转换为其对应的ASCII码。将ASCII码转换为16进制编码。例如,如果明文为"Hello",则转换为16进制编码后为"48656C6C6F"。将16进制编码转换为10进制数的步骤如下:将16进制编码每两个字母分组。将每个字母转换......
  • 基于Linux+宝塔面板+LNMP+Wordpress搭建的网站主页的网站漏洞修补的解决方案 (1)
    基于Linux+宝塔面板+LNMP+Wordpress搭建的网站主页1.网站漏洞修补的解决方案@目录基于Linux+宝塔面板+LNMP+Wordpress搭建的网站主页前言为什么要做网站漏洞扫描降低资产所面临的风险满足法律合规要求满足业界安全最佳实践及认证需求其他参考信息等级保护网络安全法安全建议漏......