Pesquisar PDF via Python

Pesquisa avançada de documentos PDF. Use o Aspose.PDF for Python for .NET para modificar documentos PDF de forma programática

Como pesquisar arquivos PDF usando Python

Para pesquisar um ficheiro PDF, utilizaremos Aspose.PDF for Python via .NET, uma API poderosa e fácil de utilizar. Abra o PyPI, instale-o e pesquise por aspose-pdf. Em alternativa, execute o comando:

Console

pip install aspose-pdf

Pesquisar arquivo PDF via Python


Você precisa Aspose.PDF for .NET testar o código em seu ambiente.

  1. Carregue o PDF com uma instância do Document.
  2. Crie um objeto TextFragmentAbsorber com texto para encontrar como parâmetro.
  3. Obtenha toda a coleção de fragmentos de texto extraídos.
  4. Percorra cada fragmento para obter todas as suas informações.

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