導航:首頁 > 編程語言 > python帶格式讀取word

python帶格式讀取word

發布時間:2025-01-02 23:48:25

❶ 如何在 Linux 上使用 python 讀取 word 文件信息

首先下載安裝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
= 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
= 13
wdFormatXMLTemplate = 14
= 15
wdFormatXPS = 18

照著字面意思應該能對應到相應的文件格式,如果你是office 2003可能支持不了這么多格式。word文件轉html有兩種格式可選wdFormatHTML、wdFormatFilteredHTML(對應數字 8、10),區別是如果是wdFormatHTML格式的話,word文件裡面的公式等ole對象將會存儲成wmf格式,而選用 wdFormatFilteredHTML的話公式圖片將存儲為gif格式,而且目測可以看出用wdFormatFilteredHTML生成的HTML 明顯比wdFormatHTML要干凈許多。
當然你也可以用任意一種語言通過com來調用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)
#不要(),容易引起歧義。

❷ 如何在 Linux 上使用 Python 讀取 word 文件信息

第一步:獲取doc文件的xml組成文件

import zipfiledef get_word_xml(docx_filename):
with open(docx_filename) as f:
zip = zipfile.ZipFile(f)
xml_content = zip.read('word/document.xml')
return xml_content

第二步:解析xml為樹形數據結構
from lxml import etreedef get_xml_tree(xml_string):
return etree.fromstring(xml_string)

第三步:讀取word內容:
def _itertext(self, my_etree):
"""Iterator to go through xml tree's text nodes"""
for node in my_etree.iter(tag=etree.Element):
if self._check_element_is(node, 't'):
yield (node, node.text)def _check_element_is(self, element, type_char):
word_schema = '99999'
return element.tag == '{%s}%s' % (word_schema,type_char)

❸ python如何讀取word文件中的文本內容並寫入到新的txt文件

閱讀全文

與python帶格式讀取word相關的資料

熱點內容
php按鈕點擊事件 瀏覽:934
河南伺服器機房售後服務雲主機 瀏覽:184
android仿聯系人 瀏覽:368
什麼app軟體可以學音標 瀏覽:669
郭天祥十天學會單片機優酷 瀏覽:330
什麼app兒童免費 瀏覽:582
遺傳演算法的理解 瀏覽:800
php刪除sql 瀏覽:841
紅進藍出指標源碼 瀏覽:700
python數據轉換列表類型 瀏覽:717
解壓後的文件怎麼解開 瀏覽:175
四川補貼認證下載什麼app 瀏覽:858
android設計風格 瀏覽:426
視頻不支持我的加密 瀏覽:342
布包pdf 瀏覽:267
程序員錄制課程表 瀏覽:626
eclipsephp斷點調試 瀏覽:895
虛擬成交量指標源碼 瀏覽:838
什麼APP有背單詞小組 瀏覽:43
蘋果2g視頻怎麼加密 瀏覽:204