Redigieren Sie PDF mit Python

Vertrauliche Redaktionsinformationen für PDF-Dokumente. Verwenden Sie Aspose.PDF für Python for .NET, um PDF-Dokumente programmgesteuert zu ändern

So redigieren Sie eine PDF-Datei mit der Python Library

Um eine PDF-Datei zu redigieren, verwenden Sie Aspose.PDF for Python via .NET, eine leistungsstarke und einfach zu bedienende API. Öffne PyPI, suche nach aspose-pdf und installiere es. Führen Sie alternativ den folgenden Befehl aus:

Redigieren Sie PDF-Dokumente über Python


Sie benötigen Aspose.PDF für Python via .NET, um den Code in Ihrer Umgebung auszuprobieren.

  1. Laden Sie das PDF mit einer Instanz von Document.
  2. Erstellen Sie ein TextFragmentAbsorber-Objekt mit Suchbegriffen als Argument.
  3. Legen Sie die Suchoptionen fest.
  4. Durchlaufe jedes Fragment, um es zu redigieren.
  5. Speichern Sie die PDF-Datei.

Redigieren Sie PDF-Dateien - 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)