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

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

Aspose.Slides สร้างข้อความคณิตศาสตร์นั้นโดยใช้สามอ็อบเจกต์หลัก:
- รูปร่างคณิตศาสตร์, สร้างด้วย add_math_shape, คือรูปร่างที่บรรจุสมการ
- MathPortion เก็บเนื้อหาคณิตศาสตร์ภายในกรอบข้อความของรูปร่าง
- MathParagraph ประกอบด้วยหนึ่งหรือหลายอ็อบเจกต์ MathBlock
ตัวอย่างส่วนใหญ่ด้านล่างใช้ MathematicalText และเมธอดเชิงลำดับจาก MathElementBase เพื่อทำให้โค้ดสั้นและอ่านง่าย
สำหรับสถานการณ์การส่งออก MathML ดูที่ Export Math Equations from Presentations in Python via .NET
สร้างสมการ
ตัวอย่างนี้ใช้ add_math_shape เพื่อสร้างรูปร่างคณิตศาสตร์และเพิ่มทฤษฎีพีทากอรัส:

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)
Note
add_math_shape สร้างรูปร่างที่มี MathParagraph อยู่แล้ว. เข้าถึง MathPortion ตัวแรก, ดึง math_paragraph, แล้วเพิ่ม MathBlock หรือ MathElement ลงไปเพิ่มเศษส่วน
ใช้ divide เพื่อสร้างเศษส่วน. คุณสามารถเลือกสไตล์ของเศษส่วนได้ด้วย MathFractionTypes

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:
import aspose.slides.mathtext as math
stacked_fraction = math.MathematicalText("x + 1").divide("y - 1", math.MathFractionTypes.BAR)
เพิ่มราก
ใช้ radical เพื่อสร้างรากกำลังสอง, รากกำลังสาม หรือรากอื่น ๆ. อิลิเมนต์ปัจจุบันจะเป็นฐาน, ส่วนอาร์กิวเมนต์จะเป็นดีกรี

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)
แสดงหรือซ่อนดีกรีของรากกำลังสอง
รากกำลังสองสามารถแสดงเลข 2 อย่างชัดเจนหรือใช้สัญลักษณ์ √x ตามปกติได้. property hide_degree ของ MathRadical ควบคุมรูปแบบที่แสดง. ทั้งสองรูปแบบจะรักษาดีกรี 2 และ radicand x ไว้ในสมการที่แก้ไขได้

ตัวอย่างจะบันทึกรากกำลังสองเดียวกันสองครั้ง. ดีกรี 2 ปรากฏใน visible-degree.pptx และถูกซ่อนใน hidden-degree.pptx.
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation() as presentation:
slide = presentation.slides[0]
radical = math.MathematicalText("x").radical("2")
shape = slide.shapes.add_math_shape(20, 20, 200, 100)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.math_paragraph.add(math.MathBlock(radical))
radical.hide_degree = False
presentation.save("visible-degree.pptx", slides.export.SaveFormat.PPTX)
radical.hide_degree = True
presentation.save("hidden-degree.pptx", slides.export.SaveFormat.PPTX)
เพิ่มฟังก์ชันและขีดจำกัด
ใช้ as_argument_of_function หรือ function สำหรับฟังก์ชันเช่น sin(x), log(x), หรือชื่อฟังก์ชันที่กำหนดเอง. สำหรับขีดจำกัด, ใส่ lim ใน MathLimit หรือใช้ set_lower_limit

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)
สำหรับชื่อฟังก์ชันที่กำหนดเอง, ทำให้ชื่อฟังก์ชันเป็นอิลิเมนต์ปัจจุบันและเรียก function พร้อมอาร์กิวเมนต์ของมัน:
import aspose.slides.mathtext as math
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:
import aspose.slides.mathtext as math
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)
แก้ไขเมทริกซ์ที่มีอยู่
สมมติว่าบทเรียนพีชคณิตเชิงเส้นเริ่มด้วยเมทริกซ์ค่าสัมประสิทธิ์ในไฟล์ matrix.pptx ที่บันทึกจากตัวอย่างก่อนหน้า. สำหรับแบบฝึกหัดต่อไป, เก็บสมการแรก, แทนที่สมการที่สองด้วยสมการใหม่สองสมการ, และปรับชุดตัวแปร. เข้าถึงเมทริกซ์ที่มีอยู่เป็น MathMatrix เพื่อรักษาค่าสัมประสิทธิ์ที่ยังใช้ได้

