テキスト正規表現を置換
Contents
[
Hide
]
正規表現を使うと、固定文字列の代わりにパターンに基づいて柔軟にテキストを置換できます。‘replace_text_strategy ‘で正規表現サポートを有効にすることで、数値、日付、フォーマットされた文字列などの動的コンテンツを照合できます。
- を作成 PDF コンテンツエディター インスタンス。
- 入力 PDF ドキュメントをバインドします。
- 正規表現を使用するように置換ストラテジーを設定します。
- 文書全体で一致するパターンを置き換えます。
- 更新した PDF ドキュメントを保存します。
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
def replace_text_regex(infile, outfile):
# Create PdfContentEditor object
content_editor = pdf_facades.PdfContentEditor()
# Bind document to PdfContentEditor
content_editor.bind_pdf(infile)
# Replace text in the whole document
content_editor.replace_text_strategy.replace_scope = (
pdf_facades.ReplaceTextStrategy.Scope.REPLACE_ALL
)
content_editor.replace_text_strategy.is_regular_expression_used = True
content_editor.replace_text(r"\d{4}", "[NUMBER]")
# Save updated document
content_editor.save(outfile)