使用Jinja在doc表中显示的其他行 [英] Addtional rows showing in doc table using jinja

查看:51
本文介绍了使用Jinja在doc表中显示的其他行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个Jinja代码,我想在其中将环境及其服务器添加到表中.所需的输出:

I have written a jinja code where I want to add the environment and its servers to the table. Desired output:

这是我的Jinja代码:

Here is my Jinja code in word:

我的python代码,其中上下文中的服务器包括环境和属性列表,例如,上下文中的服务器,下面是 ['Qualif',["Tomcat",7,i,.,","","]]] ,其中i是服务器名称

My python code where servers in context include both the environment and the list of attributes, for e.g servers in context, below will be ['Qualif',["Tomcat",7,i," "," "," ",""]]where i is the server name

doc = DocxTemplate("./word_excel_templates/documentation_template.docx")
context = {
    'headers' : ['Component', 'Component Version', 'Server FQDN', 'Application port', 'DB SID', 'DB Port', 'Infos'],
    'servers': []
    }
server_1= ["Tomcat",7,i," "," "," ",""]
#put environment in context as required env
context['servers'].append(environment)
context['servers'].append(server_1)
doc.render(context)        
doc.save("documentation_.docx")

现在这仅仅是代码的一部分,我在上下文中真正的服务器是 ['Qualif',["Tomcat",7,i,.,",",","Dev",["Tomcat",7,i,",","","]] 我得到的输出是这样:

Now this is only part of the code, my true servers in context is ['Qualif',["Tomcat",7,i," "," "," ",""], 'Dev',["Tomcat",7,i," "," "," ",""]] And the output I am getting is this:

对于Jinja来说我还很陌生,所以我不知道为什么我要在表中添加这些额外的单元格.还有另一种方法吗?

I am quite new to Jinja, so I don't know why I am getting these additional cells in the table. Is there another way to do this?

推荐答案

此处实际发生的情况是,如果您的 row is string 条件等于false,则行将被绘制为空.为了避免这种情况,您可以完全更改策略.我假设每个服务器都将具有其环境".标签,如果是这种情况,首先将Word文档的格式更改为:

What is actually happening here is that the rows are being drawn as empty if your row is string condition equals false. In order to avoid that you could change strategy altogether. I am assuuming that every server is going to have its "environment" label, and if that is the case, firstly, change the format of your word document to:

然后,按如下所示调整您的 context 结构:

Then, adjust your context structure as follows:

context = {
'headers' : ['Component', 'Component Version', 'Server FQDN', 'Application port', 'DB SID', 'DB Port', 'Infos'],
'servers':  []
}

server_1 = {}
server_1['environment'] = 'Qualif'
server_1['cols'] = ["Tomcat",7,'a',5000," ",200,""]

server_2 = {}
server_2['environment'] = 'Dev'
server_2['cols'] = ["Tomcat",7,'b',5000," ",200,""]

context['servers'].append(server_1)
context['servers'].append(server_2)

这样,产生的输出将是:

This way, the produced output will be:

如果相反,您希望将多个个服务器整合到一个环境中,则应调整上下文以使其具有类似的结构:

If instead you would like to have multiple servers into one environment, then you should adjust the context to make it have a similar structure:

context = {
'headers' : ['Component', 'Component Version', 'Server FQDN', 'Application port', 'DB SID', 'DB Port', 'Infos'],
'servers':  
         {
           "Qualif": [],
           "Dev" : []
         }
}

server_1 = ["Tomcat",7,'a',5000," ",200,""]
server_2 = ["Tomcat",7,'b',5000," ",200,""]

context['servers']['Qualif'].append(server_1)
context['servers']['Qualif'].append(server_2)
context['servers']['Dev'].append(server_1)
context['servers']['Dev'].append(server_2)

您的word文件如下:

And your word file as follows:

这最终将是您得到的:

这篇关于使用Jinja在doc表中显示的其他行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