Python を使って PDF を編集

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

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

PDF ファイルを編集するために、python-net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作 API である Aspose.PDF for .NET API を使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Python 経由でPDFドキュメントを墨消し


お使いの環境でコードを試すには Aspose.PDF for .NET が必要です。

  1. PDF を Document のインスタンスとともに読み込みます。
  2. 検索用語を引数として TextFragmentAbsorber オブジェクトを作成します。
  3. [検索オプション] を設定します。
  4. 各フラグメントコレクションをループして編集します。
  5. 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)