Redaguj PDF przez Python

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

Jak zredagować 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 API. Otwórz PyPI, wyszukaj aspose-pdf i zainstaluj. Alternatywnie uruchom polecenie:

Redagowanie dokumentów PDF za pośrednictwem Python


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

  1. Załaduj plik PDF z wystąpieniem dokumentu.
  2. Tworzenie obiektu TextFragmentAbsorber z wyszukiwanych haseł jako argument.
  3. Ustaw opcje wyszukiwania.
  4. Pętla przez każdy fragment zbierać do redagowania.
  5. Zapisz plik PDF.

Redagowanie plików 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)