تنسيق نص العرض التقديمي في بايثون
نظرة عامة
توضح هذه المقالة كيفية تنسيق النص في عروض PowerPoint وOpenDocument التقديمية باستخدام Aspose.Slides للغة Python عبر .NET. تغطي إبراز النص، ألوان الخلفية، الشفافية، تباعد الأحرف، خصائص الخط، الدوران، مسافات الفقرات، سلوك التحجيم التلقائي، تثبيت النص، نقاط التبويب، وإعدادات اللغة.
في الأمثلة أدناه، سنستخدم ملفًا اسمه “sample.pptx” يحتوي على مربع نص واحد في الشريحة الأولى بالنص التالي:

إبراز النص
استخدم طريقة TextFrame.highlight_text عندما تحتاج إلى إبراز النص الذي يطابق عينة محددة داخل إطار نص. تُطبق الطريقة لون إبراز على مقاطع النص المطابقة ويمكن استخدامها مع TextSearchOptions للتحكم في طريقة إجراء البحث، على سبيل المثال، لمطابقة الكلمات الكاملة فقط.
يوضح المثال البرمجي أدناه إبراز جميع مرات ظهور الحرفين “try” ثم إبراز الكلمة الكاملة “to” فقط.
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
# احصل على الشكل الأول من الشريحة الأولى.
shape = presentation.slides[0].shapes[0]
# إبراز الكلمة "try" في الشكل.
shape.text_frame.highlight_text("try", draw.Color.light_blue)
search_options = slides.TextSearchOptions()
search_options.whole_words_only = True
# إبراز الكلمة "to" في الشكل.
shape.text_frame.highlight_text("to", draw.Color.violet, search_options, None)
presentation.save("highlighted_text.pptx", slides.export.SaveFormat.PPTX)

إبراز النص باستخدام التعبيرات النمطية
طريقة TextFrame.highlight_regex تُبرز التطابقات النصية التي يتم العثور عليها عن طريق تعبير نمطي. في Python، تُعرض هذه الواجهة البرمجية على TextFrame.
يوضح المثال البرمجي أدناه إبراز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
regex = r"\b[^\s]{7,}\b"
# إبراز جميع الكلمات التي تحتوي على سبعة أحرف أو أكثر.
shape.text_frame.highlight_regex(regex, draw.Color.yellow, None)
presentation.save("highlighted_text_using_regex.pptx", slides.export.SaveFormat.PPTX)

تعيين لون خلفية النص
استخدم ParagraphFormat.default_portion_format لتحديد لون الإبراز الافتراضي لفقرة، أو استخدم PortionFormat.highlight_color لأجزاء النص الفردية.
الكود التالي يوضح كيفية تعيين لون الخلفية للفقرة كاملة:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# تعيين لون الإبراز للفقرة بالكامل.
paragraph.paragraph_format.default_portion_format.highlight_color.color = draw.Color.light_gray
presentation.save("gray_paragraph.pptx", slides.export.SaveFormat.PPTX)

يوضح المثال البرمجي أدناه كيفية تعيين لون الخلفية لأجزاء النص ذات الخط العريض:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# تعيين لون الإبراز لجزء النص.
portion.portion_format.highlight_color.color = draw.Color.light_gray
presentation.save("gray_text_portions.pptx", slides.export.SaveFormat.PPTX)

محاذاة فقرات النص
استخدم ParagraphFormat.alignment لتعيين محاذاة الفقرة داخل إطار النص. يمكن أن تكون القيمة مركزية، محاذاة إلى اليسار، محاذاة إلى اليمين، مبررة، وما إلى ذلك.
يوضح الكود التالي كيفية محاذاة الفقرة إلى الوسط:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# تعيين محاذاة الفقرة إلى الوسط.
paragraph.paragraph_format.alignment = slides.TextAlignment.CENTER
presentation.save("aligned_paragraph.pptx", slides.export.SaveFormat.PPTX)

