2

Hi i am using docxtpl to generate the MS Word documents from python using JINJA template engine, I checked this documentation which says we can use special tags for table rows, columns and paragraphs but i am not able to generate table rows dynamically.

First i tried the following way

{% for name in rows %}
{{ name }}
{% endfor %}

The template screenshot

But it adds all items in the same row no new generated.

then i tried the following way as mentioned in the above mentioned documentation.

{%tr for name in rows %}
{{ name }}
{%tr endfor %}

but it generates the following error

Encountered unknown tag 'endfor'.

then i tried the following way, it works but it some how change the generated document margins, formats and styles . all document just mess up visually .

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 preserved
mozway
  • 194,879
  • 13
  • 39
  • 75
Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29

1 Answers1

3

Your second attempt with the %tr tag is correct, but it is possible that it is not in the correct format in your template document.

Try using that for loop in this format in your template: template screenshot

Using that template I generated this output: output

I was able to figure this out based on this issue on the Github repo which pointed me to this test and this test template.

p_sutherland
  • 471
  • 1
  • 11
  • 21