Python: Group or Ungroup Shapes in PowerPoint

Grouping shapes in PowerPoint can greatly simplify the shape editing process, especially when dealing with complex arrangements of shapes. It allows you to modify the entire group collectively, saving time and effort compared to adjusting each shape individually. This is particularly beneficial when you need to apply consistent formatting or positioning to a set of shapes. Ungrouping shapes provides increased flexibility and customization options. By ungrouping a set of grouped shapes, you regain individual control over each shape. This allows you to make specific modifications, resize or reposition individual shapes, and apply unique formatting or styling as needed. In this article, we will explain how to group and ungroup shapes in PowerPoint in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Group Shapes in PowerPoint in Python

Spire.Presentation for Python provides the ISlide.GroupShapes(shapeList: List) method to group two or more shapes on a specific slide. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Get the first slide using Presentation.Slides[0] property.
  • Add two shapes to the slide using ISlide.Shapes.AppendShape() method.
  • Create a list to store the shapes that need to be grouped.
  • Add the two shapes to the list.
  • Group the two shapes using ISlide.GroupShapes(shapeList: List) method.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Get the first slide
slide = ppt.Slides[0]

# Add two shapes to the slide
rectangle = slide.Shapes.AppendShape(ShapeType.Rectangle, RectangleF.FromLTRB (250, 180, 450, 220))
rectangle.Fill.FillType = FillFormatType.Solid
rectangle.Fill.SolidColor.KnownColor = KnownColors.SkyBlue

rectangle.Line.Width = 0.1
ribbon = slide.Shapes.AppendShape(ShapeType.Ribbon2, RectangleF.FromLTRB (290, 155, 410, 235))
ribbon.Fill.FillType = FillFormatType.Solid
ribbon.Fill.SolidColor.KnownColor = KnownColors.LightPink
ribbon.Line.Width = 0.1

# Add the two shapes to a list
shape_list = []
shape_list.append(rectangle)
shape_list.append(ribbon)

# Group the two shapes
slide.GroupShapes(shape_list)

# Save the resulting document
ppt.SaveToFile("GroupShapes.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Group or Ungroup Shapes in PowerPoint

Ungroup Shapes in PowerPoint in Python

To ungroup the grouped shapes in a PowerPoint document, you need to iterate through all slides in the document and all shapes on each slide, find the grouped shapes and then ungroup them using ISlide.Ungroup(groupShape: GroupShape) method. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load the PowerPoint document using Presentation.LoadFromFile() method.
  • Iterate through all slides in the document.
  • Iterate through all shapes on each slide.
  • Check if the current shape is of GroupShape type. If the result is True, ungroup it using ISlide.Ungroup(groupShape: GroupShape) method.
  • Save the result document using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *

# Create an object of the Presentation class
ppt = Presentation()
# Load a PowerPoint document
ppt.LoadFromFile("GroupShapes.pptx")

# Iterate through all slides in the document
for i in range(ppt.Slides.Count):
    slide = ppt.Slides[i]
    
    # Iterate through all shapes on each slide
    for j in range(slide.Shapes.Count):
        shape = slide.Shapes[j]
        
        # Check if the shape is a grouped shape
        if isinstance(shape, GroupShape):
            groupShape = shape            
            # Ungroup the grouped shape
            slide.Ungroup(groupShape)

# Save the resulting document
ppt.SaveToFile("UngroupShapes.pptx", FileFormat.Pptx2013)
ppt.Dispose()

Python: Group or Ungroup Shapes in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.