python批量提取word内信息


Posted in Python onAugust 09, 2015
单位收集了很多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 相关文章推荐
Python时区设置方法与pytz查询时区教程
Nov 27 Python
Python yield使用方法示例
Dec 04 Python
Python内置函数的用法实例教程
Sep 08 Python
Python中的字典与成员运算符初步探究
Oct 13 Python
python脚本监控docker容器
Apr 27 Python
python的多重继承的理解
Aug 06 Python
python2.7实现复制大量文件及文件夹资料
Aug 31 Python
python编写猜数字小游戏
Oct 06 Python
pytorch 自定义参数不更新方式
Jan 06 Python
Python3 读取Word文件方式
Feb 13 Python
python 密码学示例——凯撒密码的实现
Sep 21 Python
Python 如何安装Selenium
May 06 Python
python实现下载指定网址所有图片的方法
Aug 08 #Python
Python实现多线程抓取妹子图
Aug 08 #Python
通过Python来使用七牛云存储的方法详解
Aug 07 #Python
Python爬虫框架Scrapy实战之批量抓取招聘信息
Aug 07 #Python
深入理解Python中命名空间的查找规则LEGB
Aug 06 #Python
举例详解Python中yield生成器的用法
Aug 05 #Python
Python中return语句用法实例分析
Aug 04 #Python
You might like
php下实现一个阿拉伯数字转中文数字的函数
2008/07/10 PHP
PHP5.5迭代生成器用法实例详解
2016/03/16 PHP
PHP读取CSV大文件导入数据库的实例
2017/07/24 PHP
PHP array_reverse() 函数原理及实例解析
2020/07/14 PHP
一个js拖拽的效果类和dom-drag.js浅析
2010/07/17 Javascript
JavaScript mapreduce工作原理简析
2012/11/25 Javascript
jQuery向后台传入json格式数据的方法
2015/02/13 Javascript
jQuery实现ctrl+enter(回车)提交表单
2015/10/19 Javascript
简单的JS时钟实例讲解
2016/01/13 Javascript
html+javascript+bootstrap实现层级多选框全层全选和多选功能
2017/03/09 Javascript
Vue.js实战之组件之间的数据传递
2017/04/01 Javascript
javascript 判断用户有没有操作页面
2017/10/17 Javascript
详解JSONObject和JSONArray区别及基本用法
2017/10/25 Javascript
vue路由嵌套的SPA实现步骤
2017/11/06 Javascript
Vue.js实现的表格增加删除demo示例
2018/05/22 Javascript
详解ES6 Promise对象then方法链式调用
2018/10/20 Javascript
Element input树型下拉框的实现代码
2018/12/21 Javascript
JavaScript事件对象深入详解
2018/12/30 Javascript
JS html事件冒泡和事件捕获操作示例
2019/05/01 Javascript
Vue.js watch监视属性知识点总结
2019/11/11 Javascript
浅谈vue使用axios的回调函数中this不指向vue实例,为undefined
2020/09/21 Javascript
python获取网页状态码示例
2014/03/30 Python
用不到50行的Python代码构建最小的区块链
2017/11/16 Python
使用python分析统计自己微信朋友的信息
2019/07/19 Python
Pyinstaller 打包exe教程及问题解决
2019/08/16 Python
Python就将所有的英文单词首字母变成大写
2021/02/12 Python
豆腐の盛田屋官网:日本自然派的豆乳面膜、肥皂、化妆水、乳液等
2016/10/08 全球购物
cosme官方海外旗舰店:日本最大化妆品和美容产品的综合口碑网站
2017/01/18 全球购物
罗马尼亚在线杂货店:Pilulka.ro
2019/09/28 全球购物
自主招生自荐书
2013/11/29 职场文书
五年级科学教学反思
2014/02/05 职场文书
社区创先争优承诺书
2014/08/30 职场文书
七一建党日演讲稿
2014/09/05 职场文书
电影地道战观后感
2015/06/04 职场文书
学习计划是什么
2019/04/30 职场文书
Spring中的@Transactional的工作原理
2022/06/05 Java/Android