赞
踩
word应用第3节, 转化能力3星,文本类word可以转化,无法满足复杂结构word的转化需求;
批量word 转批量txt
需要安装docx和 os, 左下角终端:输入“pip install docx ”,“pip install os ”安装成功后,
- import os
- import docx
-
- docx_folder = 'C:/Users/xxxx/change/'
- txt_folder = 'C:/Users/xxxx/document/'
-
- # 获取所有Word文档的文件名
- docx_files = [filename for filename in os.listdir(docx_folder) if filename.endswith('.docx')]
-
- for docx_file in docx_files:
- # 读取Word文档
- doc = docx.Document(os.path.join(docx_folder, docx_file))
-
- # 将文档中的所有段落拼接成一个字符串
- doc_text = '\n'.join([paragraph.text for paragraph in doc.paragraphs])
-
- # 生成纯文本文件名
- txt_file = os.path.splitext(docx_file)[0] + '.txt'
-
- # 写入纯文本文件
- #f = open(txt_file, "w", encoding='utf-8')
-
- with open(os.path.join(txt_folder, txt_file), 'w') as f:
- f.write(doc_text)
与上贴不一样 ,本次是2个路径:docx_folder = 'C:/Users/xxxx/change/'
txt_folder = 'C:/Users/xxxx/document/'
红色字体文件路径为自己电脑内部需要改文件的路径,其他内容不用进行变动;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。