通过 Python 搜索 PDF

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

Aspose.PDF for Python for .NET Logo

如何使用 Python 搜索 PDF 文件

要搜索 PDF 文件,我们将使用 Aspose.PDF for Python via .NET,这是一个功能强大且易于使用的 API。打开 PyPI,安装它,然后搜索“aspose-pdf”。或者,运行以下命令:

Console

pip install aspose-pdf

通过 Python 搜索 PDF 文件


你需要 Aspose.PDF for .NET 在你的环境中试用代码。

  1. 使用 “文档” 实例加载 PDF。
  2. 使用要查找的文本作为参数创建 textFragmentAbsorber 对象。
  3. 获取所有提取的文本片段集合。
  4. 循环浏览每个片段以获取其所有信息。

搜索 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} " )