ดัชนีแถวและคอลัมน์เริ่มที่ศูนย์และอ้างอิงเมทริกซ์ตามสภาพในแต่ละการเรียก. การแทรกแถวก่อนดัชนี 1 จะย้ายแถวที่สองเดิมไปที่ดัชนี 2. การแทรกหลังดัชนี 2 จะเพิ่มแถวใหม่. การลบแถวที่ 2 จะลบสมการที่สองเดิมและย้ายแถวที่เพิ่มเข้ามาขึ้นหนึ่งตำแหน่ง. คอลัมน์จะเลื่อนในลักษณะเดียวกัน
import aspose.slides as slides
import aspose.slides.mathtext as math
with slides.Presentation("matrix.pptx") as presentation:
shape = presentation.slides[0].shapes[0]
portion = shape.text_frame.paragraphs[0].portions[0]
matrix = portion.math_paragraph[0][0]
matrix.insert_row_before(1)
matrix.insert_row_after(2)
matrix.delete_row(2)
matrix.insert_column_before(1)
matrix.insert_column_after(3)
matrix.delete_column(3)
matrix[1, 0] = math.MathematicalText("12")
matrix[1, 1] = math.MathematicalText("1")
matrix[2, 2] = math.MathematicalText("10")
matrix[2, 3] = math.MathematicalText("1")
for row in range(matrix.row_count):
for column in range(matrix.column_count):
if matrix[row, column] is None:
matrix[row, column] = math.MathematicalText("0")
matrix.set_column_alignment(0, math.MathHorizontalAlignment.LEFT)
matrix.set_columns_alignment(1, 3, math.MathHorizontalAlignment.RIGHT)
presentation.save("updated-matrix.pptx", slides.export.SaveFormat.PPTX)
set_column_alignment จัดแนวคอลัมน์แรกไปทางซ้าย. set_columns_alignment จัดแนวสามคอลัมน์ต่อเนื่องไปทางขวา, เริ่มจากดัชนี 1. ในไฟล์ updated-matrix.pptx, เมทริกซ์มีสามสมการและสี่ตัวแปร. ค่าสัมประสิทธิ์แถวแรกเดิม 1 และ x ปรากฏในคอลัมน์แรกและคอลัมน์ที่สาม

