เพิ่มสมการคณิตศาสตร์ลงในงานนำเสนอ PowerPoint ด้วย Python

ภาพรวม

PowerPoint เก็บสมการเป็น Office Math Markup Language (OMML) ด้วย Aspose.Slides for Python via .NET คุณสามารถสร้างเนื้อหาคณิตศาสตร์ประเภทเดียวกันโดยโปรแกรมเม็ต: เศษส่วน, ราก, ฟังก์ชัน, ขีดจำกัด, ตัวดำเนินการ N-ary, เมทริกซ์, อาเรย์, และบล็อกคณิตศาสตร์ที่จัดรูปแบบ

ใน PowerPoint ผู้ใช้โดยทั่วไปเพิ่มสมการจาก Insert > Equation:

แท็บ Insert ของ PowerPoint พร้อมคำสั่ง Equation ที่เลือก

ผลลัพธ์คือข้อความคณิตศาสตร์ที่สามารถแก้ไขได้บนสไลด์:

สไลด์ PowerPoint ที่มีสมการคณิตศาสตร์ที่สามารถแก้ไขได้

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นผ่านสามอ็อบเจกต์หลัก:

  • รูปคณิตศาสตร์ที่สร้างด้วย add_math_shape, คือรูปที่ประกอบด้วยสมการ.
  • MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูป.
  • MathParagraph มีหนึ่งหรือหลายอ็อบเจกต์ MathBlock.

ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธอด fluent จาก IMathElement เพื่อให้โค้ดสั้นและอ่านง่าย.

สำหรับกรณีส่งออก MathML ดูที่ Export Math Equations from Presentations in Python via .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 Math ที่สามารถแก้ไขได้.

ฉันสามารถส่งออกรูปสมการเป็น LaTeX ได้หรือไม่?

ใช่ ดึง MathParagraph ของสมการจาก MathPortion, แล้วเรียก MathParagraph.to_latex เพื่อส่งออกโดยตรง สำหรับตัวอย่างครบถ้วน ดูที่ Export Math Equations from Presentations in Python via .NET.