تعيين الشفافية للنص
تُتحكم شفافية النص من خلال المكوّن ألفا للون المخصص إلى PortionFormat.fill_format. في الأمثلة أدناه، alpha = 50 هو قيمة قناة ألفا بنظام ARGB على مقياس 0-255، وليس نسبة شفافية.
يوضح المثال البرمجي أدناه كيفية تطبيق الشفافية على الفقرة كاملة:
import aspose.pydrawing as draw
import aspose.slides as slides
alpha = 50
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# تعيين لون تعبئة النص إلى لون شفاف.
paragraph.paragraph_format.default_portion_format.fill_format.fill_type = slides.FillType.SOLID
paragraph.paragraph_format.default_portion_format.fill_format.solid_fill_color.color = draw.Color.from_argb(alpha, draw.Color.black)
presentation.save("transparent_paragraph.pptx", slides.export.SaveFormat.PPTX)

يوضح المثال البرمجي التالي كيفية تطبيق الشفافية على أجزاء النص ذات الخط العريض:
import aspose.pydrawing as draw
import aspose.slides as slides
alpha = 50
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# تعيين شفافية جزء النص.
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.from_argb(alpha, draw.Color.black)
presentation.save("transparent_text_portions.pptx", slides.export.SaveFormat.PPTX)

تعيين تباعد الأحرف للنص
استخدم BasePortionFormat.spacing لتوسيع أو تقليل التباعد بين الأحرف في مربع نص.
الكود البايثوني التالي يوضح كيفية توسيع تباعد الأحرف في الفقرة كاملة:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# ملاحظة: استخدم القيم السلبية لتقليل تباعد الأحرف.
paragraph.paragraph_format.default_portion_format.spacing = 3 # توسيع تباعد الأحرف.
presentation.save("character_spacing_in_paragraph.pptx", slides.export.SaveFormat.PPTX)

يوضح المثال البرمجي أدناه كيفية توسيع تباعد الأحرف في أجزاء النص ذات الخط العريض:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# ملاحظة: استخدم القيم السلبية لتقليل تباعد الأحرف.
portion.portion_format.spacing = 3 # توسيع تباعد الأحرف.
presentation.save("character_spacing_in_text_portions.pptx", slides.export.SaveFormat.PPTX)

