Wyszukaj pliki PDF w Python

Natywne i wydajne wyszukiwanie dokumentów PDF przy użyciu serwera Aspose.PDF dla interfejsów API Python, bez użycia jakiegokolwiek oprogramowania, takiego jak Microsoft czy Adobe PDF.

Jak przeszukiwać plik PDF za pomocą Python

Aby przeszukiwać plik PDF, użyjemy interfejsu API Aspose.PDF for Python, który jest bogatym w funkcje, wydajnym i łatwym w użyciu interfejsem API do manipulacji dokumentami dla platformy python-net. Otwórz menedżera pakietów NuGet, wyszukaj Aspose.pdf i zainstaluj. Można również użyć następującego polecenia z konsoli Menedżera pakietów.

Python Package Manager Console

pip install aspose-pdf

Przeszukaj plik PDF przez Python


Potrzebujesz Aspose.PDF for .NET, aby wypróbować kod w swoim środowisku.

  1. Załaduj plik PDF z wystąpieniem dokumentu.
  2. Tworzenie obiektu TextFragmentAbsorber z tekstem, aby znaleźć jako parametr.
  3. Pobierz całą kolekcję wyodrębnionych fragmentów tekstu.
  4. Pętla przez każdy fragment, aby uzyskać wszystkie jego informacje.

Wyszukaj pliki 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);
}