Python如何調(diào)用spire.doc輕松讀取Word文檔內(nèi)容
前言
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 組件

注意,文件在讀取或?qū)懭氩僮鲿r必須是關閉狀態(tài),否則會報錯。
讀取全部文本內(nè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é)點讀取數(shù)據(jù)
Document.Sections[index] 屬性可用于獲取Word 文檔中的特定節(jié)點。 獲取后,可遍歷該節(jié)中的段落、表格等。
print(len(document.Sections)) # 獲取節(jié)點數(shù)量
print(document.Sections.Count) # 獲取節(jié)點數(shù)量
section = document.Sections
# 分段落獲取文本內(nèi)容
for i in range(document.Sections.Count):
paragraphs = section[i].Paragraphs
for p in range(paragraphs.Count):
print(paragraphs[p].Text)
按頁讀取
因為Word文檔本質(zhì)上是流式文檔,流式布局,所以沒有“頁面”的概念。為了方便頁面操作,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)
遍歷表格數(shù)據(jù)
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 = ""
# 獲取單元格中的段落內(nèi)容
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())
# 打印行數(shù)據(jù)
print(row_data)
document.Close()查找指定文本
def FindAllString(self ,matchString:str,caseSensitive:bool,wholeWord:bool)->List['TextSelection']
參數(shù):
- matchString:str,要查找的內(nèi)容
- caseSensitive:bool,如果為True,匹配是區(qū)分大小寫的。
- wholeWord:bool,如果為True,匹配的必須是一個完整的單詞。
可對查找的內(nèi)容進行其他操作
document = Document() # 創(chuàng)建Document實例
document.LoadFromFile(inputFile) # 加載Word文檔
textSelections = document.FindAllString("測試報告", False, True)
# 對找到的內(nèi)容設置高亮顯示顏色
for selection in textSelections:
selection.GetAsOneRange().CharacterFormat.HighlightColor = Color.get_Blue()
document.SaveToFile(outputFile, FileFormat.Docx)
document.Close()以上就是Python如何調(diào)用spire.doc輕松讀取Word文檔內(nèi)容的詳細內(nèi)容,更多關于Python讀取Word的資料請關注腳本之家其它相關文章!
相關文章
Python操作Redis數(shù)據(jù)庫的超詳細教程
大家應該都知道redis是一個基于內(nèi)存的高效的鍵值型非關系數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關于Python操作Redis的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06
Python連接SQL?server數(shù)據(jù)庫并進行簡單查詢的操作詳解
SQL?Server是微軟推出的重量級的數(shù)據(jù)庫,本文將給大家詳細介紹了一下Python連接SQL?server數(shù)據(jù)庫詳細流程,并通過代碼示例給大家講解的非常清除,具有一定的參考價值,需要的朋友可以參考下2024-02-02

