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:

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

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:

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()
Note
addMathShape tạo một hình đã chứa một đoạn MathParagraph. Truy cập phần tử MathPortion đầu tiên, lấy MathParagraph của nó, và thêm các khối hoặc phần tử toán học vào đó.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.

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.

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.

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.

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ư +, - và = 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.

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.

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.

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.

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ử.

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.

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.

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.

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.