Rédigez un PDF à l’aide de 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 rédiger un fichier PDF à l'aide de la bibliothèque Python

Pour rédiger un fichier PDF, utilisez Aspose.PDF pour Python via .NET, une API puissante et facile à utiliser. Ouvrez PyPI, recherchez aspose-pdf et installez-le. Vous pouvez également exécuter la commande suivante :

Rédigez des documents PDF via Python


Vous avez besoin de Aspose.PDF pour Python via .NET pour essayer le code dans votre environnement.

  1. Chargez le PDF avec une instance de Document.
  2. Créez un objet TextFragmentAbsorber avec les termes de recherche comme argument.
  3. Définissez les options de recherche.
  4. Parcourez chaque fragment collecté pour le rédiger.
  5. Enregistrez le fichier PDF.

Réviser 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)