Pythonを使って PDF を編集する

PDF ドキュメントの機密編集情報。プログラムで PDF ドキュメントを変更するには、Aspose.PDF for Python for .NET} を使用してください

Python ライブラリを使って PDF ファイルを編集する方法

PDF ファイルを編集するには、強力で使いやすい API である Aspose.PDF for Python via .NET を使用してください。PyPi を開き、「aspose-pdf」を検索してインストールします。または、以下のコマンドを実行します。

Python を使って PDF ドキュメントを編集できます


ご使用の環境でコードを試すには、.NET 経由の Python Aspose.PDF が必要です。

1。PDF に Document のインスタンスをロードします。 1。検索語を引数として TextFragmentAbSorber オブジェクトを作成します。 1。検索オプションを設定します。 1。各フラグメントコレクションをループ処理して編集します。 1。PDF ファイルを保存します。

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)