Python で PDF を検索する

高度な PDF ドキュメント検索。プログラムで PDF ドキュメントを変更するには、Python for .NET の Aspose.PDF を使用してください

Python を使ってPDFファイルを検索する方法

PDFファイルを検索するには、強力で使いやすいAPIであるAspose.PDF for Python via .NETを使用します。PyPIを開いてインストールし、「aspose-pdf」を検索してください。または、以下のコマンドを実行してください。

Console

pip install aspose-pdf

Python でPDFファイルを検索


お使いの環境でコードを試すには Aspose.PDF for .NET が必要です。

  1. Document インスタンスを使用して PDF を読み込みます。
  2. 検索するテキストをパラメータとして TextFragmentAbsorber オブジェクトを作成します。
  3. 抽出されたすべてのテキストフラグメントコレクションを取得します。
  4. 各フラグメントをループ処理して、そのすべての情報を取得します。

PDF ファイルを検索-{{プログラミング言語}}

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