python批量提取word內(nèi)信息
更新時間:2015年08月09日 16:26:29 投稿:hebedich
這里給大家分享的是php讀取word并提取word內(nèi)信息的方法,十分的簡單實(shí)用,有需要的小伙伴可以參考下。
單位收集了很多word格式的調(diào)查表,領(lǐng)導(dǎo)需要收集表單里的信息,我就把所有調(diào)查表放一個文件里,寫了個python小程序把所需的信息打印出來
#coding:utf-8
import os
import win32com
from win32com.client import Dispatch, constants
from docx import Document
def parse_doc(f):
"""讀取doc,返回姓名和行業(yè)
"""
doc = w.Documents.Open( FileName = f )
t = doc.Tables[0] # 根據(jù)文件中的圖表選擇信息
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,返回姓名和行業(yè)
"""
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()
這種方式產(chǎn)生的text文檔,不能用python用普通的r方式讀取,為了讓python可以用r方式讀取,應(yīng)當(dāng)寫成
doc.SaveAs('c:/test', 4)
注意:系統(tǒng)執(zhí)行完成后,會自動產(chǎn)生文件后綴txt(雖然沒有指明后綴)。
在xp系統(tǒng)下面,應(yīng)當(dāng),
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
照著字面意思應(yīng)該能對應(yīng)到相應(yīng)的文件格式,如果你是office 2003可能支持不了這么多格式。word文件轉(zhuǎn)html有兩種格式可選wdFormatHTML、wdFormatFilteredHTML(對應(yīng)數(shù)字 8、10),區(qū)別是如果是wdFormatHTML格式的話,word文件里面的公式等ole對象將會存儲成wmf格式,而選用 wdFormatFilteredHTML的話公式圖片將存儲為gif格式,而且目測可以看出用wdFormatFilteredHTML生成的HTML 明顯比wdFormatHTML要干凈許多。
當(dāng)然你也可以用任意一種語言通過com來調(diào)用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 日志模塊logging的使用場景及示例
- Python中l(wèi)ogging日志的四個等級和使用
- python tkinter的消息框模塊(messagebox,simpledialog)
- Python網(wǎng)絡(luò)爬蟲信息提取mooc代碼實(shí)例
- python讀取raw binary圖片并提取統(tǒng)計信息的實(shí)例
- python 利用正則表達(dá)式提取特殊信息
- python提取照片坐標(biāo)信息的實(shí)例代碼
- Python網(wǎng)絡(luò)爬蟲與信息提取(實(shí)例講解)
- 如何用Python提取10000份log中的產(chǎn)品信息
相關(guān)文章
pandas中DataFrame字典互轉(zhuǎn)的實(shí)現(xiàn)
在數(shù)據(jù)處理和分析中,Pandas是一個非常強(qiáng)大的Python庫,本文主要介紹了pandas中DataFrame字典互轉(zhuǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
Python OpenCV實(shí)現(xiàn)視頻追蹤
這篇文章主要為大家詳細(xì)介紹了Python OpenCV實(shí)現(xiàn)視頻追蹤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
深入探究Python如何實(shí)現(xiàn)100個并發(fā)請求
在Web開發(fā)和數(shù)據(jù)抓取等領(lǐng)域,并發(fā)請求是提高效率和性能的重要手段,本文將深入探討如何使用Python實(shí)現(xiàn)100個并發(fā)請求,感興趣的小伙伴可以了解下2025-02-02
Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法
這篇文章主要介紹了Python實(shí)現(xiàn)解析Bit Torrent種子文件內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python針對Torrent文件的讀取與解析相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-08-08
機(jī)器學(xué)習(xí)python實(shí)戰(zhàn)之決策樹
這篇文章主要為大家詳細(xì)介紹了機(jī)器學(xué)習(xí)python實(shí)戰(zhàn)之決策樹的相關(guān)資料 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
利用Django提供的ModelForm增刪改數(shù)據(jù)的方法
這篇文章主要介紹了利用Django提供的ModelForm增刪改數(shù)據(jù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
Python下singleton模式的實(shí)現(xiàn)方法
這篇文章主要介紹了Python下singleton模式的實(shí)現(xiàn)方法,有一定的借鑒價值,需要的朋友可以參考下2014-07-07

