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

Tổng quan

PowerPoint lưu trữ các phương trình dưới dạng Office Math Markup Language (OMML). Với Aspose.Slides cho Python thông qua Java, 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ố, gố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 phương trình 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 một phương trình toán học có thể chỉnh sửa

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

  • Một hình toán học, được tạo bằng addMathShape, là hình chứa phương trình.
  • 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ụ dưới đây sử dụng MathematicalText và các phương thức chuỗi từ MathElementBase để giữ 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.

Tạo một Phương trình

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

Phương trình c bình phương bằng a bình phương cộng b bình phương

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    a_squared = MathematicalText("a").setSuperscript("2")
    b_squared = MathematicalText("b").setSuperscript("2")
    equation = MathematicalText("c").setSuperscript("2").join("=").join(a_squared).join("+").join(b_squared)

    math_paragraph.add(equation)

    presentation.save("pythagorean-theorem.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Phân Số

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ố toán học nghiêng hiển thị một chia cho x

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathFractionTypes, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    fraction = MathematicalText("1").divide("x", MathFractionTypes.Skewed)

    math_block = MathBlock(fraction)
    math_paragraph.add(math_block)

    presentation.save("fraction.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Đối với phân số dạng chồng, sử dụng MathFractionTypes.Bar:

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathFractionTypes, MathematicalText

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

Thêm Gốc

Sử dụng radical để tạo căn bậc hai, bậc ba hoặc các căn 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 gốc bậc n với x nằm dưới dấu căn

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

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

    math_block = MathBlock(radical)
    math_paragraph.add(math_block)

    presentation.save("radical.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Hàm và Giới Hạn

Sử dụng asArgumentOfFunction 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 sử dụng setLowerLimit.

Giới hạn của x khi x tiến tới vô cực

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    limit = MathematicalText("lim").setLowerLimit("x\u2192\u221E").function("x")

    math_block = MathBlock(limit)
    math_paragraph.add(math_block)

    presentation.save("functions-and-limits.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathematicalText

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

Thêm Toán tử N-ary và Tích Phân

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

Một ký hiệu tổng có giới hạn dưới và trên

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathNaryOperatorTypes, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    a_power = MathematicalText("a").setSuperscript("n-k")
    summation_base = MathematicalText("x").setSuperscript("k").join(a_power)

    summation = summation_base.nary(MathNaryOperatorTypes.Summation, "k=0", "n")

    math_block = MathBlock(summation)
    math_paragraph.add(math_block)

    presentation.save("nary-operators.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathIntegralTypes, MathematicalText

differential = MathematicalText("dx").toBox()
integral_base = MathematicalText("x").join(differential)
integral = integral_base.integral(MathIntegralTypes.Simple, "0", "1")

Thêm Ma Trận

Sử dụng MathMatrix cho các hàng và cột. Mặc định ma trận không bao gồm dấu ngoặc, vì vậy cần 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 jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathMatrix, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    matrix = MathMatrix(2, 3)
    cell_0_0 = MathematicalText("1")
    matrix.set_Item(0, 0, cell_0_0)
    cell_0_1 = MathematicalText("x")
    matrix.set_Item(0, 1, cell_0_1)
    cell_1_0 = MathematicalText("x")
    matrix.set_Item(1, 0, cell_1_0)
    cell_1_1 = MathematicalText("2")
    matrix.set_Item(1, 1, cell_1_1)
    cell_1_2 = MathematicalText("y")
    matrix.set_Item(1, 2, cell_1_2)

    math_block = MathBlock(matrix)
    math_paragraph.add(math_block)

    presentation.save("matrix.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Mảng Phương Trình

Sử dụng toMathArray khi bạn cần các phương trình căn chỉnh hoặc một dải dọc các biểu thức.

Một mảng toán học dọc với x ở trên y

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 140)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    equation_array = MathematicalText("x").join("y").toMathArray()

    math_block = MathBlock(equation_array)
    math_paragraph.add(math_block)

    presentation.save("equation-array.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Hàm Lượng Giác

Sử dụng asArgumentOfFunction 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 jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathFunctionsOfOneArgument, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    cosine = MathematicalText("2x").asArgumentOfFunction(MathFunctionsOfOneArgument.Cos)

    math_block = MathBlock(cosine)
    math_paragraph.add(math_block)

    presentation.save("trigonometric-function.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Chỉ Số và Lũy Thừa

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

Chữ Y in hoa với chỉ số dưới trái 1 và lũy thừa trên trái n

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    scripts = MathematicalText("Y").setSubSuperscriptOnTheLeft("1", "n")

    math_block = MathBlock(scripts)
    math_paragraph.add(math_block)

    presentation.save("subscript-superscript.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Dấu Ngăn

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

Biểu thức dấu ngăn chứa x, y và z được phân tách bằng các thanh dọc

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    delimiter = MathematicalText("x").join("y").join("z").enclose('<', '>')
    delimiter.setSeparatorCharacter('|')

    math_block = MathBlock(delimiter)
    math_paragraph.add(math_block)

    presentation.save("delimiters.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Thêm Hộp Đánh Giáp

Sử dụng toBorderBox khi phương trình cần được bao khung.

Phương trình trong hộp cho thấy a bình phương bằng b bình phương cộng c bình phương

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    b_squared = MathematicalText("b").setSuperscript("2")
    c_squared = MathematicalText("c").setSuperscript("2")
    boxed_equation = MathematicalText("a").setSuperscript("2").join("=").join(b_squared).join("+").join(c_squared).toBorderBox()

    math_block = MathBlock(boxed_equation)
    math_paragraph.add(math_block)

    presentation.save("border-box.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Nhóm Các Thuật Ngữ

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

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

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathTopBotPositions, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 120)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

    grouped = MathematicalText("x + y").group('\u23DF', MathTopBotPositions.Bottom, MathTopBotPositions.Top).setLowerLimit("any text")

    math_block = MathBlock(grouped)
    math_paragraph.add(math_block)

    presentation.save("grouped-terms.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

Định Dạng Các Phần Tử Toán Học

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

Biểu thức toán học ABC có một thanh ngang phía trên

import jpype
import asposeslides

if not jpype.isJVMStarted():
    jpype.startJVM()

from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat

presentation = Presentation()
try:
    slide = presentation.getSlides().get_Item(0)

    math_shape = slide.getShapes().addMathShape(20, 20, 700, 100)
    math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()

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

    math_block = MathBlock(overbar)
    math_paragraph.add(math_block)

    presentation.save("overbar.pptx", SaveFormat.Pptx)
finally:
    presentation.dispose()

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ử MathElementBase.join
Tạo phân số MathElementBase.divide
Thêm chỉ số trên hoặc dưới setSuperscript, setSubscript
Thêm hàm function, asArgumentOfFunction
Thêm gốc MathElementBase.radical
Thêm giới hạn setLowerLimit, setUpperLimit
Thêm chỉ số bên trái setSubSuperscriptOnTheLeft
Thêm tổng và tích phân nary, integral
Thêm ma trận MathMatrix
Thêm mảng phương trình toMathArray
Thêm dấu ngăn enclose
Thêm thanh và khung overbar, toBorderBox
Nhóm các thuật ngữ group

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

Tôi có thể chỉnh sửa một phương trình PowerPoint hiện có không?

Có. Mở bản trình chiếu, tìm hình chứa một 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 phương trình 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 phương trình dưới dạng nội dung Office Math có thể chỉnh sửa.

Tôi có thể xuất phương trình sang LaTeX không?

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