在 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():
jpile.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()
結果:

停用特定字型的字距微調
在某些情況下,Aspose.Slides 產生的文字看起來比 PowerPoint 中相同文字稍為緊密。這可能是因為 PowerPoint 會忽略某些字型的字距微調資料,即使該字型包含有效的字距微調資訊且在 PowerPoint 設定中已啟用字距微調。
若要使產生的輸出更接近 PowerPoint,您可以對使用受影響字型的文字區段停用字距微調。將 BasePortionFormat.setKerningMinimalSize 設為大於實際字型大小的數值。此範例需要「presentation.pptx」且第一張投影片的第一個形狀為文字方塊。它會檢查有效的字型名稱(包括繼承的字型),並對使用 Roboto 的區段設定 100 點的閾值。這會對字型大小低於 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()
結果:

設定文字旋轉
使用 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()
設定文字定位點
使用 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 效果,如下圖所示:

以下程式碼範例示範如何提取帶有 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!
FAQ
如何在投影片的表格中修改文字?
要在投影片的表格中修改文字,請使用 Table。遍歷儲存格,並透過 Cell.getTextFrame 更新每個儲存格,並透過 Paragraph.getParagraphFormat 更新段落格式。
如何在 PowerPoint 投影片的文字上套用漸層顏色?
要為文字套用漸層顏色,請使用 BasePortionFormat.getFillFormat。將 FillFormat.setFillType 設為 FillType.Gradient,並配置漸層停止點、方向與透明度。