เพิ่มอาเรย์สมการ
ใช้ to_math_array เมื่อคุณต้องการสมการที่จัดแนวหรือกองแนวตั้งของนิพจน์

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)
จัดแนวอาเรย์กับอิลิเมนต์โดยรอบ
MathArray.base_justification ควบคุมการจัดแนวแนวตั้งของอาเรย์เทียบกับอิลิเมนต์คณิตศาสตร์ข้าง ๆ. ตัวอย่างด้านล่างเปรียบเทียบการจัดแนวกับแถวแรก, ศูนย์ของอาเรย์, และแถวสุดท้าย
ตัวอย่างต่อไปนี้สร้างอาเรย์สามอันที่เหมือนกัน, แต่ละอันมีสามแถวและอิลิเมนต์ข้างเคียง A= และ =B. ข้อความคณิตศาสตร์ทั้งหมดใช้ Cambria Math ขนาด 24 จุด. มีแต่การจัดแนวอาเรย์ที่เปลี่ยนแปลง
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(780, 200, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
alignments = [math.MathVerticalAlignment.TOP, math.MathVerticalAlignment.CENTER, math.MathVerticalAlignment.BOTTOM]
labels = ["Top", "Center", "Bottom"]
for i, alignment in enumerate(alignments):
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 20 + i * 260, 10, 240, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = labels[i]
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 16
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
math_shape = slide.shapes.add_math_shape(20 + i * 260, 60, 240, 130)
math_shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = math_shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = alignment
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save("equation-array-alignment.pptx", slides.export.SaveFormat.PPTX)
การเปรียบเทียบต่อไปนี้แสดงงานนำเสนอที่บันทึกไว้ใน PowerPoint. สังเกตตำแหน่งของ A= และ =B ข้างอาเรย์แต่ละอัน

กำหนดระยะห่างระหว่างแถว
row_spacing_rule เลือกกฎการเว้นระยะระหว่างแถวภายในอาเรย์. row_spacing ให้ค่าตัวเลขสำหรับกฎการเว้นระยะแบบเจาะจงและจะถูกละเลยโดยกฎที่กำหนดไว้ล่วงหน้า
สำหรับการเว้นระยะที่แม่นยำ, ค่าจะวัดเป็นจุด. สำหรับการเว้นระยะแบบสัดส่วน, จะวัดเป็นครึ่งบรรทัด: ค่า 3 ในตัวอย่างหมายถึง 1.5 บรรทัด. ตั้งค่าทั้งสองคุณสมบัติเมื่อระบุค่าที่เจาะจง; การเปลี่ยนค่าเชิงตัวเลขอย่างเดียวจะไม่เลือกกฎการเว้นระยะ
ตัวอย่างด้านล่างบันทึกงานนำเสนอห้าชุดที่มีสมการ, การจัดแนว, และการตั้งค่าฟอนต์เดียวกัน. สามชุดแรกใช้ค่ากำหนดล่วงหน้า; สองชุดสุดท้ายแสดงค่าที่เจาะจง
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
settings = [
(math.MathRowSpacingRule.SINGLE_LINE_GAP, 0),
(math.MathRowSpacingRule.ONE_AND_A_HALF_LINE_GAP, 0),
(math.MathRowSpacingRule.TWO_LINE_GAP, 0),
(math.MathRowSpacingRule.EXACTLY, 20),
(math.MathRowSpacingRule.MULTIPLE, 3)
]
for rule, spacing in settings:
file_name = f"equation-array-{rule.name}.pptx"
with slides.Presentation() as presentation:
math_shape = presentation.slides[0].shapes.add_math_shape(20, 20, 600, 300)
paragraph = math_shape.text_frame.paragraphs[0]
paragraph.paragraph_format.space_before = 0
paragraph.paragraph_format.space_after = 0
math_paragraph = paragraph.portions[0].math_paragraph
math_paragraph.justification = math.MathJustification.LEFT_JUSTIFIED
rows = [create_text("x+y=3"), create_text("x-y=1"), create_text("x=2")]
equation_array = math.MathArray(rows)
equation_array.base_justification = math.MathVerticalAlignment.CENTER
equation_array.row_spacing_rule = rule
equation_array.row_spacing = spacing
left_text = create_text("A=")
right_text = create_text("=B")
equation = math.MathBlock(left_text).join(equation_array).join(right_text)
math_paragraph.add(equation)
presentation.save(file_name, slides.export.SaveFormat.PPTX)
การเว้นระยะแถวของอาเรย์แยกจากการเว้นระยะของย่อหน้า. space_before และ space_after เพิ่มระยะรอบย่อหน้าที่บรรจุ, ไม่ได้อยู่ระหว่างแถวของอาเรย์. เช่นเดียวกัน, MathParagraph.justification กำหนดตำแหน่งของย่อหน้า Math ทั้งหมดในแนวนอน; มันไม่แทนค่าการจัดแนวแนวตั้งของอาเรย์
เพิ่มฟังก์ชันตรีโกณมิติ
ใช้ as_argument_of_function เมื่ออาร์กิวเมนต์เป็นอิลิเมนต์ปัจจุบันและชื่อฟังก์ชันทราบ

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

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 เพื่อใส่นิพจน์ภายในตัวคั่น. คุณยังสามารถตั้งอักขระคั่นสำหรับนิพจน์ที่มีหลายอิลิเมนต์ได้

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)
ขนาดและการจัดแนวตัวคั่น
ตั้งค่า grow_to_match_operand_height เป็น True เพื่อให้วงเล็บขยายตามความสูงของออพอแรนด์ที่สูง, หรือเป็น False เพื่อให้คงขนาดปกติ. delimiter_shape รับค่าหนึ่งในสองค่าของ MathDelimiterShape. CENTERED ทำให้วงเล็บอยู่ตรงกลางแกนคณิตศาสตร์, ซึ่งไหลผ่านจุดกึ่งกลางของเครื่องหมายเท่ากับ. MATCH จัดแนวตามเนื้อหาที่ห่อหุ้ม. ทั้งสองการตั้งค่ามีผลต่อเพียงตัวคั่น. นิพจน์ภายในคงเดิม
ตัวอย่างนี้ใส่เศษส่วนในเลขชี้กำลังของ x ทำให้นิพจน์ขยายขึ้นเหนือเส้นฐานมากกว่าที่อยู่ด้านล่าง. สมการสี่สมการใช้วงเล็บและการตั้งค่าฟอนต์เดียวกัน
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_expression(slide, left, top, grow, shape):
fraction = math.MathematicalText("a + b").divide("c + d")
power = math.MathematicalText("x").set_superscript(fraction)
delimiter = power.enclose("(", ")")
delimiter.grow_to_match_operand_height = grow
delimiter.delimiter_shape = shape
math_shape = slide.shapes.add_math_shape(left, top, 300, 100)
portion = math_shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 32
equation = math.MathematicalText("y = ").join(delimiter)
portion.math_paragraph.add(equation)
with slides.Presentation() as presentation:
slide = presentation.slides[0]
add_expression(slide, 20, 20, True, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 20, True, math.MathDelimiterShape.MATCH)
add_expression(slide, 20, 140, False, math.MathDelimiterShape.CENTERED)
add_expression(slide, 350, 140, False, math.MathDelimiterShape.MATCH)
presentation.save("delimiter-sizing.pptx", slides.export.SaveFormat.PPTX)
ใน PowerPoint, เปรียบเทียบแถวเพื่อดูผลของการขยาย. ในแถวบน, วงเล็บขยายเพื่อหุ้มเลขชี้กำลังและดูเหมือนกันสำหรับทั้งสองรูปแบบ. ในแถวล่าง, การขยายปิด. วงเล็บเล็กกว่าด้านซ้าย (CENTERED) จัดแนวกับเครื่องหมายเท่ากับ, ขณะที่ด้านขวา (MATCH) อยู่สูงกว่าเหนือนิพจน์

