使用Python打开Word文档时没有类型关系 [英] No relationship of type when opening Word document with Python

查看:2456
本文介绍了使用Python打开Word文档时没有类型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用python-docx打开.dot文件时,出现错误:

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"

这是有问题的代码

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

这里的实际问题是什么?

What is the actual problem here?

推荐答案

您是如何生成输入文件的? 此处将文件另存为严格打开XML文档.尝试使用标准的 Word文档.

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.

您可以使用 opc-diag 获取有关文件内部关系的更多信息:

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

这篇关于使用Python打开Word文档时没有类型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