Search code examples
pythonpython-docx

Dynamic Table Rows with docx-template


Good day SO,

I am trying to dynamically populate a Table in a .docx file. The table looks like: enter image description here Where there are N projects (1..N) and each project has N rows (1..N), making the structure like such:

table_data = [
{
    'title':'Proj1',
    'rows':[
        {'col1':'col1txt',
        'col2':'col2txt',
        'col3':'col3txt',
        'col4':'col4txt',
        'col5':'col5txt',
        'col6':'col6txt'},
        {'col1':'col1txt',
        'col2':'col2txt',
        'col3':'col3txt',
        'col4':'col4txt',
        'col5':'col5txt',
        'col6':'col6txt'}
        ]
    },
    {..repeat..}
]

context={'table_data':table_data}

I am currently using the python-docx-template library, and there are very few example online regarding nested tables. Can anyone guide me in the right direction?

Any help would be greatly appreciated.

SOLUTION:

I found the information required for this nested table case at: https://github.com/elapouya/python-docx-template/blob/master/tests/templates/subdoc_tpl.docxhttps://github.com/elapouya/python-docx-template/blob/master/tests/templates/subdoc_tpl.docx

and it's syntax at: https://github.com/elapouya/python-docx-template/blob/master/tests/dynamic_table.py

Hope this helps somebody like me :) Good day!

enter image description here


Solution

  • I found the information required for this nested table case at: https://github.com/elapouya/python-docx-template/blob/master/tests/templates/subdoc_tpl.docxhttps://github.com/elapouya/python-docx-template/blob/master/tests/templates/subdoc_tpl.docx

    and it's syntax at: https://github.com/elapouya/python-docx-template/blob/master/tests/dynamic_table.py