在试图打开docx文档内容时,以为可以向读取txt文档一样,于是写下了下面的代码
with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r')as f:
contents = f.read()
print(contents)
结果遇上报错:UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xe3 in position 55: illegal multibyte sequence

解决方法一:
一看,编码错误,祖传方法encoding='utf-8‘’百试百灵的修改
with open('C:\\Users\\Administrator\\Desktop\\案例二.docx','r',encoding='utf-8‘’)as f:
contents = f.read()
print(contents)
结果一样报错UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x87 in position 10: invalid start byte

我就纳闷了,怎么还有 utf-8都解码不了,utf-8号称‘万国码’’(UTF-8编码:它是一种全国家通过的一种编码,如果网站涉及到多个国家的语言,那么建议选择UTF-8编码。),基本上用上它一切就ok了,怎么还报错。我就打了一个“你好”在里面啊!
但既然是编码错误,就继续。
之后按照这篇文章《UnicodeDecodeError: ‘

本文详细记录了解决在Python中读取docx文件遇到的UnicodeDecodeError问题的过程,从尝试各种编码到使用chardet自动判断,最终采用python-docx模块成功读取文档内容。
最低0.47元/天 解锁文章
2万+

被折叠的 条评论
为什么被折叠?



