欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何調用spire.doc輕松讀取Word文檔內容

 更新時間:2025年02月13日 09:13:01   作者:覓遠  
Spire.Doc?for?.NET?是一款專門對?Word?文檔進行操作的?.NET?類庫,本文為大家介紹了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的資料請關注腳本之家其它相關文章!

相關文章

最新評論