Redazione di un PDF utilizzando 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 un file PDF utilizzando la libreria Python

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

Redazione di documenti PDF tramite Python


È necessario Aspose.PDF for Python via .NET per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Crea un oggetto TextFragmentAbsorber con i termini di ricerca come argomento.
  3. Imposta le opzioni di ricerca.
  4. Passa in rassegna ogni frammento raccolto per oscurare.
  5. Salva il file PDF.

Redazione di 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)