This script uses changing colspan attribute for generation of table with dynamic column count depending on input data.
16 lines
502 B
Python
16 lines
502 B
Python
from docxtpl import DocxTemplate
|
|
|
|
tpl=DocxTemplate('test_files/dynamic_table_tpl.docx')
|
|
|
|
context = {
|
|
'col_labels' : ['fruit', 'vegetable', 'stone', 'thing'],
|
|
'tbl_contents': [
|
|
{'label': 'yellow', 'cols': ['banana', 'capsicum', 'pyrite', 'taxi']},
|
|
{'label': 'red', 'cols': ['apple', 'tomato', 'cinnabar', 'doubledecker']},
|
|
{'label': 'green', 'cols': ['guava', 'cucumber', 'aventurine', 'card']},
|
|
]
|
|
}
|
|
|
|
tpl.render(context)
|
|
tpl.save('test_files/dynamic_table.docx')
|