尝试读取docx文件时出现UnicodeDecodeError

问题描述 投票:0回答:1

使用python 3打开docx文件时出错

当我试图跑:

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 python-3.x utf-8 python-unicode
1个回答
0
投票

python-docx可以从所谓的文件object打开文档。它还可以保存到类似文件的对象:

from docx import Document
f = open('jinuj.docx', 'rb')
document = Document(f)
f.close()

要么

with open('jinuj.docx', 'rb') as f:
    source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()

Docs

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.