python批量提取word内信息
作者:hebedich 时间:2021-05-18 14:25:50
单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来
#coding:utf-8
import os
import win32com
from win32com.client import Dispatch, constants
from docx import Document
def parse_doc(f):
"""读取doc,返回姓名和行业
"""
doc = w.Documents.Open( FileName = f )
t = doc.Tables[0] # 根据文件中的图表选择信息
name = t.Rows[0].Cells[1].Range.Text
situation = t.Rows[0].Cells[5].Range.Text
people = t.Rows[1].Cells[1].Range.Text
title = t.Rows[1].Cells[3].Range.Text
print name, situation, people,title
doc.Close()
def parse_docx(f):
"""读取docx,返回姓名和行业
"""
d = Document(f)
t = d.tables[0]
name = t.cell(0,1).text
situation = t.cell(0,8).text
people = t.cell(1,2).text
title = t.cell(1,8).text
print name, situation, people,title
if __name__ == "__main__":
w = win32com.client.Dispatch('Word.Application')
# 遍历文件
PATH = "H:\work\\aaa" # windows文件路径
doc_files = os.listdir(PATH)
for doc in doc_files:
if os.path.splitext(doc)[1] == '.docx':
try:
parse_docx(PATH+'\\'+doc)
except Exception as e:
print e
elif os.path.splitext(doc)[1] == '.doc':
try:
parse_doc(PATH+'\\'+doc)
except Exception as e:
print e
下载安装win32com
from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open('c:/test')
doc.SaveAs('c:/test.text', 2)
doc.Close()
word.Quit()
这种方式产生的text文档,不能用python用普通的r方式读取,为了让python可以用r方式读取,应当写成
doc.SaveAs('c:/test', 4)
注意:系统执行完成后,会自动产生文件后缀txt(虽然没有指明后缀)。
在xp系统下面,应当,
open(r'c:\text','r')
wdFormatDocument = 0 wdFormatDocument97 = 0 wdFormatDocumentDefault = 16 wdFormatDOSText = 4 wdFormatDOSTextLineBreaks = 5 wdFormatEncodedText = 7 wdFormatFilteredHTML = 10 wdFormatFlatXML = 19 wdFormatFlatXMLMacroEnabled = 20 wdFormatFlatXMLTemplate = 21 wdFormatFlatXMLTemplateMacroEnabled = 22 wdFormatHTML = 8 wdFormatPDF = 17 wdFormatRTF = 6 wdFormatTemplate = 1 wdFormatTemplate97 = 1 wdFormatText = 2 wdFormatTextLineBreaks = 3 wdFormatUnicodeText = 7 wdFormatWebArchive = 9 wdFormatXML = 11 wdFormatXMLDocument = 12 wdFormatXMLDocumentMacroEnabled = 13 wdFormatXMLTemplate = 14 wdFormatXMLTemplateMacroEnabled = 15 wdFormatXPS = 18
照着字面意思应该能对应到相应的文件格式,如果你是office 2003可能支持不了这么多格式。word文件转html有两种格式可选wdFormatHTML、wdFormatFilteredHTML(对应数字 8、10),区别是如果是wdFormatHTML格式的话,word文件里面的公式等ole对象将会存储成wmf格式,而选用 wdFormatFilteredHTML的话公式图片将存储为gif格式,而且目测可以看出用wdFormatFilteredHTML生成的HTML 明显比wdFormatHTML要干净许多。
当然你也可以用任意一种语言通过com来调用office API,比如PHP.
from win32com import client as wc
word = wc.Dispatch('Word.Application')
doc = word.Documents.Open(r'c:/test1.doc')
doc.SaveAs('c:/test1.text', 4)
doc.Close()
import re
strings=open(r'c:\test1.text','r').read()
result=re.findall('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)',strings)
chan=re.sub('\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)|\(\s*[A-D]\s*\)|\(\xa1*[A-D]\xa1*\)','()',strings)
question=open(r'c:\question','a+')
question.write(chan)
question.close()
answer=open(r'c:\answeronly','a+')
for i,a in enumerate(result):
m=re.search('[A-D]',a)
answer.write(str(i+1)+' '+m.group()+'\n')
answer.close()
chan=re.sub(r'\xa3\xa8\s*[A-D]\s*\xa3\xa9','()',strings) #不要(),容易引起歧义。
标签:python,批量提取word
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 短视频爬虫教程
2022-02-13 00:17:33
![](https://img.aspxhome.com/file/2023/3/102803_0s.png)
python多线程方法详解
2023-10-16 02:46:31
Python中一些深不见底的“坑”
2021-10-10 05:03:57
Python中文分词工具之结巴分词用法实例总结【经典案例】
2023-05-05 01:46:30
![](https://img.aspxhome.com/file/2023/6/100886_0s.jpg)
编程经验点滴 动态SQL的拼接技巧
2012-11-30 20:03:58
网页设计之文字的辨识度与可读性
2007-10-26 16:19:00
![](https://img.aspxhome.com/file/UploadPic/200710/26/20071026162228999s.gif)
正计时JS代码
2008-05-25 14:53:00
python 如何查看pytorch版本
2021-02-05 05:38:59
浅谈sklearn中predict与predict_proba区别
2023-11-08 03:53:45
python匿名函数的使用方法解析
2022-05-08 13:00:25
asp解决防止表单重复提交的方法
2007-10-19 18:40:00
完美解决phpdoc导出文档中@package的warning及Error的错误
2023-10-07 10:07:49
Python 字典(Dictionary)操作详解
2023-07-24 06:39:35
Python小游戏之300行代码实现俄罗斯方块
2023-10-09 17:35:33
![](https://img.aspxhome.com/file/2023/1/108121_0s.png)
asp长文章分页显示思路
2007-08-23 13:54:00
python多进程控制学习小结
2021-08-31 00:48:57
![](https://img.aspxhome.com/file/2023/2/67732_0s.png)
python自定义时钟类、定时任务类
2021-02-12 12:21:37
K最近邻算法(KNN)---sklearn+python实现方式
2023-09-14 15:29:31
![](https://img.aspxhome.com/file/2023/9/90769_0s.jpg)
ubuntu系统下使用pm2设置nodejs开机自启动的方法
2023-10-02 22:23:16
python机器学习理论与实战(一)K近邻法
2021-08-27 18:05:49
![](https://img.aspxhome.com/file/2023/4/90764_0s.jpg)