尝试读取docx文件时出现UnicodeDecodeError [英] UnicodeDecodeError when trying to read docx file
本文介绍了尝试读取docx文件时出现UnicodeDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用python 3打开docx文件时发生错误
Error occurs when opening docx file using python 3
当我尝试运行时:
file=open("jinuj.docx","r",encoding="utf-8").read()
发生以下错误
319 # decode input (taking the buffer into account)
320 data = self.buffer + input
--> 321 (result, consumed) = self._buffer_decode(data, self.errors, final)
322 # keep undecoded input until the next call
323 self.buffer = data[consumed:]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 11: invalid start byte
推荐答案
python-docx
可以从类似于文件的object
中打开文档.它还可以保存到类似文件的对象中:
python-docx
can open a document from a so-called file-like object
. It can also save to a file-like object:
from docx import Document
f = open('jinuj.docx', 'rb')
document = Document(f)
f.close()
OR
with open('jinuj.docx', 'rb') as f:
source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
这篇关于尝试读取docx文件时出现UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文