通过 Python 搜索 PDF

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

如何使用Python搜索PDF檔

要搜尋 PDF 文件,我們將使用 Aspose.PDF for Python via .NET,這是一個強大且易於使用的 API。打開 PyPI,安裝它,然後搜尋「aspose-pdf」。或者,執行以下命令:

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 apdf

from os import path
path_infile = path.join(self.data_dir, infile)

document = apdf.Document(path_infile)

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

# Accept the absorber for all the pages
document.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} " )