Python读取word文档(python-docx包)
2017-08-15 09:23
926 查看
最近想统计word文档中的一些信息,人工统计的话。。。三天三夜吧
python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.readthedocs.io/en/latest/index.html
(v0.8.6)
还有一个是 win32com 包,这个包安装步骤如下:
http://jingyan.baidu.com/article/d3b74d64c853081f77e60929.html
安装好 win32com之后安装 docx包:
word文档里是这样的:
执行代码结果:
还可以读取word文档中的表格,图片等其他信息,方便对多个word文档进行统计,分析,处理
python 不愧是万能语言,发现有一个包叫做 docx,非常好用,具体查看官方文档:https://python-docx.readthedocs.io/en/latest/index.html
(v0.8.6)
还有一个是 win32com 包,这个包安装步骤如下:
http://jingyan.baidu.com/article/d3b74d64c853081f77e60929.html
安装好 win32com之后安装 docx包:
pip install python-docx
import docx from win32com import client as wc import matplotlib.pyplot as plt from collections import Counter import os # 首先将doc转换成docx word = 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 = 0 for para in doc.paragraphs : print(para.text) parag += 1 print ('This document has ', parag, ' paragraphs')
word文档里是这样的:
执行代码结果:
还可以读取word文档中的表格,图片等其他信息,方便对多个word文档进行统计,分析,处理
相关文章推荐
- python网络爬虫文档读取-微软Word文档和.docx
- 用Python读取Word文档并写入Excel(二)
- [Python] python-docx 编辑word文档
- python读取word文档的方法
- python-docx修改已存在的Word文档的表格的字体格式方法
- Python读取Word(.docx)正文信息
- python读取word文档的方法
- python读取word文档,插入mysql数据库的示例代码
- Python-docx:读写word文档的Python库
- python如何处理解析word文档doc docx , python-docx,python-docx2txt,zipfile
- Python读取Word(.docx)正文信息的方法
- 将Doc或者Docx文档处理成html的代码逻辑;统计word中的字数,段数,句数,读取word中文档内容的代码逻辑
- Ubuntu下使用python读取doc和docx文档的内容
- Ubuntu下使用python读取doc和docx文档的内容方法
- 用python-docx修改已存在的Word文档的表格的字体格式:
- python python-docx 替换word文档部分内容
- 用Python读取Word文档并写入Excel(一)
- $用python-docx模块读写word文档
- Java读取word文档,转换为网页
- Java用poi读取Word文档