Rédigez le PDF via Python

Informations de rédaction sensibles pour les documents PDF. Utilisez Aspose.PDF pour Python for .NET afin de modifier des documents PDF par programmation

Comment biffer un fichier PDF à l'aide de la bibliothèque Python

Pour rédiger un fichier PDF, utilisez Aspose.PDF for Python via .NET, une API puissante et simple d’utilisation. Ouvrez PyPI, recherchez « aspose-pdf » et installez-le. Vous pouvez également exécuter la commande suivante :

Biffure des documents PDF via Python


Vous devez Aspose.PDF for Python via .NET essayer le code dans votre environnement.

  1. Chargez le PDF avec une instance de Document.
  2. Créez un objet TextFragmentAbsorber avec des termes de recherche en argument.
  3. Définissez les options de recherche.
  4. Passez en boucle chaque fragment collecté pour biffer.
  5. Enregistrez le fichier PDF.

Biffure des fichiers 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)