Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no relationship of type ... in collection error #204

Closed
cristian-codorean opened this issue Sep 3, 2015 · 6 comments
Closed

no relationship of type ... in collection error #204

cristian-codorean opened this issue Sep 3, 2015 · 6 comments

Comments

@cristian-codorean
Copy link

Hi, I was trying python-docx with a .docx file which I got from a .doc file 'Saved as' a Strict Open XML Document with Microsoft Office Standard 2013. Unfortunately, when I'm trying to open the .docx file with python-docx, I get the error below. Does anybody have any idea what the problem is ?

Traceback (most recent call last):
  File "fillin_subrep_contract.py", line 39, in <module>
    fillin_contract_docx(sys.argv[1])
  File "fillin_subrep_contract.py", line 20, in fillin_contract_docx
    document = Document(wordfile)
  File "/var/tmp/mow/git/virtualenvs/webdev/lib/python2.7/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/var/tmp/mow/git/virtualenvs/webdev/lib/python2.7/site-packages/docx/opc/package.py", line 108, in main_document_part
    return self.part_related_by(RT.OFFICE_DOCUMENT)
  File "/var/tmp/mow/git/virtualenvs/webdev/lib/python2.7/site-packages/docx/opc/package.py", line 127, in part_related_by
    return self.rels.part_with_reltype(reltype)
  File "/var/tmp/mow/git/virtualenvs/webdev/lib/python2.7/site-packages/docx/opc/rel.py", line 63, in part_with_reltype
    rel = self._get_rel_of_type(reltype)
  File "/var/tmp/mow/git/virtualenvs/webdev/lib/python2.7/site-packages/docx/opc/rel.py", line 116, in _get_rel_of_type
    raise KeyError(tmpl % reltype)
KeyError: u"no relationship of type 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' in collection"
@scanny
Copy link
Contributor

scanny commented Sep 3, 2015

I expect you'll see a difference in the .rels part of the document package. I expect it uses a different URI for the main document relationship. If you dump the .rels part with opc-diag you should be able to see it.

If you do that on a regular Word document you get something like this:

$ opc browse default.docx .rels
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="x" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
  <Relationship Id="x" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
  <Relationship Id="x" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
  <Relationship Id="x" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" Target="docProps/thumbnail.jpeg"/>
</Relationships>

The second relationship item is the one python-docx is looking for to start the "open" process. Apparently there's change in there somewhere when saving as strict OpenXML.

@cristian-codorean
Copy link
Author

Thanks @scanny, my bad for using a 'Strict Open XML Document'. Re-generated my input document as a standard 'Word document' .docx and that seems to have fixed the problem.

@scanny
Copy link
Contributor

scanny commented May 26, 2016

Closing as resolved.

@scanny scanny closed this as completed May 26, 2016
@wjchun
Copy link

wjchun commented Apr 23, 2021

i have same issue , could you have other way using the python-docx to open the docx when saving as the strict open xml ?

@tungduonghgg123
Copy link

i have same issue , could you have other way using the python-docx to open the docx when saving as the strict open xml ?

@scanny Hi, is there a way we can open this?

@Sicolas-Flamel
Copy link

Hi you guys: try save AS a new file and read it..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants