使用 Python 的 AutoFit 強化您的簡報
簡介
預設情況下,當您新增文字方塊時,Microsoft PowerPoint 會使用 Resize shape to fit text 設定 — 它會自動調整文字方塊的大小,以確保文字始終適合其中。

- 當文字方塊中的文字變長或變大時,PowerPoint 會自動放大文字方塊—增加其高度—以容納更多文字。
- 當文字方塊中的文字變短或變小時,PowerPoint 會自動縮小文字方塊—減少其高度—以移除多餘的空間。
在 PowerPoint 中,以下四個重要參數或選項會控制文字方塊的自動調整行為:
- Do not Autofit
- Shrink text on overflow
- Resize shape to fit text
- Wrap text in shape.

Aspose.Slides for Python via Java 提供類似的選項——TextFrameFormat 類別下的某些屬性——允許您在簡報中控制文字方塊的自動調整行為。
調整形狀以符合文字
如果您希望文字方塊中的文字在變更後始終能適合該方塊,必須使用 Resize shape to fit text 選項。要指定此設定,請使用 setAutofitType 方法(來自 TextFrameFormat 類別),並傳入 Shape。

以下 Python 程式碼示範如何在 PowerPoint 簡報中指定文字必須始終適合其方塊:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, ShapeType, Portion, FillType, TextAutofitType, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30, 350, 100)
portion = Portion("lorem ipsum...")
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().add(portion)
text_frame_format = auto_shape.getTextFrame().getTextFrameFormat()
text_frame_format.setAutofitType(TextAutofitType.Shape)
presentation.save("Output-presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
如果文字變長或變大,文字方塊會自動調整大小(高度增高),以確保所有文字都能適合其中。若文字變短,則會相反。
不自動調整
如果您希望文字方塊或形狀在文字變更時保持其尺寸不變,必須使用 Do not Autofit 選項。要指定此設定,請使用 setAutofitType 方法(來自 TextFrameFormat 類別),並傳入 None。

以下 Python 程式碼示範如何在 PowerPoint 簡報中指定文字方塊必須始終保持其尺寸:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, ShapeType, Portion, FillType, TextAutofitType, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30, 350, 100)
portion = Portion("lorem ipsum...")
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().add(portion)
text_frame_format = auto_shape.getTextFrame().getTextFrameFormat()
text_frame_format.setAutofitType(TextAutofitType.None_)
presentation.save("Output-presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
當文字過長而超出方塊時,會溢出。
文字過長時縮小
如果文字過長而無法容納於方塊內,您可以使用 Shrink text on overflow 選項,指定必須縮小文字的大小和間距以使其適合方塊。要設定此選項,請使用 setAutofitType 方法(來自 TextFrameFormat 類別),並傳入 Normal。

以下 Python 程式碼示範如何在 PowerPoint 簡報中指定文字在溢出時縮小:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, ShapeType, Portion, FillType, TextAutofitType, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30, 350, 100)
portion = Portion("lorem ipsum...")
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().add(portion)
text_frame_format = auto_shape.getTextFrame().getTextFrameFormat()
text_frame_format.setAutofitType(TextAutofitType.Normal)
presentation.save("Output-presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Note
使用 Shrink text on overflow 選項時,僅在文字過長而超出方塊時才會套用此設定。文字自動換行
如果您希望形狀內的文字在超出形狀邊框(僅寬度)時自動換行,必須使用 Wrap text in shape 參數。要指定此設定,需使用 setWrapText 方法(來自 TextFrameFormat 類別),並傳入 NullableBool.True_。
以下 Python 程式碼示範如何在 PowerPoint 簡報中使用換行文字設定:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, ShapeType, Portion, FillType, NullableBool, SaveFormat
from java.awt import Color
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
auto_shape = slide.getShapes().addAutoShape(ShapeType.Rectangle, 30, 30, 350, 100)
portion = Portion("lorem ipsum...")
portion.getPortionFormat().getFillFormat().getSolidFillColor().setColor(Color.BLACK)
portion.getPortionFormat().getFillFormat().setFillType(FillType.Solid)
auto_shape.getTextFrame().getParagraphs().get_Item(0).getPortions().add(portion)
text_frame_format = auto_shape.getTextFrame().getTextFrameFormat()
text_frame_format.setWrapText(NullableBool.True_)
presentation.save("Output-presentation.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
常見問答
文字框的內部邊距會影響 AutoFit 嗎?
是。內部邊距(Padding)會減少文字可用的區域,導致 AutoFit 更早啟動——更快縮小字型或調整形狀大小。在微調 AutoFit 之前,請先檢查並調整邊距。
AutoFit 如何與手動和軟換行互動?
強制換行會保留原樣,AutoFit 會依據它們調整字型大小與間距。移除不必要的換行通常可減少 AutoFit 必須縮小文字的力度。
變更主題字型或觸發字型替換會影響 AutoFit 結果嗎?
會。使用不同字形度量的字型替換會改變文字的寬高,從而影響最終的字型大小與換行。任何字型變更或替換後,請重新檢查投影片。