Redazione del PDF tramite Python

Informazioni sensibili sulla redazione dei documenti PDF. Usa Aspose.PDF per Python for .NET per modificare i documenti PDF a livello di codice

Come redigere il file PDF usando la libreria Python

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

Redigere documenti 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 i termini di ricerca come argomento.
  3. Imposta le opzioni di ricerca.
  4. Esegui un ciclo attraverso ogni frammento raccolto per oscurare.
  5. Salva il file PDF.

Redigere i file 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)