UnicodeDecodeError при попытке прочитать файл docx - PullRequest
0 голосов
/ 18 марта 2019

Ошибка возникает при открытии файла docx с использованием 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

1 Ответ

0 голосов
/ 18 марта 2019

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

Документы

...