UnicodeDecodeError when trying to read docx file

前端 未结 1 1337
野的像风
野的像风 2021-01-07 10:05

Error occurs when opening docx file using python 3

When I tried to run:

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

b

相关标签:
1条回答
  • 2021-01-07 10:35

    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

    0 讨论(0)
提交回复
热议问题