zhangyu836 / xltpl

A python module to generate xls/x files from a xls/x template.
MIT License
71 stars 17 forks source link

Column not written when nested list is empty #16

Closed fbarre96 closed 1 year ago

fbarre96 commented 1 year ago

I tried to use nested loop to write data in my sheet and a column is missing in the final excel output if a the nested list is empty. Here is the code I tried with the template template.xlsx :

from xlsxtpl.writerx import BookWriter

writer = BookWriter('template.xlsx')
writer.jinja_env.globals.update(dir=dir, getattr=getattr)
# Define the data that you want to fill into the template
data = {
    'persons':[
        {
            'name': 'John',
            'children':[
            ],
            'age': 28
        },
        {
            'name': 'Peter',
            'children':[
                {'name':'Ana'},

                {'name':'Eve'}
            ],
            'age': 49
        }

    ],

}

writer.render_book(payloads=[data])
# Create a new workbook
writer.save("out.xlsx")

This is what was generated : out.xlsx.

the expected output would be an empty B2 cell and the data "28" in the C2 cell instead of the B2 cell.

zhangyu836 commented 1 year ago

This is expected behavior. Try this template.xlsx

fbarre96 commented 1 year ago

This solved my issue thanks