Span columns with docxTemplate python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Span columns with docxTemplate python (/thread-18713.html) |
Span columns with docxTemplate python - jimmeh - May-28-2019 I'm having trouble trying to fill in a table row by row using DocxTemplate. I'm only confused on how the template should look like. This is my python code: from docxtpl import Docxtemplate doc = DocxTemplate("Template.docx") context = {} table_info = [{start: "A", end: "C"}, {start: "B", end: "C"}, {start:"F", end:"L"}, {start:"B", end:"R"}] context["table_info"] = table_info doc.render(context) doc.save("Finish.docx")I want the final result to be in a table like this: | start | end | | A | C | | B | C | | F | L | | B | R |Would anyone be willing to show me how I can accomplish this? I saw this example: https://github.com/elapouya/python-docx-template/blob/master/tests/templates/dynamic_table_tpl.docx but I wasn't able to wrap my head around what I'm trying to do. Thanks in advance! RE: Span columns with docxTemplate python - heiner55 - May-28-2019 I think you want to use python-docx (not python-docx-template), see here: https://python-docx.readthedocs.io/en/latest/ There is a sample which create tables. RE: Span columns with docxTemplate python - heiner55 - May-28-2019 #!/usr/bin/python3 from docx import Document from docx.shared import Inches document = Document() records = ( ('A', 'C'), ('B', 'C'), ) table = document.add_table(rows=1, cols=2) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'start' hdr_cells[1].text = 'end' for c0, c1 in records: row_cells = table.add_row().cells row_cells[0].text = c0 row_cells[1].text = c1 document.save('demo.docx') RE: Span columns with docxTemplate python - jimmeh - May-29-2019 The problem with this is it manually adds its own table into a new docx file. Is there a way to do this except have it insert into a table that already exists in a docx file. RE: Span columns with docxTemplate python - heiner55 - May-29-2019 Ok, then you need python-docx-template. As I understand python-docx-template, you have to made first a template yourself with a table already. And then you can change or add data to the table with python. |