在 Python via Java 中格式化簡報文字
概述
本文介紹如何使用 Aspose.Slides for Python via Java 來格式化 PowerPoint 與 OpenDocument 簡報中的文字。內容涵蓋背景顏色、透明度、字元間距、字型屬性、旋轉、段落間距、自動適應行為、文字錨點、定位點與語言設定。
以下範例中,我們將使用名為「sample.pptx」的檔案,其中第一張投影片包含一個文字方塊,文字如下:

若要尋找並突出顯示文字或正則表達式的匹配項,請參閱搜尋與取代文字。
設定文字背景顏色
使用ParagraphFormat.getDefaultPortionFormat可為段落設定預設的突顯顏色,或使用PortionFormat.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():
jpace.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()
結果如下:

設定文字透明度
文字透明度是透過指派給PortionFormat.getFillFormat的顏色之 alpha 成分來控制的。在以下範例中,alpha = 50 為 ARGB alpha 通道值,範圍為 0–255,而非透明度百分比。
以下程式碼範例示範如何將透明度套用於整段文字:
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()
結果如下:

設定文字字元間距
使用PortionFormat.setSpacing可擴大或收縮文字方塊中字元之間的間距。
以下 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,可對使用受影響字型的文字部份停用字距微調。將PortionFormat.setKerningMinimalSize 設為遠大於實際字型大小的值:
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()
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_format.setKerningMinimalSize(100)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
此設定可防止對符合條件的文字部份套用字距微調,協助將 Aspose.Slides 的渲染結果與 PowerPoint 的視覺輸出保持一致。
管理文字字型屬性
字型屬性可透過ParagraphFormat.getDefaultPortionFormat 在段落層級設定,或透過PortionFormat 在單一部份設定。
以下程式碼設定整段文字的字型與樣式:套用字型大小、粗體、斜體、點狀底線,以及 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()
結果如下:

以下程式碼範例將相同屬性套用於使用粗體字型的文字部份:
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可為形狀內的文字設定預先定義的方向。
以下程式碼範例將形狀內的文字方向設定為 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 以控制段落間距。這些屬性的使用方式如下:
- 使用正值可將行間距指定為行高的百分比。
- 使用負值可以點 (pt) 為單位指定行間距。
以下程式碼範例示範如何在段落內指定行間距:
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()
結果如下:

設定文字框的自動調整類型
TextFrameFormat.setAutofitType 決定文字超出容器邊界時的行為。使用它可控制文字是縮小、溢出或自動調整形狀大小。
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()
設定文字框的錨點
TextFrameFormat.setAnchoringType 定義文字在形狀內的垂直位置,例如置頂、置中或置底。
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 可在段落中配置定位點。
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 提供PortionFormat.setLanguageId 可為文字部份設定校對語言。校對語言決定在 PowerPoint 中進行拼寫與文法檢查時使用的語言。
以下程式碼範例示範如何為文字部份設定校對語言:
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 可定義在載入或建立簡報時所建立文字的預設語言。
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 pt:
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 的第一張投影片上有以下文字方塊:

以下程式碼範例示範如何提取套用 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 投影片上的文字套用漸層顏色?
要在文字上套用漸層顏色,請使用PortionFormat.getFillFormat。將FillFormat.setFillType 設為FillType.Gradient,並設定漸層停止點、方向與透明度。