Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docxtpl special tr tag {%tr %} is not working

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
like image 699
Zain Ul Abidin Avatar asked Sep 05 '25 03:09

Zain Ul Abidin


1 Answers

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.

like image 82
p_sutherland Avatar answered Sep 07 '25 19:09

p_sutherland