Replace Text Regex
Contents
[
Hide
]
Regular expressions allow flexible text replacement based on patterns instead of fixed strings. By enabling regex support in ‘replace_text_strategy’, you can match dynamic content such as numbers, dates, or formatted strings.
- Create a PdfContentEditor instance.
- Bind the input PDF document.
- Configure replacement strategy to use regex.
- Replace matching patterns across the entire document.
- Save the updated PDF document.
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)