在 Python via Java 中格式化簡報文字
概述
本文說明如何使用 Aspose.Slides for Python via Java 在 PowerPoint 與 OpenDocument 簡報中格式化文字。內容涵蓋背景色、透明度、字元間距、字型屬性、旋轉、段落間距、自動調整行為、文字錨點、定位點以及語言設定。
除非另有說明,範例皆使用 sample.pptx。其第一張投影片的第一個圖形是一個文字方塊,第一個段落包含下列文字。投影片與圖形索引皆以零為起點。選取粗體部份的範例使用有效格式,包括繼承的粗體格式:

若要搜尋並突顯文字字面值或正規表達式匹配,請參閱 Search and Replace Text。
設定文字背景色
使用 ParagraphFormat.getDefaultPortionFormat 為段落設定預設突出顯示色,或使用 BasePortionFormat.getHighlightColor 為單一文字部份設定。
以下範例將第一段的預設突出顯示色設為淡灰色。對個別部份的明確突出顯示色會優先於此預設設定:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
from java.awt import Color
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
# 設定整個段落的突出顯示顏色。
paragraph.getParagraphFormat().getDefaultPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY)
presentation.save("gray_paragraph.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

以下程式碼示範如何為 粗體字型的文字部份 設定背景色:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
from java.awt import Color
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
for portion in paragraph.getPortions():
if portion.getPortionFormat().getEffective().getFontBold():
# 設定文字部份的突出顯示顏色。
portion.getPortionFormat().getHighlightColor().setColor(Color.LIGHT_GRAY)
presentation.save("gray_text_portions.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

對齊文字段落
使用 ParagraphFormat.setAlignment 設定文字框內段落的對齊方式。可設定為置中、左對齊、右對齊、兩端對齊等。
以下程式碼示範如何將段落置於 中心:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TextAlignment
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
# 設定段落的對齊方式為置中。
paragraph.getParagraphFormat().setAlignment(TextAlignment.Center)
presentation.save("aligned_paragraph.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

在同一行內對齊字型
使用 ParagraphFormat.setFontAlignment 於同一行內垂直對齊不同字型大小的文字部份。此設定套用於整段,且控制每行內的對齊方式。
以下自包含範例在同一張投影片上建立四個帶標籤的文字方塊。每個段落以 18、36、54 點大小顯示相同文字,且字型對齊方式不同。使用 Arial、停用自動調整與換列,並保持文字框足夠寬以容納單行文字。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontAlignment, FontData, NullableBool, Paragraph, Portion, Presentation, SaveFormat, ShapeType, TextAlignment, TextAnchorType, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
alignments = [FontAlignment.Baseline, FontAlignment.Top, FontAlignment.Center, FontAlignment.Bottom]
alignment_names = ["Baseline", "Top", "Center", "Bottom"]
font_sizes = [18.0, 36.0, 54.0]
font = FontData("Arial")
for i, alignment in enumerate(alignments):
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 20 + i * 130, 660, 120)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)
text_frame.getTextFrameFormat().setWrapText(NullableBool.False_)
label = text_frame.getParagraphs().get_Item(0)
label.setText(alignment_names[i])
label.getParagraphFormat().setAlignment(TextAlignment.Left)
label.getParagraphFormat().getDefaultPortionFormat().setFontHeight(14)
label.getParagraphFormat().getDefaultPortionFormat().setLatinFont(font)
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
label.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.GRAY)
paragraph = Paragraph()
paragraph.getParagraphFormat().setFontAlignment(alignment)
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left)
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(font)
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
for font_size in font_sizes:
portion = Portion("Ag ")
portion.getPortionFormat().setFontHeight(font_size)
paragraph.getPortions().add(portion)
text_frame.getParagraphs().add(paragraph)
presentation.save("font_alignment.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

字型對齊使用字型度量,因此個別字母的可見邊緣不一定完全對齊。範例同時包含大寫字母與下行字元,以說明基線與底部對齊的差異。字型可用性與替代、使用的字元以及字型大小差異皆會影響結果。框架大小、邊距、行距、換列與自動調整亦會影響版面;比較模式時請使用相同的字型與版面設定。
此設定不同於 ParagraphFormat.setAlignment(水平段落對齊)以及 TextFrameFormat.setAnchoringType(文字區塊在圖形內的垂直位置)。透過 BasePortionFormat.setEscapement 的上標與下標格式會相對基線移動部份文字,而非設定段落行的字型對齊。
設定文字透明度
文字透明度透過指派給 BasePortionFormat.getFillFormat 的顏色之 alpha 成分控制。以下範例中,alpha = 50 為 0–255 之間的 ARGB alpha 通道值,而非透明度百分比。
以下程式碼示範如何將 整段 設為透明:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat
from java.awt import Color
alpha = 50
text_color = Color(0, 0, 0, alpha)
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
# 設定文字的填充顏色為透明顏色。
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
paragraph.getParagraphFormat().getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(text_color)
presentation.save("transparent_paragraph.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

以下程式碼示範如何將 粗體字型的文字部份 設為透明:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, Presentation, SaveFormat
from java.awt import Color
alpha = 50
text_color = Color(0, 0, 0, alpha)
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
for portion in paragraph.getPortions():
if portion.getPortionFormat().getEffective().getFontBold():
# 設定文字部份的透明度。
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(text_color)
presentation.save("transparent_text_portions.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

設定文字字元間距
使用 BasePortionFormat.setSpacing 以擴大或緊縮文字方塊內字元之間的間距。範例加入 3 點間距;負值則會緊縮文字。
以下 Python 程式碼示範如何在 整段 中擴大字元間距:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
# 註:使用負值可壓縮字元間距。
paragraph.getParagraphFormat().getDefaultPortionFormat().setSpacing(3) # 擴展字元間距。
presentation.save("character_spacing_in_paragraph.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

以下程式碼示範如何在 粗體字型的文字部份 中擴大字元間距:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
for portion in paragraph.getPortions():
if portion.getPortionFormat().getEffective().getFontBold():
# 註:使用負值可壓縮字元間距。
portion.getPortionFormat().setSpacing(3) # 擴展字元間距。
presentation.save("character_spacing_in_text_portions.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

為特定字型停用字距調整 (Kerning)
在某些情況下,Aspose.Slides 所渲染的文字看起來會比 PowerPoint 中的同樣文字稍微緊密。這可能是因為 PowerPoint 會忽略某些字型的字距調整資料,即使該字型本身包含有效的字距資訊且在 PowerPoint 設定中已啟用字距調整。
若需讓渲染結果更接近 PowerPoint,可為使用受影響字型的文字部份停用字距調整。將 BasePortionFormat.setKerningMinimalSize 設為大於實際字型大小的值。本例需要「presentation.pptx」且其第一張投影片的第一個圖形為文字方塊。程式會檢查有效字型名稱(包括繼承的字型),並為使用 Roboto 的部份設定 100 點的門檻值;低於此門檻的部份將停用字距調整:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("presentation.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
target_font = "Roboto"
for paragraph in auto_shape.getTextFrame().getParagraphs():
for portion in paragraph.getPortions():
portion_format = portion.getPortionFormat().getEffective()
fonts = (portion_format.getLatinFont(), portion_format.getEastAsianFont(), portion_format.getComplexScriptFont())
if any(font is not None and font.getFontName() == target_font for font in fonts):
portion.getPortionFormat().setKerningMinimalSize(100)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
對於低於門檻的相符文字,此設定會阻止字距調整,並有助於使 Aspose.Slides 的渲染與 PowerPoint 針對受此 PowerPoint 特定行為影響的字型之視覺輸出保持一致。
管理文字字型屬性
可透過 ParagraphFormat.getDefaultPortionFormat 在段落層級設定字型屬性,或透過 PortionFormat 在單一文字部份上設定。
以下範例將第一段的預設字型設為 12 點 Times New Roman,且套用粗體、斜體與點狀底線。個別部份的明確格式會優先於這些預設:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, NullableBool, Presentation, SaveFormat, TextUnderlineType
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
# 設定段落的字型屬性。
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontHeight(12)
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontBold(NullableBool.True_)
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontItalic(NullableBool.True_)
paragraph.getParagraphFormat().getDefaultPortionFormat().setFontUnderline(TextUnderlineType.Dotted)
font = FontData("Times New Roman")
paragraph.getParagraphFormat().getDefaultPortionFormat().setLatinFont(font)
presentation.save("font_properties_for_paragraph.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

以下範例對有效格式為粗體的部份套用 13 點 Times New Roman、斜體以及點狀底線:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, NullableBool, Presentation, SaveFormat, TextUnderlineType
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
for portion in paragraph.getPortions():
if portion.getPortionFormat().getEffective().getFontBold():
# 設定文字部份的字型屬性。
portion.getPortionFormat().setFontHeight(13)
portion.getPortionFormat().setFontItalic(NullableBool.True_)
portion.getPortionFormat().setFontUnderline(TextUnderlineType.Dotted)
font = FontData("Times New Roman")
portion.getPortionFormat().setLatinFont(font)
presentation.save("font_properties_for_text_portions.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

控制文字高度正規化
使用 BasePortionFormat.setNormaliseHeight 以在重現範本排版或研究字元高度差異時啟用或停用正規化。在 PowerPoint 中,啟用正規化會將個別字元在垂直方向上拉伸或壓縮,使其在文字部份內呈現統一的可見高度。此設定不會保證不同字型之間的文字高度相等。
將屬性設定為 NullableBool.True_ or False_ 以明確啟用或停用正規化。NullableBool.NotDefined 會保留本地設定未定義,讓部份繼承其值,例如自段落的 getDefaultPortionFormat。它並不等同於 False。使用 PortionFormat.getEffective 並讀取回傳的有效格式之 getNormaliseHeight 以取得解析後的 Boolean 值。
以下自包含範例需要安裝 Arial。它建立兩個文字區塊,內容相同,包含大寫字母、小寫字母、下行字元與重音字元,字型大小為 54 點。兩段皆預設啟用正規化;上方文字部份明確停用,下方則繼承設定。框架大小、邊距與段落間距相同,且停用換列與自動調整以保持比較一致。程式會輸出 False: False 與 NotDefined: True,最後儲存為「text_height_normalization.pptx」。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, NullableBool, Presentation, SaveFormat, ShapeType, TextAlignment, TextAnchorType, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
settings = [NullableBool.False_, NullableBool.NotDefined]
setting_names = ["False", "NotDefined"]
for i, setting in enumerate(settings):
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 60 + i * 200, 660, 150)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
text_frame = shape.getTextFrame()
text_frame.setText("Hxgjpq ÅÉ 0123")
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)
text_frame.getTextFrameFormat().setWrapText(NullableBool.False_)
text_frame.getTextFrameFormat().setAnchoringType(TextAnchorType.Top)
text_frame.getTextFrameFormat().setMarginTop(0)
text_frame.getTextFrameFormat().setMarginLeft(0)
paragraph = text_frame.getParagraphs().get_Item(0)
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left)
paragraph.getParagraphFormat().setSpaceBefore(0)
paragraph.getParagraphFormat().setSpaceAfter(0)
paragraph.getParagraphFormat().setSpaceWithin(100)
paragraph.getParagraphFormat().getDefaultPortionFormat().setNormaliseHeight(NullableBool.True_)
portion = paragraph.getPortions().get_Item(0)
font = FontData("Arial")
portion.getPortionFormat().setLatinFont(font)
portion.getPortionFormat().setFontHeight(54)
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
portion.getPortionFormat().setNormaliseHeight(setting)
print(f"{setting_names[i]}: {bool(portion.getPortionFormat().getEffective().getNormaliseHeight())}")
presentation.save("text_height_normalization.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
以下比較圖為已儲存簡報的匯出結果。上排正規化為停用 (False);下排繼承啟用 (NotDefined → True)。請保留 Arial 以重現此外觀。

正規化會改變字元高度而不改變指派的 getFontHeight 值(即字型大小,以點為單位)。段落的 setSpaceWithin 控制行距,而文字框的 setAutofitType 控制文字或其容器的調整方式。這些設定各自用途不同,且可能獨立影響整體版面。欲了解更多段落格式與繼承,請參閱 Manage Paragraph。
套用與移除刪除線
使用 BasePortionFormat.setStrikethroughType 可於文字部份上顯示更正、取消的字詞或先前的價格,而不必刪除原始文字。可設定為 TextStrikethroughType.Single or Double 以顯示單線或雙線刪除線。
若要移除刪除線,傳入 TextStrikethroughType.None_。即使周圍格式指定了刪除線,也會明確停用。相比之下,TextStrikethroughType.NotDefined 會保留本地設定未定義,讓部份繼承自段落等上層格式,並不保證刪除線被移除。使用 PortionFormat.getEffective 可讀取最終格式,包括繼承值。
以下自包含範例建立三個價格比較。每段都有標籤、原價與現價三個部份,僅原價會受到刪除線影響。第一排先套用再移除刪除線;其他排分別保留單線與雙線刪除線。變更此設定可保留文字及其他格式(包括粗體的原價)。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, NullableBool, Paragraph, Portion, Presentation, SaveFormat, ShapeType, TextAlignment, TextStrikethroughType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 620, 220)
shape.getFillFormat().setFillType(FillType.NoFill)
shape.getLineFormat().getFillFormat().setFillType(FillType.NoFill)
shape.getTextFrame().getParagraphs().clear()
labels = ["None: ", "Single: ", "Double: "]
for label in labels:
paragraph = Paragraph()
paragraph.getParagraphFormat().setAlignment(TextAlignment.Left)
paragraph.getParagraphFormat().setSpaceAfter(20)
portion_format = paragraph.getParagraphFormat().getDefaultPortionFormat()
portion_format.setFontHeight(28)
font = FontData("Arial")
portion_format.setLatinFont(font)
portion_format.getFillFormat().setFillType(FillType.Solid)
portion_format.getFillFormat().getSolidFillColor().setColor(Color.BLACK)
label_portion = Portion(label)
paragraph.getPortions().add(label_portion)
previous_price = Portion("$100")
previous_price.getPortionFormat().setFontBold(NullableBool.True_)
paragraph.getPortions().add(previous_price)
current_price = Portion(" → $80")
paragraph.getPortions().add(current_price)
shape.getTextFrame().getParagraphs().add(paragraph)
paragraphs = shape.getTextFrame().getParagraphs()
# 套用刪除線,然後明確移除第一個先前價格的刪除線。
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single)
paragraphs.get_Item(0).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.None_)
paragraphs.get_Item(1).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Single)
paragraphs.get_Item(2).getPortions().get_Item(1).getPortionFormat().setStrikethroughType(TextStrikethroughType.Double)
presentation.save("strikethrough.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果(由上至下):無刪除線、單線刪除線、雙線刪除線。

設定文字旋轉
使用 TextFrameFormat.setTextVerticalType 於圖形內設定預定義的文字方向。
以下程式碼將文字方向設定為 TextVerticalType.Vertical270,這會將文字 逆時針旋轉 90 度:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TextVerticalType
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
auto_shape.getTextFrame().getTextFrameFormat().setTextVerticalType(TextVerticalType.Vertical270)
presentation.save("text_rotation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

為文字框設定自訂旋轉角度
使用 TextFrameFormat.setRotationAngle 為 TextFrame 設定自訂旋轉角度。
以下程式碼在圖形內將文字框順時針旋轉 3 度:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
auto_shape.getTextFrame().getTextFrameFormat().setRotationAngle(3)
presentation.save("custom_text_rotation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

設定段落行距
Aspose.Slides 提供 ParagraphFormat.setSpaceAfter、ParagraphFormat.setSpaceBefore 與 ParagraphFormat.setSpaceWithin 以控制段落間距。使用方式如下:
- 正值表示以行高的百分比指定行距。
- 負值表示以點數指定行距。
以下範例將第一段的段內行距設定為行高的 200%(雙倍行距):
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
paragraph.getParagraphFormat().setSpaceWithin(200)
presentation.save("line_spacing.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

控制換列行為
段落換列規則在窄文字區塊與混合拉丁與東亞文字的簡報中很有用。以下方法屬於 ParagraphFormat,因此會套用於整段:
- setLatinLineBreak 控制拉丁文字的換列規則。在混合文字中,變更此設定亦可能影響相鄰東亞文字與標點的換列位置。
- setEastAsianLineBreak 控制東亞文字的換列規則,包括行首與行尾字符的限制。
這些規則不會取代 TextFrameFormat.setWrapText,後者會在文字框內啟用自動換列。規則會在換列發生時影響版面布局;它們不會插入換列字元。明確的換列會強制段落內另起新行,與可用寬度無關。
以下自包含範例建立包含中文與拉丁文字的窄文字區塊,明確設定兩項換列選項,並儲存為「line_breaking.pptx」。若要測試任一規則,變更對應的值,同時保持另一設定不變。範例使用 24 點 Arial 與 SimSun,框寬 160 點,水平文字框邊距為 0。呼叫 TextFrameFormat.setAutofitType 並傳入 TextAutofitType.None_,使文字大小與框尺寸固定。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, NullableBool, Presentation, SaveFormat, ShapeType, TextAlignment, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 160, 300)
shape.getFillFormat().setFillType(FillType.NoFill)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setWrapText(NullableBool.True_)
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)
text_frame.getTextFrameFormat().setMarginLeft(0)
text_frame.getTextFrameFormat().setMarginRight(0)
paragraph = text_frame.getParagraphs().get_Item(0)
paragraph.setText("中文排版测试,PowerPoint 中文演示。")
paragraph_format = paragraph.getParagraphFormat()
paragraph_format.setAlignment(TextAlignment.Left)
paragraph_format.getDefaultPortionFormat().setFontHeight(24)
latin_font = FontData("Arial")
paragraph_format.getDefaultPortionFormat().setLatinFont(latin_font)
east_asian_font = FontData("SimSun")
paragraph_format.getDefaultPortionFormat().setEastAsianFont(east_asian_font)
paragraph_format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
paragraph_format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
paragraph_format.setLatinLineBreak(NullableBool.False_)
paragraph_format.setEastAsianLineBreak(NullableBool.True_)
presentation.save("line_breaking.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
控制懸掛標點符號
ParagraphFormat.setHangingPunctuation 允許符合條件的標點符號延伸至文字行右側之外,而不是佔用下一行。此設定適用於整段,且不同於懸掛縮排。
以下自包含範例在寬度為 100 點的文字框內啟用懸掛標點,並儲存為「hanging_punctuation.pptx」。使用 24 點 Arial 與水平文字框邊距為 0 時,最後的句點仍位於「sentence」之後,並延伸至文字右邊緣。將屬性設定為 NullableBool.False_ 以比較:此設定下句點會佔據單獨一行。啟用換列且停用自動調整,以保持可用寬度固定。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FillType, FontData, NullableBool, Presentation, SaveFormat, ShapeType, TextAlignment, TextAutofitType
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 50, 50, 100, 200)
shape.getFillFormat().setFillType(FillType.NoFill)
text_frame = shape.getTextFrame()
text_frame.getTextFrameFormat().setWrapText(NullableBool.True_)
text_frame.getTextFrameFormat().setAutofitType(TextAutofitType.None_)
text_frame.getTextFrameFormat().setMarginLeft(0)
text_frame.getTextFrameFormat().setMarginRight(0)
paragraph = text_frame.getParagraphs().get_Item(0)
paragraph.setText("Simple text, next sentence.")
paragraph_format = paragraph.getParagraphFormat()
paragraph_format.setAlignment(TextAlignment.Left)
paragraph_format.getDefaultPortionFormat().setFontHeight(24)
latin_font = FontData("Arial")
paragraph_format.getDefaultPortionFormat().setLatinFont(latin_font)
paragraph_format.getDefaultPortionFormat().getFillFormat().setFillType(FillType.Solid)
paragraph_format.getDefaultPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
paragraph_format.setHangingPunctuation(NullableBool.True_)
presentation.save("hanging_punctuation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
並非所有標點符號皆可懸掛。上述 控制換列 中的字型與版面條件同樣適用於此比較:變更字型、可用寬度、邊距或自動調整設定,都可能消除可見差異。
設定文字框的自動調整類型
TextFrameFormat.setAutofitType 決定文字超出容器邊界時的行為。可用以控制文字是縮小、溢出,或自動調整圖形大小以容納文字。以下範例將圖形設定為自動調整以符合文字,並將結果儲存為「autofit_type.pptx」。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TextAutofitType
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
auto_shape.getTextFrame().getTextFrameFormat().setAutofitType(TextAutofitType.Shape)
presentation.save("autofit_type.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
若要在自動換列後統計行數並觀察文字或圖形寬度變化,請參閱 Count Rendered Lines。僅依行數無法判斷文字是否溢出容器。
設定文字框的錨點
TextFrameFormat.setAnchoringType 定義文字在圖形內的垂直位置,例如置頂、置中或置底。以下範例將文字錨定於第一個圖形的底部,並將結果儲存為「text_anchor.pptx」。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TextAnchorType
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
auto_shape.getTextFrame().getTextFrameFormat().setAnchoringType(TextAnchorType.Bottom)
presentation.save("text_anchor.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
設定文字定位點 (Tab)
使用 ParagraphFormat.setDefaultTabSize 與 ParagraphFormat.getTabs 於段落中配置定位點。以下範例將預設定位點間距設定為 100 點,並在 30 點處新增左對齊的定位點。此設定會影響包含定位字元的文字。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat, TabAlignment
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
paragraph.getParagraphFormat().setDefaultTabSize(100)
paragraph.getParagraphFormat().getTabs().add(30, TabAlignment.Left)
presentation.save("paragraph_tabs.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
結果:

設定校對語言
Aspose.Slides 提供 BasePortionFormat.setLanguageId,可為文字部份設定校對語言。校對語言決定 PowerPoint 中拼寫與文法檢查所使用的語言。
以下範例需要「presentation.pptx」且其第一張投影片的第一個圖形為文字方塊,且至少有一個段落。它將第一段內容取代為「1。」、將字型設為 SimSun,並指派簡體中文校對語言 (zh-CN)。結果儲存為「proofing_language.pptx」:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import FontData, Portion, Presentation, SaveFormat
presentation = Presentation("presentation.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
paragraph = auto_shape.getTextFrame().getParagraphs().get_Item(0)
paragraph.getPortions().clear()
font = FontData("SimSun")
text_portion = Portion()
text_portion.getPortionFormat().setComplexScriptFont(font)
text_portion.getPortionFormat().setEastAsianFont(font)
text_portion.getPortionFormat().setLatinFont(font)
# 設定校對語言的 Id。
text_portion.getPortionFormat().setLanguageId("zh-CN")
text_portion.setText("1。")
paragraph.getPortions().add(text_portion)
presentation.save("proofing_language.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
設定預設語言
使用 LoadOptions.setDefaultTextLanguage 定義在載入或建立簡報時所建立文字的預設語言。以下範例建立一個簡報,預設文字語言為美式英語,新增一個文字方塊,並列印其第一個文字部份的語言代碼 en-US。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import LoadOptions, Presentation, ShapeType
load_options = LoadOptions()
load_options.setDefaultTextLanguage("en-US")
presentation = Presentation(load_options)
try:
slide = presentation.getSlides().get_Item(0)
# 新增一個帶文字的矩形圖形。
shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 20, 20, 150, 50)
shape.getTextFrame().setText("Sample text")
# 檢查第一個部份的語言。
portion = shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
print(portion.getPortionFormat().getLanguageId())
finally:
presentation.dispose()
設定預設文字樣式
若要在簡報層級套用預設文字格式,請使用 Presentation.getDefaultTextStyle。
以下範例將新簡報中頂層段落的預設字型設定為 14 點粗體,並儲存為「default_text_style.pptx」。文字可繼承這些預設,除非更具體的格式覆寫它們。
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import NullableBool, Presentation, SaveFormat
presentation = Presentation()
try:
# 取得頂層段落格式。
paragraph_format = presentation.getDefaultTextStyle().getLevel(0)
if paragraph_format is not None:
paragraph_format.getDefaultPortionFormat().setFontHeight(14)
paragraph_format.getDefaultPortionFormat().setFontBold(NullableBool.True_)
presentation.save("default_text_style.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
以全大寫效果擷取文字
在 PowerPoint 中,套用 All Caps 字型效果會使文字在投影片上顯示為大寫,即使原始輸入為小寫。使用 Aspose.Slides 取得此類文字部份時,函式庫會回傳原始輸入的文字。若要符合顯示的文字,請檢查 TextCapType,在值為 All 時將回傳的字串轉為大寫。
此範例需要「sample2.pptx」且其第一張投影片的第一個圖形為文字方塊。其第一段的第一個部份包含「Hello, Aspose!」且套用了全大寫效果,如下圖所示。

以下程式碼示範如何擷取套用 All Caps 效果的文字:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, TextCapType
presentation = Presentation("sample2.pptx")
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().get_Item(0)
text_portion = auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().get_Item(0)
print("Original text: " + str(text_portion.getText()))
text_format = text_portion.getPortionFormat().getEffective()
if text_format.getTextCapType() == TextCapType.All:
text = str(text_portion.getText()).upper()
print("All-Caps effect: " + text)
finally:
presentation.dispose()
輸出:
Original text: Hello, Aspose!
All-Caps effect: HELLO, ASPOSE!
常見問題集
如何修改投影片中表格的文字?
使用 Table 迭代儲存格,然後透過 Cell.getTextFrame 更新每個儲存格,並使用 Paragraph.getParagraphFormat 調整段落格式。
如何在 PowerPoint 投影片的文字上套用漸層顏色?
使用 BasePortionFormat.getFillFormat。將 FillFormat.setFillType 設為 FillType.Gradient,並配置漸層停止點、方向與透明度。