เพิ่มกรอบเส้นขอบ
เรียกใช้ to_border_box บนเศษส่วนเพื่อทำกรอบรอบนิพจน์ทั้งหมด. MathBorderBox ที่คืนค่ามาแสดงทั้งสี่ด้านโดยค่าเริ่มต้น. ใช้ hide_top, hide_bottom, hide_left, และ hide_right เพื่อลบด้านแต่ละด้าน
ตัวอย่างนี้ใส่สำเนาเศษส่วนเดียวกันหกชิ้นบนสไลด์เดียว. เพื่อแสดงการลบ, ซ่อนสี่ด้านทั้งหมดและตั้งค่า strikethrough_bottom_left_to_top_right สำหรับเส้นทแยงมุ่งขึ้น (↗) หรือ strikethrough_top_left_to_bottom_right สำหรับเส้นทแยงมุ่งลง (↘). คุณยังสามารถใช้ strikethrough_horizontal หรือ strikethrough_vertical ร่วมกับการแสดงด้านใดด้านหนึ่ง
import aspose.slides as slides
import aspose.slides.mathtext as math
def add_fraction(slide, x, y, hide_border=False):
fraction = math.MathematicalText("x+1").divide("y+2")
box = fraction.to_border_box()
box.hide_top = box.hide_bottom = box.hide_left = box.hide_right = hide_border
shape = slide.shapes.add_math_shape(x, y, 160, 90)
portion = shape.text_frame.paragraphs[0].portions[0]
portion.portion_format.font_height = 28
portion.math_paragraph.add(math.MathBlock(box))
return box
with slides.Presentation() as presentation:
presentation.slide_size.set_size(540, 220, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
add_fraction(slide, 10, 10)
partial = add_fraction(slide, 190, 10)
partial.hide_top = True
partial.hide_right = True
add_fraction(slide, 370, 10, hide_border=True).strikethrough_bottom_left_to_top_right = True
add_fraction(slide, 10, 120, hide_border=True).strikethrough_top_left_to_bottom_right = True
add_fraction(slide, 190, 120, hide_border=True).strikethrough_horizontal = True
add_fraction(slide, 370, 120).strikethrough_vertical = True
presentation.save("border-box.pptx", slides.export.SaveFormat.PPTX)

บนเศษส่วนนี้, เส้นขีดแนวนอนทับบาร์ของเศษส่วน. เส้นขีดแนวตั้งข้ามทั้งตัวเศษและตัวส่วนในขณะที่กรอบรอบ ๆ ยังปรากฏ. เส้นเหล่านี้ตกแต่งนิพจน์ทั้งหมด. การขีดเส้นทับข้อความธรรมดาจะใช้กับอักขระในรันข้อความ. หลังจากเปิดงานนำเสนอใน PowerPoint อีกครั้ง, คุณยังสามารถแก้ไขตัวเศษและตัวส่วนแยกกันได้
ควบคุมการมองเห็นและการจัดวางด้วย Phantom Elements
ห่อหุ้มนิพจน์ด้วย MathPhantom เพื่อควบคุมการมองเห็นและพื้นที่ที่มันทำให้สมการขยายโดยอิสระ. คุณสมบัติของมันควบคุมดังต่อไปนี้:
| คุณสมบัติ | ผลลัพธ์ |
|---|---|
| show | ตั้งค่าเป็น False เพื่อซ่อนนิพจน์. มิติของมันยังคงมีผลต่อการจัดวางจนกว่าจะตั้งคุณสมบัติต่อไปนี้เป็น True. |
| zero_width | ถือความกว้างแนวนอนของนิพจน์เป็นศูนย์, ให้เอเลเมนต์ข้างเคียงเคลื่อนเข้ามาใกล้กัน |
| zero_asc | ถือส่วนขึ้นของมัน (ความสูงเหนือเส้นฐาน) เป็นศูนย์. นี้สามารถลดพื้นที่ที่สงวนไว้เหนือเอเลเมนต์ข้างเคียง |
| zero_desc | ถือส่วนลงของมัน (ความลึกต่ำกว่าเส้นฐาน) เป็นศูนย์. นี้สามารถทำให้บาร์ของเศษส่วนหรือเนื้อหาอื่น ๆ อยู่ใกล้เอเลเมนต์ข้างเคียง |
Baseline คือเส้นอ้างอิงที่ตัวอักษรคณิตศาสตร์ปกติวางอยู่. การซ่อนเนื้อหาไม่ได้ลบมิติของมัน, และการถือมิติเป็นศูนย์ไม่ได้ลบนิพจน์. นิพจน์พื้นฐานยังคงเข้าถึงได้ผ่าน base. ตั้งค่า show กลับเป็น True เพื่อแสดงอีกครั้ง; การตั้งค่ามิติยังคงเดิม
ตัวอย่างด้านล่างสร้างห้าตัวแปรที่วางเคียงกัน: เศษส่วนที่มองเห็น, เศษส่วนที่ซ่อนแต่ยังสงวนพื้นที่, และสามเศษส่วนที่ซ่อนโดยตั้งมิติหนึ่งเป็นศูนย์. ตัวแปรแต่ละตัวใช้นิพจน์รอบ ๆ เดียวกันและ Cambria Math ขนาด 24 จุด. รูปร่างสมการทั้งหมดมีตำแหน่งด้านบนเดียวกัน, ทำเครื่องหมายด้วยไกด์สีเทา. การวาง phantom ในตัวเศษของเศษส่วนภายนอกทำให้การเปลี่ยนแปลงการจัดวางแนวตั้งดูง่ายขึ้น
import aspose.slides as slides
import aspose.slides.mathtext as math
def create_text(value):
text = math.MathematicalText(value)
text.format.font_height = 24
text.format.latin_font = slides.FontData("Cambria Math")
return text
with slides.Presentation() as presentation:
presentation.slide_size.set_size(1000, 180, slides.SlideSizeScaleType.DO_NOT_SCALE)
slide = presentation.slides[0]
labels = ["Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc"]
guide = slide.shapes.add_auto_shape(slides.ShapeType.LINE, 10, 60, 980, 0)
guide.line_format.fill_format.fill_type = slides.FillType.SOLID
guide.line_format.fill_format.solid_fill_color.color = slides.Color.light_gray
for i, label_text in enumerate(labels):
left = 10 + i * 200
label = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, left, 10, 190, 30)
label.fill_format.fill_type = slides.FillType.NO_FILL
label.line_format.fill_format.fill_type = slides.FillType.NO_FILL
label.text_frame.text = label_text
label_format = label.text_frame.paragraphs[0].portions[0].portion_format
label_format.font_height = 14
label_format.fill_format.fill_type = slides.FillType.SOLID
label_format.fill_format.solid_fill_color.color = slides.Color.black
fraction_numerator = create_text("a+b")
fraction_denominator = create_text("c+d")
fraction = math.MathFraction(fraction_numerator, fraction_denominator)
phantom = math.MathPhantom(fraction)
phantom.show = i == 0
phantom.zero_width = i == 2
phantom.zero_asc = i == 3
phantom.zero_desc = i == 4
left_text = create_text("x+")
right_text = create_text("+y")
numerator = math.MathBlock(left_text).join(phantom).join(right_text)
denominator = create_text("z")
equation = math.MathFraction(numerator, denominator)
shape = slide.shapes.add_math_shape(left, 60, 190, 100)
shape.text_frame.text_frame_format.anchoring_type = slides.TextAnchorType.TOP
math_paragraph = shape.text_frame.paragraphs[0].portions[0].math_paragraph
math_paragraph.add(math.MathBlock(equation))
presentation.save("math-phantom.pptx", slides.export.SaveFormat.PPTX)

