UnicodeDecodeError when trying to read docx file

南笙酒味 提交于 2020-06-22 13:20:49

问题


Error occurs when opening docx file using python 3

When I tried to run:

file=open("jinuj.docx","r",encoding="utf-8").read()

below error occured

    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

回答1:


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()

Docs



来源:https://stackoverflow.com/questions/55217249/unicodedecodeerror-when-trying-to-read-docx-file

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