elapouya / python-docx-template

Use a docx as a jinja2 template
GNU Lesser General Public License v2.1
1.98k stars 385 forks source link

DocumentPart object has no attribute '_rels'. #541

Open theodore86 opened 5 months ago

theodore86 commented 5 months ago

Describe the bug

A clear and concise description of what the bug is.

It seems like python-docx has released an new version:

python-docx==1.1.1
docxtpl==0.16.8
uri = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/header'
    def get_headers_footers(self, uri):
>       for relKey, val in self.docx._part._rels.items():
E       AttributeError: 'DocumentPart' object has no attribute '_rels'. Did you mean: 'rels'?
lcpprop commented 5 months ago

I have the same issue. Everything was working the day before python-docx 1.1.1 release and not it gets this error. As soon as I specified that I want previous version of python-docx 1.1.0, everything started working again. Here is my traceback:


AttributeError Traceback (most recent call last) File , line 90 89 template_vars {"name":"John", "lastname":"Oliver"} ---> 90 doc.render(template_vars) 92 doc.save("".docx")

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:382, in DocxTemplate.render(self, context, jinja_env, autoescape) 379 # Headers 380 headers = self.build_headers_footers_xml(context, self.HEADER_URI, 381 jinja_env) --> 382 for relKey, xml in headers: 383 self.map_headers_footers_xml(relKey, xml) 385 # Footers

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:338, in DocxTemplate.build_headers_footers_xml(self, context, uri, jinja_env) 337 def build_headers_footers_xml(self, context, uri, jinja_env=None): --> 338 for relKey, part in self.get_headers_footers(uri): 339 xml = self.get_part_xml(part) 340 encoding = self.get_headers_footers_encoding(xml)

File /local_disk0/.ephemeral_nfs/envs/.../lib/python3.10/site-packages/docxtpl/template.py:324, in DocxTemplate.get_headers_footers(self, uri) 323 def get_headers_footers(self, uri): --> 324 for relKey, val in self.docx._part._rels.items(): 325 if (val.reltype == uri) and (val.target_part.blob): 326 yield relKey, val.target_part

AttributeError: 'DocumentPart' object has no attribute '_rels'

msp-corbett commented 5 months ago

I've hit the same issue and rolled back to python-docx 1.1.0 as a fix.

joao-gabrielC commented 5 months ago

I've hit the same issue and rolled back to python-docx 1.1.0 as a fix.

Rolling back also fixed the issue for me.

elapouya commented 5 months ago

I am going to investigate about what's new in that python-docx version...

elapouya commented 5 months ago

I created the new docxtpl 0.17.0 : it requires python-docx 1.1.1+

Please, could you test ?

lcpprop commented 5 months ago

Yes, everything works perfectly for me. Thank you so much! How about the others?

msp-corbett commented 5 months ago

Yes, working with docxtpl 0.17.0 and python-docx 1.1.2. Thanks!

theodore86 commented 5 months ago

Thank, i have already locked the dependencies so it is not an major issue, it works.