استبدال صيغة النص

Contents
[ ]

تسمح التعبيرات العادية باستبدال النص المرن استنادًا إلى الأنماط بدلاً من السلاسل الثابتة. من خلال تمكين دعم regex في «replace_text_strategy»، يمكنك مطابقة المحتوى الديناميكي مثل الأرقام أو التواريخ أو السلاسل المنسقة.

  1. قم بإنشاء محرر محتوى PDF مثال.
  2. قم بربط وثيقة PDF المدخلة.
  3. قم بتكوين إستراتيجية الاستبدال لاستخدام regex.
  4. استبدل الأنماط المطابقة عبر المستند بأكمله.
  5. احفظ مستند 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)