elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.92k stars 378 forks source link

Nested for in table #547

Open jodyphelan opened 3 weeks ago

jodyphelan commented 3 weeks ago

Describe your problem

I would like to create a table like the example image below. I tried to use a nested for loop like

image

However this seems to ignore the initial loop and gives this:

image

Is there a way of using nested loops?

More details about your problem

Here is the code used to render the template:

from docxtpl import DocxTemplate

tpl = DocxTemplate('template.docx')

context = {
    'data': [
        {'type': 'Fruit', 'names': ['Apple', 'Banana']},
        {'type': 'Vegetable', 'names': ['Carrot', 'Onion']},
    ]
}

tpl.render(context)
tpl.save('output/result.docx')