Python将word转化为txt文本

电玩女神 2022-12-03 13:25 732阅读 0赞

目录

    1. 安装docx包
    1. 导入使用
  • 3.报错分析及解决方法
    1. 将word转化为txt文本
    1. docx模块的其他用法

1. 安装docx包

测试环境:Windows10,Python3.6.3 / Python3.8
命令如下:

pip3 install docx

安装成功截图:

在这里插入图片描述

2. 导入使用

安装完成后导入使用:

测试代码:

  1. import docx
  2. def docx_to_txt():
  3. # 打开文件
  4. file = docx.opendocx("./测试文件.docx")
  5. # 读取文本内容
  6. text = docx.getdocumenttext(file)
  7. # 打印输出到屏幕
  8. print(type(text), text)
  9. # 调用函数
  10. docx_to_txt()

会发现报错:

ModuleNotFoundError: No module named ‘exceptions’

在这里插入图片描述

3.报错分析及解决方法

报错分析:

说没有 exceptions 这个模块,由于Python3已经取消了这个模块,而 PendingDeprecationWarning 是内置
可以直接使用的,所以我们直接进入文件(根据你报错显示的路径):

在这里插入图片描述
然后把报错的第30行: from exceptions import PendingDeprecationWarning 注释掉,然后保存,就可以啦。

注释前:

在这里插入图片描述
注释后:

在这里插入图片描述

4. 将word转化为txt文本

再次运行测试代码,转化后得到的是一个列表格式的数据,然后根据自己的需求进行下一步处理或保存到文件。

在这里插入图片描述

5. docx模块的其他用法

word文档(docx)的读取和写入:docx模块

参考:https://blog.csdn.net/a649344475/article/details/81162381

发表评论

表情:
评论列表 (有 0 条评论,732人围观)

还没有评论,来说两句吧...

相关阅读