[Python] Generate table rows in Word document

Discussão em 'Python' iniciado por Stack, Junho 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Word document template with a table. I want to generate the table rows from my python structure like below.

    from docxtpl import DocxTemplate

    doc = DocxTemplate("template2.docx")

    context = {
    "topic": "Volcanoes",
    "lessons": [
    {"title": "Introduction to Volcanoes", "objective": "Understand basic concepts of volcanoes"},
    {"title": "Eruption Types", "objective": "Differentiate between types of volcanic eruptions"},
    {"title": "Volcano Case Studies", "objective": "Analyze real-world examples of eruptions"},
    {"title": "Lava and Ash", "objective": "Understand the geological impact of lava and ash"},
    ]
    }

    doc.render(context)

    doc.save("filled.docx")


    My Word document template:

    [​IMG]

    When I run the code python3 gen.py I get the following output Word document:

    [​IMG]

    As you can see, it did generate the rows, but the problem is the set of tables/cells it has inserted at the bottom. How can I avoid creating those extra cells/tables?

    Continue reading...

Compartilhe esta Página