Pythonで PDF ファイルを検索

Microsoft や Adobe PDF などのソフトウェアを使用せずに、サーバー側の Aspose.PDF を Python API で使用する、ネイティブで高性能な PDF ドキュメント検索。

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

PDFファイルを検索するために、python-net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作APIである Aspose.PDF for .NET APIを使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Python Package Manager Console

pip install aspose-pdf

Python でPDFファイルを検索


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

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

PDFファイルを検索-Python。


//Search Text from All the Pages of PDF Document
Document pdfDocument = new Document("SearchAndGetTextFromAll.pdf");

// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("text");

// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);

// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{

    Console.WriteLine("Text : {0} ", textFragment.Text);
    Console.WriteLine("Position : {0} ", textFragment.Position);
    Console.WriteLine("XIndent : {0} ", textFragment.Position.XIndent);
    Console.WriteLine("YIndent : {0} ", textFragment.Position.YIndent);
    Console.WriteLine("Font - Name : {0}", textFragment.TextState.Font.FontName);
    Console.WriteLine("Font - IsAccessible : {0} ", textFragment.TextState.Font.IsAccessible);
    Console.WriteLine("Font - IsEmbedded : {0} ", textFragment.TextState.Font.IsEmbedded);
    Console.WriteLine("Font - IsSubset : {0} ", textFragment.TextState.Font.IsSubset);
    Console.WriteLine("Font Size : {0} ", textFragment.TextState.FontSize);
    Console.WriteLine("Foreground Color : {0} ", textFragment.TextState.ForegroundColor);
}