Redaguj PDF przy użyciu Python

Informacje o poufnej redakcji dokumentu PDF. Użyj pliku Aspose.PDF dla Python for .NET, aby programowo modyfikować dokumenty PDF

Jak edytować plik PDF za pomocą biblioteki Python

Aby zredagować plik PDF, użyj Aspose.PDF for Python via .NET, potężnego i łatwego w użyciu interfejsu API. Otwórz PyPi, wyszukaj aspose-pdf i zainstaluj go. Alternatywnie uruchom polecenie:

Redaguj dokumenty PDF za pośrednictwem Python


Potrzebujesz Aspose.PDF dla Pythona przez .NET, aby wypróbować kod w swoim środowisku.

  1. Załaduj plik PDF z instancją dokumentu.
  2. Utwórz obiekt TextFragmentAbsorber z wyszukiwanymi hasłami jako argumentem.
  3. Ustaw opcje wyszukiwania.
  4. Zbierz pętlę przez każdy fragment, aby zredagować.
  5. Zapisz plik PDF.

Edytuj pliki 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)