Well, I continue to use the python-docx-template.
I was making the template for some tables, specifically this one:
The result table that I'm looking for is this (leaving aside the date/fecha):
The original idea was the table will receive data from lab results but the legislation may vary (the results can be compared by resolutions or a decree), I made some lists with dictionaries to populate the table and put the information in the table but throws an error, I was researching and everything seems that in this way the table nests the loops and generates an error when trying to look for 'cols' in legis list.
This is the code, which is more list than anything:
from docxtpl import DocxTemplate
# Cargar plantilla
doc = DocxTemplate('template/prueba.docx')
# Parametros
puntos = ['EPO-ARD-DO1: ENTRADA PLANTA DE TRATAMIENTO ARD', 'EPO-ARD-DO2: SALIDA PLANTA DE TRATAMIENTO ARD']
codSample = ['MCS 17466', 'MCS 17465']
... Many lists ...
# Sample list
norma2 = 'law name'
legis3 = [
{'art 2.2.3.3.9.14' : 'N.E.', 'art 2.2.3.3.9.16' : 'N.E.', 'art 2.2.3.3.9.17' : 'N.E.', 'art 2.2.3.3.9.18' : 'N.E.'},
{'art 2.2.3.3.9.14' : 'N.E.', 'art 2.2.3.3.9.16' : 'N.E.', 'art 2.2.3.3.9.17' : 'N.E.', 'art 2.2.3.3.9.18' : 'N.E.'},
{'art 2.2.3.3.9.14' : 'N.E.', 'art 2.2.3.3.9.16' : 'N.E.', 'art 2.2.3.3.9.17' : 'N.E.', 'art 2.2.3.3.9.18' : 'N.E.'},
... more things ...
]
# extract articles of law
art_limi = legis3[0].keys()
# Context
context = {
'puntos' : puntos,
'cdSam' : codSample,
'lab_results' : resultados,
'norma' : norma2,
'articulos' : art_limi,
'legis' : legis3
}
doc.render(context)
# Guardar documento
doc.save('output/salidaPrueba2.docx')
And that's the error that I get:
File "c:\xampp\htdocs\pythonDocx\prueba.py", line 132, in <module>
doc.render(context)
File "C:\Python\Python311\Lib\site-packages\docxtpl\template.py", line 368, in render
xml_src = self.build_xml(context, jinja_env)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\docxtpl\template.py", line 315, in build_xml
xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\docxtpl\template.py", line 245, in render_xml_part
raise exc
File "C:\Python\Python311\Lib\site-packages\docxtpl\template.py", line 239, in render_xml_part
dst_xml = template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\Python311\Lib\site-packages\jinja2\environment.py", line 1301, in render
self.environment.handle_exception()
File "C:\Python\Python311\Lib\site-packages\jinja2\environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 11, in top-level template code
File "C:\Python\Python311\Lib\site-packages\jinja2\environment.py", line 485, in getattr
return getattr(obj, attribute)
^^^^^^^^^^^^^^^^^^^^^^^
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'cols'
I was thinking of passing some data as permissible limits to the result list, but I don't know how to fit the header well.
I'm kind of stuck right now and would appreciate some help getting this table moving forward.
Thanks in advance.
Well, when I tried to separate the tables I get the correct render, I separated the tables to watch if just one table have an issue or what happened, in that example, I'll change the law of comparison and obtained good results in the replacement.
Is good work with separate tables but the point is that this replacement works on the table from the start of the query.