จัดกลุ่มเทอม
ใช้ group เพื่อวางอักขระจัดกลุ่มเหนือหรือใต้นิพจน์. เพิ่มขีดจำกัดเพื่อทำเครื่องหมายให้เทอมที่จัดกลุ่ม

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 ใส่แถบเหนืออิลิเมนต์คณิตศาสตร์

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)
จัดรูปแบบข้อความในสมการที่มีอยู่
เพื่อจัดรูปแบบข้อความทั่วทั้งสมการที่มีอยู่, ให้เดินสำรวจโครงสร้างต้นไม้อิลิเมนต์ของแต่ละ math block. MathElementBase.get_children คืนค่าเฉพาะลูกตรง, ดังนั้นเรียกอย่างวนซ้ำเพื่อเข้าถึงข้อความภายในเศษส่วน, สคริปต์, ราก, และโครงสร้างซ้อนอื่น ๆ. ตรวจสอบอิลิเมนต์ปัจจุบันและลูกของมันเพื่อรวมอิลิเมนต์เริ่มต้นเมื่อเป็น MathematicalText
ตัวอย่างนี้สมมติว่า input.pptx มีสมการเช่น z = xᵢ / ∛y, โดยมีเศษส่วนที่มีตัวบ่งชี้ในตัวเศษและรากในตัวส่วน. การประมวลผลเฉพาะอิลิเมนต์ระดับบนของบล็อกจะพลาดข้อความในเศษส่วน. ตัวอย่างนี้ค้นหา MathPortion ในเฟรมข้อความสไลด์, เข้าถึงบล็อกผ่าน MathPortion.math_paragraph, และตั้งค่า font_height เป็น 28 จุดให้กับอิลิเมนต์ข้อความแต่ละอันผ่านคุณสมบัติ format
import aspose.slides as slides
import aspose.slides.mathtext as math
def format_math_text(element):
if isinstance(element, math.MathematicalText):
element.format.font_height = 28
for child in element.get_children():
format_math_text(child)
with slides.Presentation("input.pptx") as presentation:
for slide in presentation.slides:
for shape in slide.shapes:
if not isinstance(shape, slides.AutoShape) or shape.text_frame is None:
continue
for paragraph in shape.text_frame.paragraphs:
for portion in paragraph.portions:
if isinstance(portion, math.MathPortion):
for block in portion.math_paragraph:
format_math_text(block)
presentation.save("formatted-equations.pptx", slides.export.SaveFormat.PPTX)
งานนำเสนอที่บันทึกจะมีการจัดรูปแบบข้อความที่อัปเดตในทุกระดับของแต่ละสมการที่ประมวลผล, ในขณะที่ยังคงรักษาเนื้อหาข้อความและโครงสร้างคณิตศาสตร์
อ้างอิงอย่างเร็ว
| งาน | API หลัก |
|---|---|
| สร้างข้อความคณิตศาสตร์ | MathematicalText |
| รวมอิลิเมนต์ | MathElementBase.join |
| สร้างเศษส่วน | MathElementBase.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, ดึง math_paragraph, และอัปเดต math block ในย่อหน้านั้น
สมการถูกบันทึกเป็นคณิตศาสตร์ PowerPoint ที่แก้ไขได้หรือไม่?
ได้. เมื่อบันทึกเป็น PPTX, Aspose.Slides จะเขียนสมการเป็นเนื้อหา Office math ที่แก้ไขได้
ฉันสามารถส่งออกสมการเป็น LaTeX ได้หรือไม่?
ได้. ดึง MathParagraph ของสมการจาก MathPortion, แล้วเรียก MathParagraph.to_latex เพื่อส่งออกโดยตรง. สำหรับตัวอย่างเต็ม, ดูที่ Export Math Equations from Presentations in Python via .NET