elapouya/python-docx-template

Unable to access sub values in for loop

andyman212 opened this issue · 0 comments

Please can someone help me where I am going wrong, I am unable to access sub values using "." nothing is generating in the word ty

Thank you.

python-docx==1.1.0
Jinja2==3.1.3
Python 3.10.7

`from pathlib import Path
from docxtpl import DocxTemplate

data = {"devices":
[{"192.168.50.135":
{"config":
{"snmp":
[{"server": "VM-SNMP-01","ipaddress": "10.10.10.1","community": "public","version": "v2c"},
{"server": "VM-SNMP-02","ipaddress": "10.10.10.2","community": "public","version": "v3"}]}}},
{"192.168.50.136":
{"config":
{"snmp":
[{"server": "VM-SNMP-01","ipaddress": "10.10.10.1","community": "public","version": "v2c"},
{"server": "VM-SNMP-02","ipaddress": "10.10.10.2","community": "public","version": "v3"}]}}}]}

def create_snmp_table(data):
device_info = []
for device in data["devices"]:
for key, value in device.items():
for server in value["config"]["snmp"]:
device_info.append({
"device": key,
"server": server["server"],
"ipaddress": server["ipaddress"],
"community": server["community"],
"version": server["version"],
})

return device_info

snmptable = create_snmp_table(data)

structured_data = {"root": [snmptable]}

doc = DocxTemplate(f"{Path(file).parent.parent}/Test Template.docx")
doc.render(structured_data)
doc.save(f"{Path(file).parent}/Test Document.docx")`

image