2

Well, I continue to use the python-docx-template.

I was making the template for some tables, specifically this one:

Template that I made

The result table that I'm looking for is this (leaving aside the date/fecha):

Result that I want

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.

Separe tables and get good results

Is good work with separate tables but the point is that this replacement works on the table from the start of the query.

1 Answer 1

0

Look at last line, it's obviously:

jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'cols'

This is reason of error.

  1. Look at Word code: cols is attribute of element of lab_results.

  2. lab_results defines as value of variable named resultados.

  3. But I couldn't see any definition of variable named resultados. It looks like you lost your main variable! It happens.

Well, could you look out this error by yourself? Sure! The good practice is check code by flake8 tool.

I check your code for you and flake8 confirms my suggestion:

sp.py:46:20: F821 undefined name 'resultados'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.