python 转doc为txt

系统:Windows11.

1、安装win32com

首先避雷这样直接pip install win32com不行

pywin32中包含win32com。这样才是可以的。再指出,不知道为啥我查的好多是装pypiwin32,后面查到pywin32可以理解为pypiwin32的更新版,所以请装pywin32

pip install pywin32

2、转为txt

这里看到其他很多的人说要先转docx再转txt,我后来查到一个大佬写的,完全用不着这么麻烦,一步就好了。其他用到pywin32对word操作的可以去看这位大佬写的。超级好用!这里感谢大佬!我这里用到的是doc转txt,示例代码如下:

from win32com import client as wc

wordhandle = wc.Dispatch("Word.Application")
wordhandle.Visible = 0 # 后台运行,不显示
wordhandle.DisplayAlerts = 0  #不警告
doc = wordhandle.Documents.Open("xxx.doc")
doc.SaveAs('xxx.txt', 4) #  txt=4, html=10, docx=16, pdf=17
doc.Close()

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要批量将Python中的.docx文件换为.txt文件,可以使用python-docx库来实现。以下是一个示例代码,可以将指定文件夹中的所有.docx文件换为.txt文件: ```python import os from docx import Document # 指定文件夹路径 folder_path = 'your_folder_path' # 遍历文件夹中的所有文件 for file_name in os.listdir(folder_path): if file_name.endswith('.docx'): # 构建.docx文件的完整路径 file_path = os.path.join(folder_path, file_name) # 创建一个新的.txt文件,文件名与.docx文件相同 txt_file_path = os.path.splitext(file_path)\[0\] + '.txt' txt_file = open(txt_file_path, 'w', encoding='utf-8') # 使用python-docx库读取.docx文件内容 doc = Document(file_path) for paragraph in doc.paragraphs: txt_file.write(paragraph.text + '\n') # 关闭.txt文件 txt_file.close() ``` 请将代码中的`your_folder_path`替换为你要换的.docx文件所在的文件夹路径。这段代码会遍历文件夹中的所有.docx文件,并将其内容逐行写入对应的.txt文件中。 #### 引用[.reference_title] - *1* *3* [Python批量docdocx格式](https://blog.csdn.net/vaylove/article/details/128029689)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [10行Python代码批量实现pdftxt,word,提取表格到excel](https://blog.csdn.net/weixin_43173396/article/details/122968475)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

piukaty

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值