python批量提取word內(nèi)信息
#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
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í)行完成后,會(huì)自動(dòng)產(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
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的使用場(chǎng)景及示例
- Python中l(wèi)ogging日志的四個(gè)等級(jí)和使用
- python tkinter的消息框模塊(messagebox,simpledialog)
- Python網(wǎng)絡(luò)爬蟲信息提取mooc代碼實(shí)例
- python讀取raw binary圖片并提取統(tǒng)計(jì)信息的實(shí)例
- python 利用正則表達(dá)式提取特殊信息
- python提取照片坐標(biāo)信息的實(shí)例代碼
- Python網(wǎng)絡(luò)爬蟲與信息提取(實(shí)例講解)
- 如何用Python提取10000份log中的產(chǎn)品信息
相關(guān)文章
python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的代碼實(shí)例
這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式完成車牌號(hào)檢驗(yàn)的相關(guān)資料,在Python中正則表達(dá)式是一種用于匹配和操作字符串的強(qiáng)大工具,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02python神經(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-05python目標(biāo)檢測(cè)yolo3詳解預(yù)測(cè)及代碼復(fù)現(xiàn)
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)yolo3詳解預(yù)測(cè)及代碼復(fù)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05解決python 執(zhí)行shell命令無法獲取返回值的問題
這篇文章主要介紹了解決python 執(zhí)行shell命令無法獲取返回值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12python使用Scrapy庫進(jìn)行數(shù)據(jù)提取和處理的方法詳解
在我們的初級(jí)教程中,我們介紹了如何使用Scrapy創(chuàng)建和運(yùn)行一個(gè)簡(jiǎn)單的爬蟲,在這篇文章中,我們將深入了解Scrapy的強(qiáng)大功能,學(xué)習(xí)如何使用Scrapy提取和處理數(shù)據(jù)2023-09-09Python requests接口測(cè)試實(shí)現(xiàn)代碼
這篇文章主要介紹了Python requests接口測(cè)試實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Python實(shí)現(xiàn)字典序列ChainMap
容器數(shù)據(jù)類型包括數(shù)組list,字典dict以及元組tuple等。本篇主要介紹了ChainMap字典序列的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06python模塊和包的應(yīng)用BASE_PATH使用解析
這篇文章主要介紹了python模塊和包的應(yīng)用BASE_PATH使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12python爬蟲構(gòu)建代理ip池抓取數(shù)據(jù)庫的示例代碼
這篇文章主要介紹了python爬蟲構(gòu)建代理ip池抓取數(shù)據(jù)庫的示例代碼,幫助大家更好的使用爬蟲,感興趣的朋友可以了解下2020-09-09