Python如何調用spire.doc輕松讀取Word文檔內容
前言
Spire.Doc for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。這款控件的主要功能在于幫助開發(fā)人員輕松快捷高效的創(chuàng)建、編輯、轉換、比較和打印 Microsoft Word 文檔。作為一款獨立的 Word .NET 控件,Spire.Doc for .NET 的運行系統(tǒng)(服務器端或客戶端)均無需安裝 Microsoft Word,但是它卻可以將 Microsoft Word 文檔的操作功能集成到任何開發(fā)人員的 .NET(ASP.NET、Windows Form、.NET Core、.NET 5.0、.NET 6.0、.NET 7.0、.NET Standard、 Xamarin 和 Mono Android)應用程序中。
spire的更多使用和方法可見:
冰藍科技 e-iceblue | 您的辦公文檔開發(fā)技術專家 | C#/VB.Net Excel, Word, PowerPoint, PDF, Barcode 組件
注意,文件在讀取或寫入操作時必須是關閉狀態(tài),否則會報錯。
讀取全部文本內容
from spire.doc import * from spire.doc.common import * inputFile = r'自檢測試報告.doc' outputFile = r'自檢測試報告.docx' document = Document() # 創(chuàng)建Document實例 document.LoadFromFile(inputFile) # 加載Word文檔 document_text = document.GetText() print(document_text)
通過節(jié)點讀取數據
Document.Sections[index] 屬性可用于獲取Word 文檔中的特定節(jié)點。 獲取后,可遍歷該節(jié)中的段落、表格等。
print(len(document.Sections)) # 獲取節(jié)點數量 print(document.Sections.Count) # 獲取節(jié)點數量 section = document.Sections # 分段落獲取文本內容 for i in range(document.Sections.Count): paragraphs = section[i].Paragraphs for p in range(paragraphs.Count): print(paragraphs[p].Text)
按頁讀取
因為Word文檔本質上是流式文檔,流式布局,所以沒有“頁面”的概念。為了方便頁面操作,Spire.Doc for Python提供了FixedLayoutDocument類,用于將Word文檔轉換為固定布局。
layoutDoc = FixedLayoutDocument(document) # 創(chuàng)建FixedLayoutDocument類的實例,用于將Word文檔轉換為固定布局。 print(layoutDoc.Pages.Count) for p in range(layoutDoc.Pages.Count): page_data = layoutDoc.Pages[p] # print(page_data.Text) # 按頁讀取文本 cols_data = page_data.Columns for col in range(len(cols_data)): # print(cols_data[col].Text) # 按段讀取文本 row_data = cols_data[col].Lines for row in range(len(row_data)): print(row_data[row].Text) # 按行讀取文本
讀取頁眉頁腳
section = document.Sections for i in range(document.Sections.Count): header = section[i].HeadersFooters.Header # 獲取該節(jié)的頁眉對象 footer = section[i].HeadersFooters.Footer # 獲取該節(jié)的頁腳對象 for h in range(header.Paragraphs.Count): headerPara = header.Paragraphs[h] print(headerPara.Text) for f in range(footer.Paragraphs.Count): footerPara = footer.Paragraphs[f] print(footerPara.Text)
遍歷表格數據
document = Document() # 創(chuàng)建Document實例 document.LoadFromFile(inputFile) # 加載Word文檔 for i in range(document.Sections.Count): section = document.Sections.get_Item(i) for j in range(section.Tables.Count): table = section.Tables.get_Item(j) # 遍歷表格中的行 for row in range(table.Rows.Count): row_data = [] # 遍歷行中的單元格 for cell in range(table.Rows.get_Item(row).Cells.Count): cell_obj = table.Rows.get_Item(row).Cells.get_Item(cell) cell_text = "" # 獲取單元格中的段落內容 for paragraph_index in range(cell_obj.Paragraphs.Count): paragraph = cell_obj.Paragraphs.get_Item(paragraph_index) cell_text += paragraph.Text row_data.append(cell_text.strip()) # 打印行數據 print(row_data) document.Close()
查找指定文本
def FindAllString(self ,matchString:str,caseSensitive:bool,wholeWord:bool)->List['TextSelection']
參數:
- matchString:str,要查找的內容
- caseSensitive:bool,如果為True,匹配是區(qū)分大小寫的。
- wholeWord:bool,如果為True,匹配的必須是一個完整的單詞。
可對查找的內容進行其他操作
document = Document() # 創(chuàng)建Document實例 document.LoadFromFile(inputFile) # 加載Word文檔 textSelections = document.FindAllString("測試報告", False, True) # 對找到的內容設置高亮顯示顏色 for selection in textSelections: selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.get_Blue() document.SaveToFile(outputFile, FileFormat.Docx) document.Close()
以上就是Python如何調用spire.doc輕松讀取Word文檔內容的詳細內容,更多關于Python讀取Word的資料請關注腳本之家其它相關文章!
相關文章
Python用matplotlib庫畫圖中文和負號顯示為方框的問題解決
matplotlib中畫圖的時候會遇到負號顯示為方框的問題,下面這篇文章主要給大家介紹了關于Python用matplotlib庫畫圖中文和負號顯示為方框的問題解決,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07python使用tensorflow保存、加載和使用模型的方法
本篇文章主要介紹了python使用tensorflow保存、加載和使用模型的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01Python統(tǒng)計一個字符串中每個字符出現了多少次的方法【字符串轉換為列表再統(tǒng)計】
這篇文章主要介紹了Python統(tǒng)計一個字符串中每個字符出現了多少次的方法,涉及Python字符串轉換及列表遍歷、統(tǒng)計等相關操作技巧,需要的朋友可以參考下2019-05-05Python?Dash框架在數據可視化儀表板中的應用與實踐記錄
Python的Plotly?Dash庫提供了一種簡便且強大的方式來構建和展示互動式數據儀表板,本篇文章將深入探討如何使用Dash設計一個互動數據儀表板,并通過代碼示例幫助讀者理解如何實現這一過程,感興趣的朋友一起看看吧2025-03-03