如何從python檔案中提取資訊? 3分鐘搞懂Python文本分析與擷取
单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来,这个小程序就能从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要干净许多。
以上是如何從python檔案中提取資訊? 3分鐘搞懂Python文本分析與擷取的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

攻克Investing.com的反爬蟲策略許多人嘗試爬取Investing.com(https://cn.investing.com/news/latest-news)的新聞數據時,常常�...
