Group Presentation Shapes with Python
Overview
This article explains how to work with group shapes in Aspose.Slides. It shows how to add a group shape to a slide, place shapes inside it, and save the updated presentation. It also demonstrates how to access shapes stored inside a group and read their alternative_text values. In addition, the article briefly covers related group-shape capabilities such as nested groups, z-order, and locking options.
Add Group Shapes
Aspose.Slides supports working with group shapes on a slide. This feature lets you build richer presentations by treating multiple shapes as a single object. You can add new group shapes, access existing ones, populate them with child shapes, and read or modify any of their properties. To add a group shape to a slide:
- Create an instance of the Presentation class.
- Get a reference to a slide by index.
- Add a GroupShape to the slide.
- Add shapes to the new group shape.
- Save the modified presentation as a PPTX file.
The example below shows how to add a group shape to a slide.
import aspose.slides as slides
# Instantiate the Presentation class.
with slides.Presentation() as presentation:
# Get the first slide.
slide = presentation.slides[0]
# Add a group shape to the slide.
group_shape = slide.shapes.add_group_shape()
# Add shapes inside the group shape.
group_shape.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 300, 100, 100, 100)
group_shape.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 500, 100, 100, 100)
group_shape.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 300, 300, 100, 100)
group_shape.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 500, 300, 100, 100)
# Write the PPTX file to disk.
presentation.save("group_shape.pptx", slides.export.SaveFormat.PPTX)
Access the Alt Text Property
This section explains how to read the Alt Text of shapes contained within a group shape on a slide using Aspose.Slides. To access the Alt Text of the shapes:
- Instantiate the Presentation class to represent a PPTX file.
- Obtain a reference to the slide by its index.
- Access the slide’s shapes collection.
- Access the GroupShape.
- Read the Alt Text property.
The example below retrieves the Alt Text of shapes contained within group shapes.
import aspose.slides as slides
# Instantiate the Presentation class to open the PPTX file.
with slides.Presentation("group_shape.pptx") as presentation:
# Get the first slide.
slide = presentation.slides[0]
for shape in slide.shapes:
if isinstance(shape, slides.GroupShape):
# Access the group shape.
for child_shape in shape.shapes:
# Access the Alt Text property.
print(child_shape.alternative_text)
FAQ
Is nested grouping (a group inside a group) supported?
Yes. GroupShape has a parent_group property, which directly indicates hierarchy support (a group can be a child of another group).
How do I control the group’s z-order relative to other objects on the slide?
Use the GroupShape’s z_order_position property to inspect its position in the display stack.
Can I prevent moving/editing/ungrouping?
Yes. The group’s lock section is exposed via group_shape_lock, which lets you restrict operations on the object.