إضافة معادلات رياضية إلى عروض PowerPoint التقديمية باستخدام Python

نظرة عامة

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

في PowerPoint، يضيف المستخدمون عادةً المعادلات من Insert > Equation:

علامة تبويب PowerPoint Insert مع تحديد أمر Equation

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

شريحة PowerPoint تحتوي على معادلة رياضية قابلة للتحرير

يبني Aspose.Slides ذلك النص الرياضي من خلال ثلاثة كائنات رئيسية:

  • شكل رياضي، يُنشأ باستخدام add_math_shape، وهو الشكل الذي يحتوي على المعادلة.
  • MathPortion يخزن محتوى الرياضيات داخل إطار نص الشكل.
  • MathParagraph يحتوي على كائن واحد أو أكثر من MathBlock.

تستخدم معظم الأمثلة أدناه MathematicalText وطرق السلسلة من IMathElement للحفاظ على الكود مختصرًا وسهل القراءة.

للحالات التي تتضمن تصدير MathML، راجع تصدير المعادلات الرياضية من العروض التقديمية في Python عبر .NET.

إنشاء معادلة

يوضح هذا المثال كيفية إنشاء شكل رياضي وإضافة نظرية فيثاغورس:

المعادلة c تربيع يساوي a تربيع زائد b تربيع

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)

إضافة كسور

استخدم divide لإنشاء كسر. يمكنك اختيار نمط الكسر باستخدام MathFractionTypes.

كسر رياضي مائل يُظهر 1 مقسومًا على x

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 لإنشاء جذر تربيعي، جذر مكعب، أو جذر من نوع آخر. يصبح العنصر الحالي هو الأساس، وتصبح الوسيطة هي الدرجة.

تعبير جذر من الدرجة n مع x تحت علامة الجذر

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.

حد x عندما يقترب x من اللانهاية

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 عندما تحتاج إلى معادلات محاذاة أو مجموعة رأسية من التعبيرات.

مصفوفة رياضية عمودية مع x فوق y

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 عندما يكون الوسيط هو العنصر الحالي ويكون اسم الدالة معروفًا.

الدالة المثلثية cos مطبقة على 2x

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.

حرف Y كبير مع أسفلية 1 على الجانب الأيسر وفوقية n

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 لوضع تعبير داخل محددات. يمكنك أيضًا تعيين حرف فاصل لتعبيرات المحدد التي تحتوي على عدة عناصر.

تعبير محدد يحتوي على x و y و z مفصول بأشرطة عمودية

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 عندما يجب إطارة المعادلة نفسها.

معادلة محاطة بإطار تُظهر a تربيع يساوي b تربيع زائد c تربيع

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 لوضع حرف تجميع فوق أو أسفل تعبير. أضف حدًا لتسمية المصطلحات المجمعة.

التعبير x زائد y مجمع مع تسمية أي نص تحته

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 يضع شريطًا فوق عنصر رياضي.

تعبير رياضي ABC مع شريط فوقه

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)

مرجع سريع

Task Main API
إنشاء نص رياضي 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 قابل للتحرير.

هل يمكنني تصدير المعادلات إلى LaTeX؟

نعم. احصل على الـ MathParagraph من الـ MathPortion، ثم استدعِ MathParagraph.to_latex لتصديرها مباشرة. لمثال كامل، راجع تصدير المعادلات الرياضية من العروض التقديمية في Python عبر .NET.