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 %}
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
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:
Using that template I generated this 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With