python-docx-template
python-docx-template copied to clipboard
Tables: Multiple loops in the same row
I am trying to query data for multiple columns using for loops for each column(they are all on the same row). I think something is wrong with the logic of my template, because the data in the last column will overwrite previous columns.
The code:
from docxtpl import DocxTemplate
context = {
"previous_previous_year" : {
"p1_events" : [
{
"classification" : "Event A",
"count" : 4
},
{
"classification" : "Event B",
"count" : 1
}
],
},
"previous_year" : {
"p1_events" : [
{
"classification" : "Event A",
"count" : 1
},
{
"classification" : "Event B",
"count" : 1
}
],
},
"current_year" : {
"p1_events" : [
{
"classification" : "Event A",
"count" : 1
},
{
"classification" : "Event B",
"count" : 5
}
],
},
}
doc = DocxTemplate("template.docx")
doc.render(context)
doc.save("results.docx")
Screenshots
Template
Expected
Output
You should use the for loop in one column only but loop variable 'event' in all columns. Open any table example like https://github.com/elapouya/python-docx-template/blob/master/tests/templates/dynamic_table_tpl.docx