Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested for in table #547

Open
jodyphelan opened this issue Jun 13, 2024 · 4 comments
Open

Nested for in table #547

jodyphelan opened this issue Jun 13, 2024 · 4 comments

Comments

@jodyphelan
Copy link

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')

@colorthoro
Copy link

colorthoro commented Aug 1, 2024

I have the same question. It seems like nested tr loops are not supported yet. Will this be added to the plan? It would be very useful. @elapouya

@dukre
Copy link

dukre commented Aug 20, 2024

Hi, this template produces the desired output:

nested_tr

@jodyphelan
Copy link
Author

Amazing thanks @dukre

@jodyphelan jodyphelan reopened this Oct 7, 2024
@jodyphelan
Copy link
Author

@dukre thanks for your answer. I thought I could expand your solution to an extra inner loop but it didn't seem to give the desired result. Any suggestions would be appreciated!

The desired table would look like:
image

The current template looks like this:
image

But this produces the following:
image

Here is the code:

from docxtpl import DocxTemplate

tpl = DocxTemplate('template.docx')

context = {
    'data': [
        {
            'type': 'Fruit', 'items': [
                {
                    'name':'Apple', 
                    'colours':[
                      'green', 
                      'red'
                    ]},
                {
                    'name':'Banana', 
                    'colours':[
                        'yellow'
                    ]
                }
            ]
        },
        {
            'type': 'Vegetable', 'items': [
                {
                    'name':'Carrot', 
                    'colours':[
                        'orange'
                    ]
                },
                {
                    'name':'Onion', 
                    'colours':[
                        'white', 
                        'red'
                    ]
                },
            ]
        }
    ]
}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants