python批量提取word內(nèi)信息
更新時間:2015年08月09日 16:26:29 投稿:hebedich
這里給大家分享的是php讀取word并提取word內(nèi)信息的方法,十分的簡單實用,有需要的小伙伴可以參考下。
單位收集了很多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) #不要(),容易引起歧義。
相關(guān)文章
python神經(jīng)網(wǎng)絡(luò)MobileNetV3?large模型的復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)MobileNetV3?large模型的復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
python目標(biāo)檢測yolo3詳解預(yù)測及代碼復(fù)現(xiàn)
這篇文章主要為大家介紹了python目標(biāo)檢測yolo3詳解預(yù)測及代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
解決python 執(zhí)行shell命令無法獲取返回值的問題
這篇文章主要介紹了解決python 執(zhí)行shell命令無法獲取返回值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
python使用Scrapy庫進(jìn)行數(shù)據(jù)提取和處理的方法詳解
在我們的初級教程中,我們介紹了如何使用Scrapy創(chuàng)建和運(yùn)行一個簡單的爬蟲,在這篇文章中,我們將深入了解Scrapy的強(qiáng)大功能,學(xué)習(xí)如何使用Scrapy提取和處理數(shù)據(jù)2023-09-09
python模塊和包的應(yīng)用BASE_PATH使用解析
這篇文章主要介紹了python模塊和包的應(yīng)用BASE_PATH使用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
python爬蟲構(gòu)建代理ip池抓取數(shù)據(jù)庫的示例代碼
這篇文章主要介紹了python爬蟲構(gòu)建代理ip池抓取數(shù)據(jù)庫的示例代碼,幫助大家更好的使用爬蟲,感興趣的朋友可以了解下2020-09-09

