Thêm Các Phương Trình Toán Học vào Bản Trình Chiếu PowerPoint bằng Python

Tổng quan

PowerPoint lưu các công thức dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides cho Python qua .NET, bạn có thể tạo các nội dung toán học tương tự một cách lập trình: phân số, căn bậc, hàm, giới hạn, toán tử N-ary, ma trận, mảng và các khối toán học được định dạng.

Trong PowerPoint, người dùng thường thêm công thức từ Insert > Equation:

Tab Insert của PowerPoint với lệnh Equation được chọn

Kết quả là văn bản toán học có thể chỉnh sửa trên slide:

Một slide PowerPoint chứa công thức toán học có thể chỉnh sửa

Aspose.Slides xây dựng văn bản toán học này thông qua ba đối tượng chính:

  • Một hình toán học, được tạo bằng add_math_shape, là hình chứa công thức.
  • MathPortion lưu trữ nội dung toán học trong khung văn bản của hình.
  • MathParagraph chứa một hoặc nhiều đối tượng MathBlock.

Hầu hết các ví dụ bên dưới sử dụng MathematicalText và các phương thức luồng từ IMathElement để giữ cho mã ngắn gọn và dễ đọc.

Đối với các kịch bản xuất MathML, xem Export Math Equations from Presentations in Python via .NET.

Tạo công thức

Ví dụ này tạo một hình toán học và thêm định lý Pythagoras:

Công thức c bình phương bằng a bình phương cộng b bình phương

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)

Thêm phân số

Dùng divide để tạo một phân số. Bạn có thể chọn kiểu phân số bằng MathFractionTypes.

Một phân số nghiêng hiển thị 1 chia cho 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)

Đối với phân số chồng, dùng MathFractionTypes.BAR:

stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)

Thêm căn bậc

Dùng radical để tạo căn bậc hai, căn bậc ba hoặc các căn bậc khác. Phần tử hiện tại trở thành cơ số, và đối số trở thành bậc.

Biểu thức căn bậc n với x nằm dưới dấu că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

    radical = math.MathematicalText("x").radical("n")

    math_paragraph.add(math.MathBlock(radical))

    presentation.save("radical.pptx", slides.export.SaveFormat.PPTX)

Thêm hàm và giới hạn

Dùng as_argument_of_function hoặc function cho các hàm như sin(x), log(x) hoặc tên hàm tùy chỉnh. Đối với giới hạn, đặt lim trong một MathLimit hoặc dùng set_lower_limit.

Giới hạn của x khi x tiến tới vô cự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

    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)

Đối với tên hàm tùy chỉnh, biến tên hàm thành phần tử hiện tại:

custom_function = math.MathematicalText("f").function("x + 1")

Thêm toán tử N-ary và tích phân

Dùng nary cho tổng, hợp, giao và các toán tử lớn khác. Dùng integral cho tích phân. Cả hai phương thức đều cho phép bạn đặt giới hạn dưới và trên.

Một tổng với giới hạn dưới và trê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, 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)

Toán tử N-ary dành cho các toán tử lớn có tùy chọn giới hạn. Các toán tử đơn giản như +, -= thường được thêm dưới dạng MathematicalText và nối vào biểu thức.

Đối với tích phân, dùng integral:

integral_base = math.MathematicalText("x").join(math.MathematicalText("dx").to_box())
integral = integral_base.integral(math.MathIntegralTypes.SIMPLE, "0", "1")

Thêm ma trận

Dùng MathMatrix cho các hàng và cột. Mặc định ma trận không có dấu ngoặc, vì vậy hãy bao quanh ma trận khi bạn cần dấu ngoặc tròn, vuông hoặc nhọn.

Ma trận toán học hai hàng với một ô trống

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)

Thêm mảng công thức

Dùng to_math_array khi bạn cần các công thức căn chỉnh hoặc một chồng dọc các biểu thức.

Mảng toán học dọc với x ở trên 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)

Thêm hàm lượng giác

Dùng as_argument_of_function khi đối số là phần tử hiện tại và tên hàm đã biết.

Hàm lượng giác cos áp dụng cho 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)

Thêm chỉ số và chỉ số mũ

Sử dụng các trợ giúp chỉ số và chỉ số mũ cho các chỉ mục và lũy thừa. Khi các chỉ mục phải xuất hiện phía bên trái của cơ số, dùng set_sub_superscript_on_the_left.

Chữ Y in hoa với chỉ số dưới bên trái là 1 và chỉ số mũ 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)

Thêm dấu phân cách

Dùng enclose để đặt một biểu thức trong dấu phân cách. Bạn cũng có thể đặt ký tự phân tách cho các biểu thức dấu phân cách chứa nhiều phần tử.

Biểu thức dấu phân cách chứa x, y và z tách nhau bằng các thanh dọ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

    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)

Thêm khung viền

Dùng to_border_box khi công thức cần được đóng khung.

Công thức trong khung cho thấy a bình phương bằng b bình phương cộng c bình phương

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)

Nhóm các hạng tử

Dùng group để đặt một ký tự nhóm phía trên hoặc phía dưới một biểu thức. Thêm giới hạn để gán nhãn cho các hạng tử được nhóm.

Biểu thức x cộng y được nhóm với nhãn bất kỳ bên dưới

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)

Định dạng phần tử toán học

Chỉ sử dụng các trợ giúp định dạng khi chúng làm rõ công thức. Ví dụ, overbar đặt một gạch ngang phía trên phần tử toán học.

Biểu thức toán học ABC với một gạch ngang phía trê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

    overbar = math.MathematicalText("ABC").overbar()

    math_paragraph.add(math.MathBlock(overbar))

    presentation.save("overbar.pptx", slides.export.SaveFormat.PPTX)

Tham chiếu nhanh

Nhiệm vụ API chính
Tạo văn bản toán học MathematicalText
Kết hợp các phần tử IMathElement.join
Tạo phân số IMathElement.divide
Thêm chỉ số mũ hoặc chỉ số set_superscript, set_subscript
Thêm hàm function, as_argument_of_function
Thêm căn bậc radical
Thêm giới hạn set_lower_limit, set_upper_limit
Thêm chỉ số bên trái set_sub_superscript_on_the_left
Thêm tổng và tích phân nary, integral
Thêm ma trận MathMatrix
Thêm mảng công thức to_math_array
Thêm dấu phân cách enclose
Thêm gạch ngang và khung viền overbar, to_border_box
Nhóm các hạng tử group

Câu hỏi thường gặp

Tôi có thể chỉnh sửa một công thức PowerPoint hiện có không?

Có. Mở bản trình chiếu, tìm hình chứa MathPortion, lấy MathParagraph của nó và cập nhật các khối toán học trong đoạn đó.

Các công thức có được lưu dưới dạng toán học PowerPoint có thể chỉnh sửa không?

Có. Khi lưu dưới dạng PPTX, Aspose.Slides ghi công thức dưới dạng nội dung Office Math có thể chỉnh sửa.

Tôi có thể xuất công thức ra LaTeX không?

Có. Lấy MathParagraph của công thức từ MathPortion, và gọi MathParagraph.to_latex để xuất trực tiếp. Đối với ví dụ đầy đủ, xem Export Math Equations from Presentations in Python via .NET.