通过 Python 搜索 PDF

高级 PDF 文档搜索。使用 Aspose.PDF for Python for .NET 以编程方式修改 PDF 文档

如何使用Python搜索PDF檔

為了搜索PDF檔,我們將使用[Aspose.PDF用於.NET](https://products.aspose.com/pdf/net)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 python-net 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Python Package Manager Console

pip install aspose-pdf

通過Python搜索 PDF 檔


您需要 [Aspose.PDF用於.NET](https://releases.aspose.com/pdf/net) 在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。 創建文本碎片用文本作為參數查找的吸收器物件。 獲取所有提取的文字片段集合。
  2. 迴圈遍歷每個片段以獲取其所有資訊。

搜尋 PDF 檔 - Python

import aspose.pdf as ap

# Search Text from All the Pages of PDF Document
pdfDocument = ap.Document("c:\\samples\\sample.pdf")

# Create TextAbsorber object to find all instances of the input search phrase
textFragmentAbsorber = ap.text.TextFragmentAbsorber("PDF")

# Accept the absorber for all the pages
pdfDocument.pages.accept(textFragmentAbsorber)

# Loop through the fragments
for textFragment in textFragmentAbsorber.text_fragments:
    print(f"Text : {textFragment.text}" )
    print(f"Position : {textFragment.position}")
    print(f"XIndent : {textFragment.position.x_indent}")
    print(f"YIndent : {textFragment.position.y_indent}")
    print(f"Font - Name : {textFragment.text_state.font.font_name}" )
    print(f"Font - IsAccessible : {textFragment.text_state.font.is_accessible} " )
    print(f"Font - IsEmbedded : {textFragment.text_state.font.is_embedded} " )
    print(f"Font - IsSubset : {textFragment.text_state.font.is_subset} ")
    print(f"Font Size : {textFragment.text_state.font_size}" )
    print(f"Foreground Color : {textFragment.text_state.foreground_color} " )