Cerca PDF tramite Python

Ricerca avanzata di documenti PDF. Usa Aspose.PDF per Python for .NET per modificare i documenti PDF a livello di codice

Come cercare file PDF usando Python

Per cercare un file PDF, useremo Aspose.PDF for Python via .NET, un’API potente e facile da usare. Apri PyPI, installalo e cerca aspose-pdf. In alternativa, esegui il comando:

Console

pip install aspose-pdf

Cerca file PDF tramite Python


È necessario Aspose.PDF for .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Creare un oggetto TextFragmentAbsorber con testo da trovare come parametro.
  3. Ottieni tutta la raccolta di frammenti di testo estratti.
  4. Esegui un ciclo di ogni frammento per ottenere tutte le sue informazioni.

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