File: nested_for.py

package info (click to toggle)
python-docx-template 0.20.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,404 kB
  • sloc: python: 1,742; makefile: 163
file content (45 lines) | stat: -rw-r--r-- 1,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""
Created : 2016-03-26

@author: Eric Lapouyade
"""

from docxtpl import DocxTemplate

tpl = DocxTemplate("templates/nested_for_tpl.docx")

context = {
    "dishes": [
        {"name": "Pizza", "ingredients": ["bread", "tomato", "ham", "cheese"]},
        {
            "name": "Hamburger",
            "ingredients": ["bread", "chopped steak", "cheese", "sauce"],
        },
        {
            "name": "Apple pie",
            "ingredients": ["flour", "apples", "suggar", "quince jelly"],
        },
    ],
    "authors": [
        {
            "name": "Saint-Exupery",
            "books": [
                {"title": "Le petit prince"},
                {"title": "L'aviateur"},
                {"title": "Vol de nuit"},
            ],
        },
        {
            "name": "Barjavel",
            "books": [
                {"title": "Ravage"},
                {"title": "La nuit des temps"},
                {"title": "Le grand secret"},
            ],
        },
    ],
}

tpl.render(context)
tpl.save("output/nested_for.docx")