Enhance Your Presentations with AutoFit in Python
By default, when you add a textbox, Microsoft PowerPoint uses the Resize shape to fix text setting for the textbox—it automatically resizes the textbox to ensure its text always fits into it.
- When the text in the textbox becomes longer or bigger, PowerPoint automatically enlarges the textbox—increases its height—to allow it to hold more text.
- When the text in the textbox becomes shorter or smaller, PowerPoint automatically reduces the textbox—decreases its height—to clear redundant space.
In PowerPoint, these are the 4 important parameters or options that control the autofit behavior for a textbox:
- Do not Autofit
- Shrink text on overflow
- Resize shape to fit text
- Wrap text in shape.
Aspose.Slides for Python via .NET provides similar options—some properties under the TextFrameFormat class—that allow you to control the autofit behavior for textboxes in presentations.
Resize Shapes to Fit Text
If you want the text in a box to always fit into that box after changes are made to the text, you have to use the Resize shape to fix text option. To specify this setting, set the autofit_type property from the TextFrameFormat class to SHAPE
.
This Python code shows you how to specify that a text must always fit into its box in a PowerPoint presentation:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 30, 350, 100)
portion = slides.Portion("lorem ipsum...")
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.black
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
auto_shape.text_frame.paragraphs[0].portions.add(portion)
text_frame_format = auto_shape.text_frame.text_frame_format
text_frame_format.autofit_type = slides.TextAutofitType.SHAPE
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
If the text becomes longer or bigger, the textbox will be automatically resized (increase in height) to ensure all the text fits into it. If the text becomes shorter, the reverse occurs.
Do Not Autofit
If you want a textbox or shape to retain its dimensions no matter the changes made to the text it contains, you have to use the Do not Autofit option. To specify this setting, set the autofit_type property from the TextFrameFormat class to NONE
.
This Python code shows you how to specify that a textbox must always retain its dimensions in a PowerPoint presentation:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 30, 350, 100)
portion = slides.Portion("lorem ipsum...")
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.black
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
auto_shape.text_frame.paragraphs[0].portions.add(portion)
text_frame_format = auto_shape.text_frame.text_frame_format
text_frame_format.autofit_type = slides.TextAutofitType.NONE
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
When the text becomes too long for its box, it spills out.
Shrink Text on Overflow
If a text becomes too long for its box, through the Shrink text on overflow option, you can specify that the text’s size and spacing must be reduced to make it fit into its box. To specify this setting, set the autofit_type property from the TextFrameFormat class to NORMAL
.
This Python code shows you how to specify that a text must be shrunk on overflow in a PowerPoint presentation:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 30, 350, 100)
portion = slides.Portion("lorem ipsum...")
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.black
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
auto_shape.text_frame.paragraphs[0].portions.add(portion)
text_frame_format = auto_shape.text_frame.text_frame_format
text_frame_format.autofit_type = slides.TextAutofitType.NORMAL
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Info
When the Shrink text on overflow option is used, the setting gets applied only when the text becomes too long for its box.Wrap Text
If you want the text in a shape to get wrapped inside that shape when the text goes beyond the shape’s border (width only), you have to use the Wrap text in shape parameter. To specify this setting, you have to set the wrap_text property from the TextFrameFormat class to NullableBool.TRUE
.
This Python code shows you how to use the Wrap Text setting in a PowerPoint presentation:
import aspose.slides as slides
import aspose.pydrawing as draw
with slides.Presentation() as presentation:
slide = presentation.slides[0]
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 30, 30, 350, 100)
portion = slides.Portion("lorem ipsum...")
portion.portion_format.fill_format.solid_fill_color.color = draw.Color.black
portion.portion_format.fill_format.fill_type = slides.FillType.SOLID
auto_shape.text_frame.paragraphs[0].portions.add(portion)
text_frame_format = auto_shape.text_frame.text_frame_format
text_frame_format.autofit_type = slides.TextAutofitType.NONE
text_frame_format.wrap_text = slides.NullableBool.TRUE
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Note
If you set thewrap_text
property to NullableBool.FALSE
for a shape, when the text inside the shape becomes longer than the shape’s width, the text gets extended beyond the shape’s borders along a single line.