텍스트 정규식 바꾸기
Contents
[
Hide
]
정규 표현식을 사용하면 고정된 문자열 대신 패턴을 기반으로 유연한 텍스트 교체가 가능합니다.‘replace_text_strategy’에서 정규식 지원을 활성화하면 숫자, 날짜 또는 서식이 지정된 문자열과 같은 동적 콘텐츠를 일치시킬 수 있습니다.
- 만들기 PDF 콘텐츠 편집기 예.
- 입력 PDF 문서를 바인딩합니다.
- regex를 사용하도록 대체 전략을 구성합니다.
- 전체 문서에서 일치하는 패턴을 바꿉니다.
- 업데이트된 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)