1

I have the following dictionary of dictionaries:

{
    "col_labels": [
        "Naam",
        "Discipline",
        "Rol",
        "Bedrijf"
    ],
    "project_disciplines_final": [
        {
            "dict_1": [
                "Marcel",
                "discipline_eof",
                "projectfunctie_1",
                "discipline_instantie_2"
            ]
        },
        {
            "dict_2": [
                "Hamed",
                "discipline_eof",
                "projectfunctie_2",
                "discipline_instantie_2"
            ]
        },
        {
            "dict_3": [
                "Robin",
                "discipline_eof",
                "projectfunctie_6",
                "discipline_instantie_1"
            ]
        },
        {
            "dict_4": [
                "Gerritsen",
                "discipline_eof",
                "projectfunctie_2",
                "discipline_instantie_1"
            ]
        }
    ]
}

I would like to create a Word table using the docxtpl and jinja2 Python packages, but I cannot figure out the syntax to use in the Word file.

I have the following:

Word table syntax

And I want this table in my Word file:

Desired Word table

It is important that the table is dynamic because the number of rows varies and is dependent on the length of the provided dictionary 'project_discplines_final'.

Does anyone know what is wrong with my syntax in Word (first image)? Many thanks in advance!

Luc
  • 35
  • 5

1 Answers1

1

I changed the structure of the dictionary so that it's easier to loop in the template. Instead of having arrays of dictionaries, you can just have a dictionary, where each key is just an entry and its value is the array.

Here's the template and code.

enter image description here

ctx = {
    "col_labels": ["Naam", "Discipline","Rol", "Bedrijf"],
    "project_disciplines_final": {
        "dict_1": [
            "Marcel",
            "discipline_eof",
            "projectfunctie_1",
            "discipline_instantie_2"
        ],
        "dict_2": [
            "Hamed",
            "discipline_eof",
            "projectfunctie_2",
            "discipline_instantie_2"
        ],
        "dict_3": [
            "Robin",
            "discipline_eof",
            "projectfunctie_6",
            "discipline_instantie_1"
        ],
        "dict_4": [
            "Gerritsen",
            "discipline_eof",
            "projectfunctie_2",
            "discipline_instantie_1"
        ]
    }
}

doc.render(ctx)
benkyou
  • 61
  • 5