Redacta el PDF a través de Python

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

Cómo redactar un archivo PDF con la biblioteca Python

Para redactar un archivo PDF, use Aspose.PDF for Python via .NET, una API potente y fácil de usar. Abra PyPI, busque aspose-pdf e instálelo. También puede ejecutar el comando:

Redactar documentos PDF a través de Python


Necesita Aspose.PDF for .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. Recorra cada recopilación de fragmentos para redactar.
  5. Guarde 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)