Redacta un PDF usando Python

Información de redacción confidencial de documentos PDF. Utilice Aspose.PDF para Python for .NET para modificar documentos PDF mediante programación

Cómo redactar un archivo PDF usando la biblioteca Python

Para redactar un archivo PDF, utilice Aspose.PDF para Python vía .NET, una API potente y fácil de usar. Abra PyPI, busque aspose-pdf e instálelo. Como alternativa, ejecute el comando:

Redacta documentos PDF a través de Python


Necesita Aspose.PDF para Python a través de.NET para probar el código en su entorno.

  1. Cargue el PDF con una instancia de Document.
  2. Cree un objeto TextFragmentAbsorber con términos de búsqueda como argumento.
  3. Defina las opciones de búsqueda.
  4. Recorre cada fragmento recopilado para redactarlo.
  5. Guarda el archivo PDF.

Redactar archivos 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)