إضافة معادلات رياضية إلى عروض PowerPoint التقديمية باستخدام Python
نظرة عامة
PowerPoint يخزن المعادلات بصيغة Office Math Markup Language (OMML). باستخدام Aspose.Slides for Python عبر .NET، يمكنك إنشاء نفس نوع المحتوى الرياضي برمجياً: الكسور، الجذور، الدوال، الحدود، العوامل N-ary، المصفوفات، المصفوفات المتعددة، وكتل الرياضيات المنسقة.
في PowerPoint، يضيف المستخدمون عادة المعادلات من إدراج > معادلة:

النتيجة هي نص رياضي قابل للتحرير على الشريحة:

Aspose.Slides يبني ذلك النص الرياضي من خلال ثلاثة كائنات رئيسية:
- شكل رياضي، يتم إنشاؤه باستخدام add_math_shape، هو الشكل الذي يحتوي على المعادلة.
- MathPortion يخزن محتوى الرياضيات داخل إطار النص في الشكل.
- MathParagraph يحتوي على كائن واحد أو أكثر من MathBlock.
معظم الأمثلة أدناه تستخدم MathematicalText والطرق المتسلسلة من IMathElement لجعل الشيفرة قصيرة وسهلة القراءة.
لسيناريوهات تصدير MathML، راجع Export Math Equations from Presentations in Python via .NET.
إنشاء معادلة
هذا المثال ينشئ شكلاً رياضياً ويضيف نظرية فيثاغورس:

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation = (
math.MathematicalText("c")
.set_superscript("2")
.join("=")
.join(math.MathematicalText("a").set_superscript("2"))
.join("+")
.join(math.MathematicalText("b").set_superscript("2"))
)
math_paragraph.add(equation)
presentation.save("pythagorean-theorem.pptx", slides.export.SaveFormat.PPTX)
add_math_shape ينشئ شكلاً يحتوي مسبقاً على فقرة رياضية. احصل على أول MathPortion، ثم على MathParagraph الخاص به، وأضف كتل رياضية أو عناصر رياضية إليها.
إضافة كسور
استخدم divide لإنشاء كسر. يمكنك اختيار نمط الكسر باستخدام MathFractionTypes.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
fraction = math.MathematicalText("1").divide("x", math.MathFractionTypes.SKEWED)
math_paragraph.add(math.MathBlock(fraction))
presentation.save("fraction.pptx", slides.export.SaveFormat.PPTX)
لإنشاء كسر مكدس، استخدم MathFractionTypes.BAR:
stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)
إضافة جذور
استخدم radical لإنشاء جذر تربيعي، أو جذر مكعب، أو أي جذر آخر. العنصر الحالي يصبح الأساس، والوسيط يصبح الدرجة.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
radical = math.MathematicalText("x").radical("n")
math_paragraph.add(math.MathBlock(radical))
presentation.save("radical.pptx", slides.export.SaveFormat.PPTX)
إضافة الدوال والحدود
استخدم as_argument_of_function أو function للدوال مثل sin(x)، log(x)، أو أسماء دوال مخصصة. للحدود، ضع lim في MathLimit أو استخدم set_lower_limit.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
limit = (
math.MathematicalText("lim")
.set_lower_limit("x\u2192\u221E")
.function("x")
)
math_paragraph.add(math.MathBlock(limit))
presentation.save("functions-and-limits.pptx", slides.export.SaveFormat.PPTX)
لإعطاء اسم دالة مخصص، اجعل اسم الدالة هو العنصر الحالي:
custom_function = math.MathematicalText("f").function("x + 1")
إضافة عوامل N-ary والتكاملات
استخدم nary للجمعيات، الاتحادات، التقاطعات، وغيرها من العوامل الكبيرة. استخدم integral للتكاملات. كلا الطريقتين تسمحان بتحديد الحدود السفلية والعلوية.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
summation_base = (
math.MathematicalText("x")
.set_superscript("k")
.join(math.MathematicalText("a").set_superscript("n-k"))
)
summation = summation_base.nary(math.MathNaryOperatorTypes.SUMMATION, "k=0", "n")
math_paragraph.add(math.MathBlock(summation))
presentation.save("nary-operators.pptx", slides.export.SaveFormat.PPTX)
العوامل N-ary مخصصة للعوامل الكبيرة ذات حدود اختيارية. تُضاف العوامل البسيطة مثل +، -، و= عادةً كـ MathematicalText وتُربط في التعبير.
للتكامل، استخدم integral:
integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")
إضافة مصفوفات
استخدم MathMatrix للصفوف والأعمدة. المصفوفات لا تتضمن أقواسًا افتراضيًا، لذا احط المصفوفة بالأقواس أو الأقواس المربعة أو الأقواس المعقوفة حسب الحاجة.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
matrix = math.MathMatrix(2, 3)
matrix[0, 0] = math.MathematicalText("1")
matrix[0, 1] = math.MathematicalText("x")
matrix[1, 0] = math.MathematicalText("x")
matrix[1, 1] = math.MathematicalText("2")
matrix[1, 2] = math.MathematicalText("y")
math_paragraph.add(math.MathBlock(matrix))
presentation.save("matrix.pptx", slides.export.SaveFormat.PPTX)
إضافة مصفوفات المعادلات
استخدم to_math_array عندما تحتاج إلى معادلات محاذاة أو مجموعة رأسية من التعابير.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 140)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
equation_array = (
math.MathematicalText("x")
.join("y")
.to_math_array()
)
math_paragraph.add(math.MathBlock(equation_array))
presentation.save("equation-array.pptx", slides.export.SaveFormat.PPTX)
إضافة الدوال المثلثية
استخدم as_argument_of_function عندما يكون الوسيط هو العنصر الحالي ويكون اسم الدالة معروفًا.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
cosine = math.MathematicalText("2x").as_argument_of_function(
math.MathFunctionsOfOneArgument.COS
)
math_paragraph.add(math.MathBlock(cosine))
presentation.save("trigonometric-function.pptx", slides.export.SaveFormat.PPTX)
إضافة المؤشرات السفلية والعلوية
استخدم مساعدي المؤشر السفلي والعلوي للفهارس والقوى. عندما يجب أن تظهر الفهارس على الجانب الأيسر من الأساس، استخدم set_sub_superscript_on_the_left.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
scripts = math.MathematicalText("Y").set_sub_superscript_on_the_left("1", "n")
math_paragraph.add(math.MathBlock(scripts))
presentation.save("subscript-superscript.pptx", slides.export.SaveFormat.PPTX)
إضافة محددات
استخدم enclose لوضع تعبير داخل محددات. يمكنك أيضًا تعيين حرف فاصل لتعبيرات المحدد التي تحتوي على عدة عناصر.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
delimiter = (
math.MathematicalText("x")
.join("y")
.join("z")
.enclose("<", ">")
)
delimiter.separator_character = "|"
math_paragraph.add(math.MathBlock(delimiter))
presentation.save("delimiters.pptx", slides.export.SaveFormat.PPTX)
إضافة صندوق حدودي
استخدم to_border_box عندما يجب أن تكون المعادلة نفسها محاطة بإطار.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
boxed_equation = (
math.MathematicalText("a")
.set_superscript("2")
.join("=")
.join(math.MathematicalText("b").set_superscript("2"))
.join("+")
.join(math.MathematicalText("c").set_superscript("2"))
.to_border_box()
)
math_paragraph.add(math.MathBlock(boxed_equation))
presentation.save("border-box.pptx", slides.export.SaveFormat.PPTX)
تجميع المصطلحات
استخدم group لوضع حرف تجميع أعلى أو أسفل تعبير. أضف حدًا لتسمية المصطلحات المجمعة.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 120)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
grouped = (
math.MathematicalText("x + y")
.group(chr(0x23DF), math.MathTopBotPositions.BOTTOM, math.MathTopBotPositions.TOP)
.set_lower_limit("any text")
)
math_paragraph.add(math.MathBlock(grouped))
presentation.save("grouped-terms.pptx", slides.export.SaveFormat.PPTX)
تنسيق عناصر الرياضيات
استخدم مساعدات التنسيق فقط حيث توضح الصيغة. على سبيل المثال، overbar يضع شريطًا فوق عنصر رياضي.