تعطيل التجانس (Kerning) لخطوط محددة
في بعض الحالات، قد يبدو النص الذي تُنتجه Aspose.Slides ضيقًا قليلاً مقارنةً بالنص نفسه المعروض في PowerPoint. يمكن أن يحدث هذا لأن PowerPoint قد يتجاهل بيانات التجانس لبعض الخطوط، حتى عندما يحتوي الخط على معلومات تجانس صالحة ويتم تمكين التجانس في إعدادات PowerPoint.
لجعل المخرجات المُنتجة أقرب إلى ما في PowerPoint في مثل هذه الحالات، يمكنك تعطيل التجانس لأجزاء النص التي تستخدم الخط المتأثر. ضع PortionFormat.kerning_minimal_size إلى قيمة أكبر بكثير من حجم الخط الفعلي:
import aspose.slides as slides
with slides.Presentation("presentation.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
target_font = "Roboto"
for paragraph in auto_shape.text_frame.paragraphs:
for portion in paragraph.portions:
latin_font = portion.portion_format.latin_font
east_asian_font = portion.portion_format.east_asian_font
complex_script_font = portion.portion_format.complex_script_font
if ((latin_font is not None and latin_font.font_name == target_font) or
(east_asian_font is not None and east_asian_font.font_name == target_font) or
(complex_script_font is not None and complex_script_font.font_name == target_font)):
portion.portion_format.kerning_minimal_size = 100
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
هذا الإعداد يمنع تطبيق التجانس على أجزاء النص المطابقة ويمكن أن يساعد في محاذاة عرض Aspose.Slides مع المخرجات البصرية لـ PowerPoint للخطوط المتأثرة بهذا السلوك الخاص بـ PowerPoint.
إدارة خصائص خط النص
يمكن تعيين خصائص الخط على مستوى الفقرة عبر ParagraphFormat.default_portion_format، أو على الأجزاء الفردية عبر PortionFormat.
الكود التالي يحدد الخط ونمط النص للفقرة بأكملها: يطبق حجم الخط، العريض، المائل، التسطير المتقطّع، وخط Times New Roman على جميع الأجزاء في الفقرة.
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
# تعيين خصائص الخط للفقرة.
paragraph.paragraph_format.default_portion_format.font_height = 12
paragraph.paragraph_format.default_portion_format.font_bold = slides.NullableBool.TRUE
paragraph.paragraph_format.default_portion_format.font_italic = slides.NullableBool.TRUE
paragraph.paragraph_format.default_portion_format.font_underline = slides.TextUnderlineType.DOTTED
paragraph.paragraph_format.default_portion_format.latin_font = slides.FontData("Times New Roman")
presentation.save("font_properties_for_paragraph.pptx", slides.export.SaveFormat.PPTX)

يوضح المثال البرمجي أدناه تطبيق خصائص مماثلة على أجزاء النص ذات الخط العريض:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
for portion in paragraph.portions:
if portion.portion_format.get_effective().font_bold:
# تعيين خصائص الخط لجزء النص.
portion.portion_format.font_height = 13
portion.portion_format.font_italic = slides.NullableBool.TRUE
portion.portion_format.font_underline = slides.TextUnderlineType.DOTTED
portion.portion_format.latin_font = slides.FontData("Times New Roman")
presentation.save("font_properties_for_text_portions.pptx", slides.export.SaveFormat.PPTX)

تعيين دوران النص
استخدم TextFrameFormat.text_vertical_type لتعيين توجيه نص مسبق داخل شكل.
يوضح الكود التالي تعيين توجيه النص داخل الشكل إلى VERTICAL270، والذي يدور النص 90 درجة عكس عقرب الساعة:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
auto_shape.text_frame.text_frame_format.text_vertical_type = slides.TextVerticalType.VERTICAL270
presentation.save("text_rotation.pptx", slides.export.SaveFormat.PPTX)

تعيين دوران مخصص لإطارات النص
استخدم TextFrameFormat.rotation_angle لتحديد زاوية دوران مخصصة لإطار TextFrame.
يعرض المثال البرمجي التالي تدوير إطار النص بمقدار 3 درجات باتجاه عقرب الساعة داخل الشكل:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
auto_shape.text_frame.text_frame_format.rotation_angle = 3
presentation.save("custom_text_rotation.pptx", slides.export.SaveFormat.PPTX)

تعيين تباعد الأسطر للفقرات
توفر Aspose.Slides الخصائص ParagraphFormat.space_after، ParagraphFormat.space_before، وParagraphFormat.space_within للتحكم في تباعد الفقرات. تُستخدم هذه الخصائص على النحو التالي:
- استخدم قيمة إيجابية لتحديد تباعد السطر كنسبة مئوية من ارتفاع السطر.
- استخدم قيمة سلبية لتحديد تباعد السطر بالنقاط.
يوضح الكود التالي كيفية تحديد تباعد السطر داخل الفقرة:
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.space_within = 200
presentation.save("line_spacing.pptx", slides.export.SaveFormat.PPTX)

تعيين نوع التحجيم التلقائي لإطارات النص
TextFrameFormat.autofit_type يحدد كيفية تصرف النص عندما يتجاوز حدود الحاوية الخاصة به. استخدمه للتحكم فيما إذا كان النص سيُصغر، سيتدفق، أو سيُعيد تحجيم الشكل تلقائيًا.
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
auto_shape.text_frame.text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
presentation.save("autofit_type.pptx", slides.export.SaveFormat.PPTX)
تعيين تثبيت إطارات النص
TextFrameFormat.anchoring_type يحدد كيفية تمركز النص عموديًا داخل الشكل، على سبيل المثال في الأعلى، الوسط، أو الأسفل.
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
auto_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.BOTTOM
presentation.save("text_anchor.pptx", slides.export.SaveFormat.PPTX)
تعيين تبويب النص
استخدم ParagraphFormat.default_tab_size وParagraphFormat.tabs لتكوين نقاط التبويب في فقرة.
import aspose.slides as slides
with slides.Presentation("sample.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.default_tab_size = 100
paragraph.paragraph_format.tabs.add(30, slides.TabAlignment.LEFT)
presentation.save("paragraph_tabs.pptx", slides.export.SaveFormat.PPTX)

تعيين لغة التدقيق
توفر Aspose.Slides الخاصية PortionFormat.language_id، والتي تسمح لك بتعيين لغة التدقيق لجزء النص. تحدد لغة التدقيق اللغة المستخدمة لتدقيق الإملاء والقواعد في PowerPoint.
يوضح المثال البرمجي التالي كيفية تعيين لغة التدقيق لجزء نص:
import aspose.slides as slides
with slides.Presentation("presentation.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
paragraph = auto_shape.text_frame.paragraphs[0]
paragraph.portions.clear()
font = slides.FontData("SimSun")
text_portion = slides.Portion()
text_portion.portion_format.complex_script_font = font
text_portion.portion_format.east_asian_font = font
text_portion.portion_format.latin_font = font
# تعيين معرف لغة التدقيق.
text_portion.portion_format.language_id = "zh-CN"
text_portion.text = "1."
paragraph.portions.add(text_portion)
presentation.save("proofing_language.pptx", slides.export.SaveFormat.PPTX)
تعيين اللغة الافتراضية
استخدم LoadOptions.default_text_language لتحديد اللغة الافتراضية للنص الذي يُنشأ أثناء تحميل أو إنشاء عرض تقديمي.
import aspose.slides as slides
load_options = slides.LoadOptions()
load_options.default_text_language = "en-US"
with slides.Presentation(load_options) as presentation:
slide = presentation.slides[0]
# إضافة شكل مستطيل جديد مع النص.
shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20, 20, 150, 50)
shape.text_frame.text = "Sample text"
# التحقق من لغة الجزء الأول.
portion = shape.text_frame.paragraphs[0].portions[0]
print(portion.portion_format.language_id)
تعيين نمط النص الافتراضي
لتطبيق تنسيق النص الافتراضي على مستوى العرض التقديمي، استخدم Presentation.default_text_style.
يوضح المثال البرمجي التالي كيفية تعيين خط عريض افتراضي بحجم 14 نقطة لجميع النصوص عبر الشرائح في عرض تقديمي جديد.
import aspose.slides as slides
with slides.Presentation() as presentation:
# الحصول على تنسيق الفقرة من المستوى الأعلى.
paragraph_format = presentation.default_text_style.get_level(0)
if paragraph_format is not None:
paragraph_format.default_portion_format.font_height = 14
paragraph_format.default_portion_format.font_bold = slides.NullableBool.TRUE
presentation.save("default_text_style.pptx", slides.export.SaveFormat.PPTX)
استخلاص النص مع تأثير الأحرف الكبيرة (All-Caps)
في PowerPoint، تطبيق تأثير All Caps على الخط يجعل النص يظهر بأحرف كبيرة على الشريحة حتى لو كُتب أصلاً بأحرف صغيرة. عند استخراج مثل هذا الجزء النصي باستخدام Aspose.Slides، تُعيد المكتبة النص كما تم إدخاله بالضبط. لمطابقة النص المعروض، تحقق من TextCapType وحوِّل السلسلة المسترجعة إلى أحرف كبيرة عندما تكون القيمة ALL.
لنفترض أن لدينا مربع النص التالي في الشريحة الأولى من ملف sample2.pptx.

يوضح المثال البرمجي أدناه كيفية استخراج النص مع تطبيق تأثير All Caps:
import aspose.slides as slides
with slides.Presentation("sample2.pptx") as presentation:
auto_shape = presentation.slides[0].shapes[0]
text_portion = auto_shape.text_frame.paragraphs[0].portions[0]
print("Original text:", text_portion.text)
text_format = text_portion.portion_format.get_effective()
if text_format.text_cap_type == slides.TextCapType.ALL:
text = text_portion.text.upper()
print("All-Caps effect:", text)
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
الأسئلة الشائعة
كيف يمكن تعديل النص في جدول على شريحة؟
لتعديل النص في جدول على شريحة، استخدم Table. قم بالتكرار عبر الخلايا وحدث كل خلية عبر Cell.text_frame وتنسيق الفقرة عبر Paragraph.paragraph_format.
كيف يمكن تطبيق لون تدرج على النص في شريحة PowerPoint؟
لتطبيق لون تدرج على النص، استخدم PortionFormat.fill_format. ضع FillFormat.fill_type إلى FillType.GRADIENT وكونّ نقاط التدرج، الاتجاه، والشفافية.