python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.38k stars 1.08k forks source link

Adding section creating an error in the xml data structure #343

Open Christian-1980 opened 7 years ago

Christian-1980 commented 7 years ago

Hi - having issues with adding sections in python-docx 0.8.6:

Gerneral module to write the documentation

document = Document('c:\Users\z003n2zv\Desktop\Skunk_Works\Templates\ZPS_Templates\default_ZPS.docx')

Section No. 1

section1 = document.add_section()

document.add_heading('Scope of delivery', level=1) document.add_paragraph('',style = 'Normal')

document.save('ZPS__Test.docx')

It writes a document as it should. But if I open that one again an error message occur:

image

This error only occurs when I add sections...

Thanks for your help :)

Cheers

scanny commented 7 years ago

I'll need to see the file(s) before and after to do any diagnosis.

What happens if you don't add a heading or paragraph?

It's odd that the problem is in a header part, since you wouldn't have touched that. Are you by chance using a fork? I know there was some work done on header/footer and that might be out there on a branch somewhere. Best to start with a fresh install of python-docx from PyPI just to make sure.

Christian-1980 commented 7 years ago

Hi thanks for quick response! If I don't add any paragraph or heading the same error message. If I leave out to add a sections everythings OK.

Enclosed the word file I want to process with the code above.

default_ZPS.docx

Thanks again!

scanny commented 7 years ago

Can you also send the "after" file that has the load error?

Christian-1980 commented 7 years ago

ZPS__Test.docx

Here it is :)

scanny commented 7 years ago

Hmm, interesting. It looks like Word doesn't like relationships (r:id keys) being used more than once in the same file. That'd definitely something they don't mention in the spec :)

I'm afraid it's not an easy fix, unfortunately.

The only workarounds I can think of off the top of my head is to not use headers/footers in the template document.

If you have resources to do (or sponsor) some development we can pursue a more robust solution. The essential problem is that when you create a new section, it "clones" the <w:sectPr> element of the prior section. If there are headers and/or footers, those are relationships to other "parts" (separate XML files in the zip, like header1.xml). Those will need to have new relationships formed (still pointing to the same header part), or possibly just removed and therefore "inherited" by default. The details will take a little more research and experimentation.

Christian-1980 commented 7 years ago

Hi Scanny - Ok understood! I have checked & it was possible to modify your template accordingly to get the same format as desired. With this modified template everything is working fine! So at least we know this can occur but there is a work-around! Thanks so far & keep the stone rolling, Christian