Redija PDF usando Python

Informações de redação confidenciais de documentos PDF. Use o Aspose.PDF para Python for .NET para modificar documentos PDF programaticamente

Como redigir um arquivo PDF usando a biblioteca Python

Para redigir um arquivo PDF, use Aspose.PDF para Python via.NET, uma API poderosa e fácil de usar. Abra PyPI, procure por aspose-pdf e instale-o. Como alternativa, execute o comando:

Edite documentos PDF via Python


Você precisa de Aspose.PDF para Python via .NET para testar o código em seu ambiente.

  1. Carregue o PDF com uma instância do Document.
  2. Crie o objeto TextFragmentAbsorber com termos de pesquisa como argumento.
  3. Defina as opções de pesquisa.
  4. Percorra cada fragmento coletado para redigir.
  5. Salve o arquivo PDF.

Redigir arquivos PDF - Python

import aspose.pdf as apdf

from os import path
path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)

searchTerm = "Secret word"
textFragmentAbsorber = apdf.text.TextFragmentAbsorber(searchTerm)
textSearchOptions = apdf.text.TextSearchOptions(True)
textFragmentAbsorber.text_search_options = textSearchOptions

document.pages.accept(textFragmentAbsorber)
textFragmentCollection = textFragmentAbsorber.text_fragments
for textFragment in textFragmentCollection:
    page = textFragment.page
    annotationRectangle = textFragment.rectangle
    annot = apdf.annotations.RedactionAnnotation(page, annotationRectangle)
    annot.fill_color = apdf.Color.black
    document.pages[page.number].annotations.add(annot, True)
    annot.redact()

    document.save(path_outfile)