Python で PowerPoint プレゼンテーションに数式を追加する
概要
PowerPoint は数式を Office Math Markup Language (OMML) として保存します。Aspose.Slides for Python via Java を使用すると、プログラムで同じ種類の数式コンテンツを作成できます:分数、根号、関数、リミット、N 進演算子、行列、配列、そして書式設定された数式ブロック。
PowerPoint では、ユーザーは通常 Insert > Equation から数式を追加します:

結果としてスライド上に編集可能な数式テキストが表示されます:

Aspose.Slides は、次の 3 つの主要オブジェクトを通じてその数式テキストを構築します:
- 数式シェイプは、addMathShape で作成され、数式を含むシェイプです。
- MathPortion はシェイプのテキストフレーム内に数式コンテンツを格納します。
- MathParagraph は 1 つ以上の MathBlock オブジェクトを含みます。
以下のほとんどの例は、コードを短く読みやすく保つために、MathematicalText と MathElementBase のフルエントメソッドを使用しています。
数式の作成
この例では、addMathShape を使用して数式シェイプを作成し、ピタゴラスの定理を追加します:

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 は、すでに数式段落を含むシェイプを作成します。最初の MathPortion にアクセスし、getMathParagraph を呼び出して、数式ブロックまたは数式要素を追加します。分数の追加
divide を使用して分数を作成します。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()
積み重ね形式の分数には、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)
根号の追加
radical を使用して平方根、立方根、その他の根号を作成します。現在の要素が底(base)になり、引数が指数(degree)になります。

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()
平方根の次数の表示・非表示
平方根は、明示的に 2 を表示するか、従来の √x 表記を使用できます。MathRadical の setHideDegree メソッドでどちらの形式を表示するか制御します。どちらの場合でも、編集可能な数式内で次数 2 と根体 x は保持されます。

