No relationship of type when opening Word document with Python

江枫思渺然 提交于 2019-12-02 17:01:05

问题


When trying to open a .dot file with python-docx, I am getting the error:

KeyError: "no relationship of type 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' in collection"

This is the code in question:

from docx import Document
document = Document('file.dot')

What is the actual problem here?


回答1:


How did you generate the input file? Here it is an issue about the type when you save the file as Strict Open XML Document. Try the standard Word document.

You can get more informations about relations inside the file using opc-diag:

opc browse <FILE> .rels

An idea to try to fix the bad file:

# Extract the bad file to a temporary folder
unzip <FILE> -d bad-file

# Repackage the extracted data to a new fresh file
opc repackage bad-file new-file.docx

# A diff of relationships
opc diff-item test.docx test-ok.docx .rels


来源:https://stackoverflow.com/questions/39104325/no-relationship-of-type-when-opening-word-document-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!