如何從python檔案中提取資訊?3分鐘搞懂Python文本分析和提取

來源:互聯網
上載者:User
單位收集了很多word格式的調查表,領導需要收集表單裡的 資訊,我就把所有調查表放一個檔案裡,寫了個python小程式把所需的資訊列印出來,這個小程式就能從 Python文本中分析資訊並提取資訊

#coding:utf-8 import osimport win32comfrom win32com.client import Dispatch, constantsfrom 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要乾淨許多。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.