この例では同じ平方根を 2 回保存します。visible-degree.pptx では 2 が表示され、hidden-degree.pptx では非表示になります。
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)
radical = MathematicalText("x").radical("2")
math_block = MathBlock(radical)
math_shape = slide.getShapes().addMathShape(20, 20, 200, 100)
math_paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getMathParagraph()
math_paragraph.add(math_block)
radical.setHideDegree(False)
presentation.save("visible-degree.pptx", SaveFormat.Pptx)
radical.setHideDegree(True)
presentation.save("hidden-degree.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
関数とリミットの追加
sin(x)、log(x) などの関数、またはカスタム関数名には asArgumentOfFunction または function を使用します。リミットについては、lim を MathLimit に入れるか、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()
カスタム関数名の場合、関数名を現在の要素にし、その引数で function を呼び出します:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathematicalText
custom_function = MathematicalText("f").function("x + 1")
N 進演算子と積分の追加
総和、和集合、積集合、その他の大きな演算子には nary を使用します。積分には integral を使用します。どちらのメソッドも下限と上限を設定できます。

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()
N-ary 演算子は、オプションのリミットを備えた大きな演算子用です。+、-、= のような単純な演算子は通常 MathematicalText として追加し、式に結合します。
積分の場合は、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")
行列の追加
行と列には MathMatrix を使用します。行列はデフォルトで括弧を含まないため、丸括弧、角括弧、または波括弧が必要な場合は行列を囲んでください。

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()
数式配列の追加
整列した数式や縦に積み重ねた式が必要な場合は、toMathArray を使用します。

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()
周囲の要素と配列を揃える
setBaseJustification は、配列の垂直方向の配置を隣接する数式要素に対して制御します。以下の例では、最初の行、配列の中心、最後の行での配置を比較しています。
以下の例は、3 行で構成され、隣接要素 A= と =B を持つ 3 つの同一配列を作成します。すべての数式テキストは 24 ポイントの Cambria Math を使用しています。配列の配置だけが変わります。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathVerticalAlignment, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
presentation = Presentation()
try:
presentation.getSlideSize().setSize(780, 200, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
alignments = (("Top", MathVerticalAlignment.Top), ("Center", MathVerticalAlignment.Center), ("Bottom", MathVerticalAlignment.Bottom))
for index, (name, alignment) in enumerate(alignments):
x = 20 + index * 260
label = slide.getShapes().addAutoShape(ShapeType.Rectangle, x, 10, 240, 30)
label.getFillFormat().setFillType(FillType.NoFill)
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
label.getTextFrame().setText(name)
label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
label_format.setFontHeight(16)
label_format.getFillFormat().setFillType(FillType.Solid)
label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
math_shape = slide.getShapes().addMathShape(x, 60, 240, 130)
math_shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
math_paragraph.setJustification(MathJustification.LeftJustified)
equation_array = MathArray(create_text("x+y=3"))
equation_array.getArguments().add(create_text("x-y=1"))
equation_array.getArguments().add(create_text("x=2"))
equation_array.setBaseJustification(alignment)
equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
math_paragraph.add(equation)
presentation.save("equation-array-alignment.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
以下の比較は、PowerPoint で保存されたプレゼンテーションを示します。各配列の横にある A= と =B の位置に注目してください。

行間の設定
setRowSpacingRule は配列内の行の間隔ルールを選択します。setRowSpacing は明示的な間隔ルールの数値を提供し、プリセットルールでは無視されます。
正確な間隔の場合、値はポイントで測定されます。比例間隔の場合、半行単位で測定されます:例の値 3 は 1.5 行を表します。明示的な値を指定する際は両方のプロパティを設定してください。数値だけを変更しても間隔ルールは選択されません。
以下の例では、同じ数式、配置、フォント設定で 5 つのプレゼンテーションを保存します。最初の 3 つはプリセットを使用し、最後の 2 つは明示的な値を示します。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, MathArray, MathBlock, MathJustification, MathematicalText, MathRowSpacingRule, MathVerticalAlignment, Presentation, SaveFormat
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
settings = (("SingleLineGap", MathRowSpacingRule.SingleLineGap, 0), ("OneAndAHalfLineGap", MathRowSpacingRule.OneAndAHalfLineGap, 0), ("TwoLineGap", MathRowSpacingRule.TwoLineGap, 0), ("Exactly", MathRowSpacingRule.Exactly, 20), ("Multiple", MathRowSpacingRule.Multiple, 3))
for name, rule, spacing in settings:
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
math_shape = slide.getShapes().addMathShape(20, 20, 600, 300)
paragraph = math_shape.getTextFrame().getParagraphs().get_Item(0)
paragraph.getParagraphFormat().setSpaceBefore(0)
paragraph.getParagraphFormat().setSpaceAfter(0)
math_paragraph = paragraph.getPortions().get_Item(0).getMathParagraph()
math_paragraph.setJustification(MathJustification.LeftJustified)
equation_array = MathArray(create_text("x+y=3"))
equation_array.getArguments().add(create_text("x-y=1"))
equation_array.getArguments().add(create_text("x=2"))
equation_array.setBaseJustification(MathVerticalAlignment.Center)
equation_array.setRowSpacingRule(rule)
equation_array.setRowSpacing(spacing)
equation = MathBlock(create_text("A=")).join(equation_array).join(create_text("=B"))
math_paragraph.add(equation)
presentation.save(f"equation-array-{name}.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
配列の行間は段落間隔とは別です。setSpaceBefore と setSpaceAfter は配列行間ではなく、含む段落の前後にスペースを追加します。同様に、MathParagraph.setJustification は数式段落全体の水平位置を設定し、配列の垂直配置設定を置き換えるものではありません。
三角関数の追加
引数が現在の要素で関数名が既知の場合は、asArgumentOfFunction を使用します。

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()
下付き文字と上付き文字の追加
添字と指数には下付き・上付きヘルパーを使用します。添字を底の左側に表示する必要がある場合は、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()
区切り記号の追加
enclose を使用して式を区切り記号で囲みます。複数要素を含む区切り式の場合、区切り文字を設定することもできます。

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()
区切り記号のサイズと配置
True を setGrowToMatchOperandHeight に渡すと、高いオペランドに合わせて括弧が拡大します。False を渡すと通常サイズのままです。setDelimiterShape と 2 つの MathDelimiterShape のいずれかを使用します。Centered は等号の中央を通る数式軸上で括弧を中央揃えにし、Match は囲む内容に合わせて揃えます。これらの設定は区切り記号のみに影響し、内部の式は変わりません。
この例では、x の指数に分数を配置し、式がベースライン上部により高く伸びるようにしています。4 つの式すべてで同じ括弧とフォント設定を使用しています。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathDelimiterShape, MathematicalText, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
for row, grow in enumerate((True, False)):
for column, shape in enumerate((MathDelimiterShape.Centered, MathDelimiterShape.Match)):
fraction = MathematicalText("a + b").divide("c + d")
power = MathematicalText("x").setSuperscript(fraction)
delimiter = power.enclose("(", ")")
delimiter.setGrowToMatchOperandHeight(grow)
delimiter.setDelimiterShape(shape)
math_shape = slide.getShapes().addMathShape(20 + 330 * column, 20 + 120 * row, 300, 100)
portion = math_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
portion.getPortionFormat().setFontHeight(32)
equation = MathematicalText("y = ").join(delimiter)
portion.getMathParagraph().add(equation)
presentation.save("delimiter-sizing.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
PowerPoint で各行を比較すると、拡大の効果が確認できます。上段では、括弧が指数を囲むように拡大し、両形状で同様に見えます。下段では拡大がオフになっています。左側の小さな括弧(Centered)は等号に揃えられ、右側の括弧(Match)は式の上部に位置します。

枠ボックスの追加
分数に対して toBorderBox を呼び出すと、式全体を枠で囲みます。返される MathBorderBox はデフォルトで四辺すべてを表示します。個別の辺を非表示にするには、setHideTop、setHideBottom、setHideLeft、setHideRight を使用します。
例では、同じ分数を 6 コピー 1 スライドに配置しています。取り消し線を示すために、四辺すべてを非表示にし、上向き対角線(↗)には setStrikethroughBottomLeftToTopRight に True を渡し、下向き対角線(↘)には setStrikethroughTopLeftToBottomRight に True を渡します。また、任意の表示側の組み合わせで setStrikethroughHorizontal や setStrikethroughVertical を使用できます。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import MathBlock, MathematicalText, Presentation, SaveFormat, SlideSizeScaleType
presentation = Presentation()
try:
presentation.getSlideSize().setSize(540, 220, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
def add_fraction(x, y, hide_border):
fraction = MathematicalText("x+1").divide("y+2")
box = fraction.toBorderBox()
box.setHideTop(hide_border)
box.setHideBottom(hide_border)
box.setHideLeft(hide_border)
box.setHideRight(hide_border)
shape = slide.getShapes().addMathShape(x, y, 160, 90)
portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
portion.getPortionFormat().setFontHeight(28)
math_block = MathBlock(box)
portion.getMathParagraph().add(math_block)
return box
add_fraction(10, 10, False)
partial = add_fraction(190, 10, False)
partial.setHideTop(True)
partial.setHideRight(True)
add_fraction(370, 10, True).setStrikethroughBottomLeftToTopRight(True)
add_fraction(10, 120, True).setStrikethroughTopLeftToBottomRight(True)
add_fraction(190, 120, True).setStrikethroughHorizontal(True)
add_fraction(370, 120, False).setStrikethroughVertical(True)
presentation.save("border-box.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

この分数では、水平取り消し線が分数線と重なります。垂直取り消し線は分子と分母の両方を横切り、周囲の枠は表示されたままです。これらの線は式全体を装飾します。通常のテキスト取り消し線はテキストランの文字に適用されます。PowerPoint でプレゼンテーションを再度開いても、分子と分母は個別に編集可能です。
Phantom 要素で可視性とレイアウトを制御
式を MathPhantom でラップすると、その可視性と数式への寄与スペースを個別に制御できます。MathPhantom のメソッドは以下を制御します:
| Method | Effect |
|---|---|
| setShow | False を渡すと式が非表示になります。対応するメソッドが True を受け取らない限り、寸法はレイアウトに寄与し続けます。 |
| setZeroWidth | 式の横幅をゼロとみなし、隣接要素をより近づけます。 |
| setZeroAsc | ベースライン上部の高さ(上昇部)をゼロとみなし、隣接要素上部の確保スペースを減らすことができます。 |
| setZeroDesc | ベースライン下部の深さ(下降部)をゼロとみなし、式下の分数線や他のコンテンツを隣接要素に近づけます。 |
ベースラインは、通常の数式文字が置かれる基準線です。コンテンツを非表示にしても寸法は削除されず、次元をゼロにしても式は削除されません。基になる式は getBase で取得可能です。setShow に True を渡すと再表示され、次元設定はそのまま保持されます。
以下の例では、5 つの横並びバリエーションを作成します:表示分数、スペースを確保したまま非表示の分数、そして 1 つの次元だけをゼロにした 3 つの非表示分数です。各バリエーションは同じ周囲式と 24 ポイントの Cambria Math を使用します。すべての数式シェイプは同じ上位置で、灰色のガイドで示されています。外側分数の分子に Phantom を配置すると、縦レイアウトの変化が分かりやすくなります。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, MathBlock, MathFraction, MathematicalText, MathPhantom, Presentation, SaveFormat, ShapeType, SlideSizeScaleType, TextAnchorType
from java.awt import Color
def create_text(value):
text = MathematicalText(value)
text.getFormat().setFontHeight(24)
text.getFormat().setLatinFont(FontData("Cambria Math"))
return text
presentation = Presentation()
try:
presentation.getSlideSize().setSize(1000, 180, SlideSizeScaleType.DoNotScale)
slide = presentation.getSlides().get_Item(0)
labels = ("Visible", "Hidden: reserves space", "Hidden: ZeroWidth", "Hidden: ZeroAsc", "Hidden: ZeroDesc")
guide = slide.getShapes().addAutoShape(ShapeType.Line, 10, 60, 980, 0)
guide.getLineFormat().getFillFormat().setFillType(FillType.Solid)
guide.getLineFormat().getFillFormat().getSolidFillColor().setColor(Color.LIGHT_GRAY)
for index, name in enumerate(labels):
left = 10 + index * 200
label = slide.getShapes().addAutoShape(ShapeType.Rectangle, left, 10, 190, 30)
label.getFillFormat().setFillType(FillType.NoFill)
label.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
label.getTextFrame().setText(name)
label_format = label.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0).getPortionFormat()
label_format.setFontHeight(14)
label_format.getFillFormat().setFillType(FillType.Solid)
label_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
fraction_numerator = create_text("a+b")
fraction_denominator = create_text("c+d")
fraction = MathFraction(fraction_numerator, fraction_denominator)
phantom = MathPhantom(fraction)
phantom.setShow(index == 0)
phantom.setZeroWidth(index == 2)
phantom.setZeroAsc(index == 3)
phantom.setZeroDesc(index == 4)
left_text = create_text("x+")
right_text = create_text("+y")
numerator = MathBlock(left_text).join(phantom).join(right_text)
denominator = create_text("z")
equation = MathFraction(numerator, denominator)
shape = slide.getShapes().addMathShape(left, 60, 190, 100)
shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
math_portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
math_paragraph = math_portion.getMathParagraph()
math_block = MathBlock(equation)
math_paragraph.add(math_block)
presentation.save("math-phantom.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()

項のグループ化
group を使用して、式の上または下にグループ化文字を配置します。リミットを追加してグループ化された項にラベルを付けます。

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()
数式要素の書式設定
書式設定ヘルパーは、式を明確にする場合にのみ使用してください。例として、overbar は数式要素の上にバーが付いた数式です。

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()
既存の数式内テキストの書式設定
既存の数式全体のテキストを書式設定するには、各数式ブロックの要素ツリーを走査します。MathElementBase.getChildren は直接の子要素のみを返すため、再帰的に呼び出して分数、上下添字、根号、その他の入れ子構造内のテキストに到達します。開始要素が MathematicalText の場合、現在の要素と子要素の両方をチェックしてください。
この例では、input.pptx に z = xᵢ / ∛y のような数式が含まれていると想定しています。分数の分子に下付き、分母に根号を持つ式です。ブロックの上位要素のみを処理すると、分数内部のテキストが見逃されます。例では、スライドのテキストフレームから数式部分を取得し、MathPortion.getMathParagraph を介してブロックにアクセスし、各テキスト要素の getFormat メソッドで setFontHeight を使用してフォントサイズを 28 ポイントに設定します。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import AutoShape, MathematicalText, MathPortion, Presentation, SaveFormat
def format_math_text(element):
if isinstance(element, MathematicalText):
element.getFormat().setFontHeight(28)
for child in element.getChildren():
format_math_text(child)
presentation = Presentation("input.pptx")
try:
for slide in presentation.getSlides():
for shape in slide.getShapes():
if not isinstance(shape, AutoShape):
continue
if shape.getTextFrame() is None:
continue
for paragraph in shape.getTextFrame().getParagraphs():
for portion in paragraph.getPortions():
if isinstance(portion, MathPortion):
for block in portion.getMathParagraph():
format_math_text(block)
presentation.save("formatted-equations.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
保存されたプレゼンテーションは、処理された各数式のすべてのレベルで更新されたテキスト書式設定を保持し、テキスト内容と数式構造はそのままです。
クイックリファレンス
| Task | Main API |
|---|---|
| 数式テキストの作成 | MathematicalText |
| 要素の結合 | MathElementBase.join |
| 分数の作成 | MathElementBase.divide |
| 上付き・下付きの追加 | setSuperscript, setSubscript |
| 関数の追加 | function, asArgumentOfFunction |
| 根号の追加 | MathElementBase.radical |
| リミットの追加 | setLowerLimit, setUpperLimit |
| 左側脚注(添字)の追加 | setSubSuperscriptOnTheLeft |
| 総和と積分の追加 | nary, integral |
| 行列の追加 | MathMatrix |
| 数式配列の追加 | toMathArray |
| 区切り記号の追加 | enclose |
| バーと枠の追加 | overbar, toBorderBox |
| 項のグループ化 | group |
よくある質問
既存の PowerPoint の数式を編集できますか?
はい。プレゼンテーションを開き、MathPortion を含むシェイプを見つけ、getMathParagraph を呼び出して、その段落内の数式ブロックを更新します。
数式は編集可能な PowerPoint の数式として保存されますか?
はい。PPTX として保存すると、Aspose.Slides は数式を編集可能な Office 数式コンテンツとして書き込みます。
数式を LaTeX にエクスポートできますか?
はい。数式の MathParagraph をその MathPortion から取得し、MathParagraph.toLatex を呼び出して直接エクスポートできます。完全な例については、Python でのプレゼンテーションから数式をエクスポート を参照してください。