嗨,我正在使用docxtpl来使用JINJA模板引擎从python生成MS Word文档,我检查了这文档,它说我们可以对表行、列和段落使用特殊的标记,但是我不能动态地生成表行。
,首先,我尝试了以下方法,
{% for name in rows %}
{{ name }}
{% endfor %}

但是它会添加同一行中的所有项,而不是新生成的项。
,然后我尝试了如下方法,如上面提到的文档中所提到的。
{%tr for name in rows %}
{{ name }}
{%tr endfor %}但是它会产生以下错误
遇到未知标签'endfor‘。
--然后我尝试了以下方法,它可以工作,但是它会改变生成的文档边距、格式和样式。所有的文档都在视觉上搞砸了.
row = self.document.tables[3].add_row().cells # add row
row[0].text = '' #add empty text to create paragraph
row[0].paragraphs[0].add_run('Some value') #use run to add value
row[0].paragraphs[0].style = self.document.tables[3].row_cells(3)[1].paragraphs[0].style
#this line copy the style of previous row cell to the current row cell else styles are not preservedhttps://stackoverflow.com/questions/71679088
复制相似问题