使用python读取doc、docx文档
1.读取docx
需要使用python-docx
(1)安装python-docx
python -m pip install python-docx
(2)读取docx
import docx document = docx.Document("D:/资料/me/AA.docx") #获取所有段落 all_paragraphs = document.paragraphs for paragraph in all_paragraphs: #打印每一个段落的文字 print(paragraph.text)
2.读取doc
无法直接读取doc,需要先将doc文件转换为docx文件
在windows上将doc文件转换为docx文件时,需要用到win32com包
使用win32com包需要安装——pypiwin32
(1)安装pypiwin32
python -m pip install pypiwin32
(2)读取doc
import docx import win32com.client as wc import operator #doc文件另存为docx word = wc.Dispatch("Word.Application") doc = word.Documents.Open(r"D:\\资料\\me\\BB.doc") # 12代表转换后为docx文件 doc.SaveAs(r"D:\\资料\\me\\docx\\BB.docx", 12) doc.Close word.Quit #读取转换后的docx path = "D:/资料/me/docx/BB.docx" file = docx.Document(path) for p in file.paragraphs: print(p.text)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2020-10-20 tushare
2020-10-20 baostock
2020-10-20 mpl_finance
2020-10-20 pyecharts的KLine使用
2020-10-20 AttributeError: module 'websocket' has no attribute 'enableTrace'