测试环境:Windows10,Python3.6.3 / Python3.8 命令如下:
pip3 install docx
安装成功截图:
安装完成后导入使用:
测试代码:
import docx def docx_to_txt(): # 打开文件 file = docx.opendocx("./测试文件.docx") # 读取文本内容 text = docx.getdocumenttext(file) # 打印输出到屏幕 print(type(text), text) # 调用函数 docx_to_txt()会发现报错:
ModuleNotFoundError: No module named ‘exceptions’
报错分析:
说没有 exceptions 这个模块,由于Python3已经取消了这个模块,而 PendingDeprecationWarning 是内置 可以直接使用的,所以我们直接进入文件(根据你报错显示的路径):
然后把报错的第30行: from exceptions import PendingDeprecationWarning 注释掉,然后保存,就可以啦。
注释前:
注释后:
再次运行测试代码,转化后得到的是一个列表格式的数据,然后根据自己的需求进行下一步处理或保存到文件。
word文档(docx)的读取和写入:docx模块
参考:https://blog.csdn.net/a649344475/article/details/81162381