简体   繁体   English

使用 python-docx 填充单词表

[英]Populate word table using python-docx

I have a table to populate table to populate , I am quite new to python-docx.我有一个表格来填充表格来填充,我对 python-docx 很陌生。 I have tried to populate it using render but it's only giving as output one server:我尝试使用渲染填充它,但它只提供 output 一台服务器:

for i in serverJson:
        doc.render(i)

where serverJson is the list of servers entered by the user.其中 serverJson 是用户输入的服务器列表。 for eg:例如:

    for i in appserver: 
        server_1={"component":"Tomcat","comp_version":"7","server":i,
                                         "app_port":"5000","db_sid":" ","db_port":"200"}
    
        server_2={"component":"Apache","comp_version": "2.4","server":i,
                                    "app_port":" ","db_sid":" ","db_port":"200"}
        serverJson.append(server_1)
        serverJson.append(server_2)

My question is how do i populate the table shown in the link with the number of servers entered by user?我的问题是如何使用用户输入的服务器数量填充链接中显示的表格?

So, what you are actually doing with this block of code:所以,你实际上在用这个代码块做什么:

for i in serverJson:
    doc.render(i)

Is to render the same doc multiple times, but only using the single variables you provided.是多次渲染同一个文档,但只使用您提供的单个变量。 Instead, you need to provide a jinja for statement inside the block itself, to allow it to dynamically create rows and columns.相反,您需要在块本身内提供一个 jinja for语句,以允许它动态创建行和列。 You will have to operate on both your docx file and on the Python code.您将必须对您的docx文件和 Python 代码进行操作。 Firstly, create a table and make your docx file look like:首先,创建一个表并使您的docx文件如下所示:

在此处输入图像描述

Above, we are using some jinja2 for loops to achieve the following:上面,我们使用一些 jinja2 for loops来实现以下功能:

  • Generate as many columns in the headers as we need根据需要在标题中生成尽可能多的列
  • Generate as many rows as the servers in the list生成与列表中的服务器一样多的行
  • Generate as many columns containing the data of the server in question生成包含相关服务器数据的尽可能多的列

In order to populate the above template with the correct context, take a look at the below code:为了使用正确的上下文填充上述模板,请查看以下代码:

from docxtpl import DocxTemplate
import os,sys

#Just change these according to your needs
inputFileName = "i.docx"
outputFileName = "o.docx"

#This is done to obtain the absolute paths to the input and output documents,
#because it is more reliable than using the relative path
basedir = os.path.dirname(sys.argv[0])
path = os.path.join(basedir, "", inputFileName)
outpath = os.path.join(basedir, "", outputFileName)

template = DocxTemplate(path)

#Specify all your headers in the headers column
context = {
'headers' : ['Component', 'Component Version', 'Server FQDN', 'Application port', 'DB SID', 'DB Port', 'Infos'],
'servers': []
}

#Fictious appserver list
appserver = ['a','b']

#Add data to servers 1 and 2 using a list and not a dict, remember to add
#an empty string for the Infos, as well, otherwise the border won't be drawn
for i in appserver: 
    server_1= ["Tomcat",7,i,5000," ",200,""]
    server_2= ["Apache",2.4,i," "," ",200,""]
    context['servers'].append(server_1)
    context['servers'].append(server_2)

template.render(context)
template.save(outpath)

The above, will produce o.docx which will look like:以上,将生成o.docx ,如下所示:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM