python-docx-template icon indicating copy to clipboard operation
python-docx-template copied to clipboard

Tables: Multiple loops in the same row

Open marcus-hao opened this issue 1 year ago • 1 comments

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 template

Expected Expected

Output Output

marcus-hao avatar Aug 16 '23 11:08 marcus-hao

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

SergeyZaykov avatar Nov 18 '23 22:11 SergeyZaykov