import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
math_shape = slide.shapes.add_math_shape(20, 20, 700, 100)
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
overbar = math.MathematicalText("ABC").overbar()
math_paragraph.add(math.MathBlock(overbar))
presentation.save("overbar.pptx", slides.export.SaveFormat.PPTX)
مرجع سريع
| المهمة | واجهة برمجة التطبيقات الأساسية |
|---|---|
| إنشاء نص رياضي | MathematicalText |
| دمج العناصر | IMathElement.join |
| إنشاء كسور | IMathElement.divide |
| إضافة أس أو أس سفلي | set_superscript, set_subscript |
| إضافة دوال | function, as_argument_of_function |
| إضافة جذور | radical |
| إضافة حدود | set_lower_limit, set_upper_limit |
| إضافة مؤشرات على الجانب الأيسر | set_sub_superscript_on_the_left |
| إضافة جمعيات وتكاملات | nary, integral |
| إضافة مصفوفات | MathMatrix |
| إضافة مصفوفات المعادلات | to_math_array |
| إضافة محددات | enclose |
| إضافة أشرطة وإطارات | overbar, to_border_box |
| تجميع المصطلحات | group |
الأسئلة الشائعة
هل يمكنني تعديل معادلة PowerPoint موجودة؟
نعم. افتح العرض التقديمي، ابحث عن الشكل الذي يحتوي على MathPortion، احصل على MathParagraph الخاص به، وقم بتحديث كتل الرياضيات في تلك الفقرة.
هل تُحفظ المعادلات كرياضيات PowerPoint قابلة للتحرير؟
نعم. عند حفظ الملف كـ PPTX، يكتب Aspose.Slides المعادلة كـ Office Math قابل للتحرير.
هل يمكنني تصدير المعادلات إلى LaTeX؟
Aspose.Slides يصدر المعادلات إلى MathML. إذا كنت تحتاج إلى LaTeX، قم أولاً بتصدير إلى MathML ثم حوّله إلى LaTeX باستخدام أداة تدعم صيغ LaTeX المستهدفة.