首页 > 编程语言 >[885] How to generate automated tables in Word document with Python

[885] How to generate automated tables in Word document with Python

时间:2023-09-22 12:44:51浏览次数:32  
标签:tables docx Word name 885 template automated table row

ref: How to Generate Automated Word Documents with Python

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


Creating a Template

Before you can proceed, you must first create your very own template document that is basically a normal Microsoft Word Document (.docx) formulated exactly the way you want your automated report to be, down to every nitty-gritty detail such as typefaces, font sizes, formatting, and page structure. The only thing you need to do afterward is to create placeholders for your automated content and declare them with variable names as shown below.

image

As you’ve probably promptly guessed, any automated content can be declared inside a set of double curly brackets {{variable_name}}. This includes text and images. For tables, it is a little more complicated. You need to create a table with a template row with all the columns included, and then you need to append one row above and one row below with the following notation:

First row:

{%tr for item in _variable_name_ %}

Last row:

{%tr endfor %}

Please note that in the figure above the variable names are

  • table_contents for the Python dictionary that will store our tabular data
  • Index for the dictionary keys (first column)
  • Value for the dictionary values (second column)

Once done, save your document in your directory as a .docx file and proceed with writing the code to invoke the template and generate an automated document.

Note: for tables, the variables we should change are _variable_name_, item.Index, item.Value. The word "item" needs to be kept unchanged.


Example:

table-template.docx

python script

doc = DocxTemplate('table-template.docx')

company = {
        "name": "Plumsail",
        "email": "[email protected]"
    }

employees = [
        {
            "name": "Derek Clark",
            "jobTitle": "Marketing director",
            "department": "Marketing Department",
            "office": "Room 18",
            "phone": "(206) 854-9798"
        },
        {
            "name": "Xue Li",
            "jobTitle": "Financial director",
            "department": "Financial Department",
            "office": "Room 19",
            "phone": "(206) 598-1259"
        },
        {
            "name": "Jessica Adams",
            "jobTitle": "Marketing manager",
            "department": "Marketing Department",
            "office": "Room 23",
            "phone": "(206) 789-1598"
        },
        {
            "name": "Katsuko Kawakami",
            "jobTitle": "Analyst",
            "department": "Financial Department",
            "office": "Room 26",
            "phone": "(206) 784-1258"
        }
    ]

context = {}

context['company'] = company
context['employees'] = employees

doc.render(context)
doc.save('table-template_rendered.docx')

table-template_rendered.docx

标签:tables,docx,Word,name,885,template,automated,table,row
From: https://www.cnblogs.com/alex-bn-lee/p/17722059.html

相关文章

  • [884] How to generate automated Word documents by Python
    ref:python-docxref:HowtoGenerateAutomatedWordDocumentswithPythonref:AutomatingWordDocumentsfromExcelUsingPython|‘docxtpl’Tutorialref:docxtpl快速上手使用,数据填入以及循环写入表格ref:探究Python中的文档自动化工具——docxtplref:Python......
  • 完美解决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......
  • Linux中iptables设置详细
    无论如何,iptables是一个需要特别谨慎设置的东西,万一服务器不在你身边,而你贸然设置导致无法SSH,那就等着被老板骂吧,呵呵。。。一下内容是为了防止这种情况发生而写的,当然很初级,不过一般服务器也够用了:1.首先介绍一下指令和相关配置文件1.启动指令:serviceiptablesstart2.重......
  • iptables使用示例
    iptable的各种targetiptables的结构:iptables由上而下,由Tables,Chains,Rules组成。一、iptables的表tables与链chainsiptables有Filter,NAT,Mangle,Raw四种内建表:1.Filter表Filter是iptables的默认表,它有以下三种内建链(chains):INPUT链 –处理来自外部的数据。OUTPUT链......
  • 解决此问题:07版WORD文档中的超链接、页码等变成了{HYPERLINK"网址"}、{PAGE}/{NUMPAGE
    ......