1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Python读取word文档(python-docx包)

Python读取word文档(python-docx包)

时间:2019-04-30 14:40:45

相关推荐

Python读取word文档(python-docx包)

最近想统计word文档中的一些信息,人工统计的话。。。三天三夜吧

python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.readthedocs.io/en/latest/index.html

(v0.8.6)

还有一个是 win32com 包,这个包安装步骤如下:

/article/d3b74d64c853081f77e60929.html

安装好 win32com之后安装 docx包:

pip install python-docx

import docxfrom win32com import client as wcimport matplotlib.pyplot as pltfrom collections import Counter import os# 首先将doc转换成docxword = wc.Dispatch("Word.Application")# 找到word路径 + 文件名 ,即可打开文件full_path = 'C:\\Users\\ASUS\\Desktop\\test.docx'doc = word.Documents.Open(full_path)# 使用参数16表示将doc转换成docx,保存成docx后才能 读文件doc.SaveAs(r"D:\\test2.docx",16)doc.Close()word.Quit()# 读取word内容#这里是以段落为单位的,下面用一个for 遍历所有段落doc = docx.Document("D:\\test2.docx")parag_num = 0for para in doc.paragraphs :print(para.text)parag += 1 print ('This document has ', parag, ' paragraphs')

word文档里是这样的:

执行代码结果:

还可以读取word文档中的表格,图片等其他信息,方便对多个word文档进行统计,分析,处理

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。