Add or Remove Shapes in PowerPoint Slides using Python

Work with Shapes in PowerPoint in Python

Various types of diagrams are used in PowerPoint presentations to make the content more elaborative. These diagrams are comprised of different shapes such as ellipses, lines, rectangles, and connectors. In this article, you will learn how to add, clone, and remove shapes in PowerPoint slides programmatically in Python.

Python Library to Work with PowerPoint Shapes

Aspose.Slides for Python via .NET is designed to create and manipulate PowerPoint presentations from within the Python applications. We will use this library to work with shapes in PowerPoint slides. You can install it from PyPI using the following pip command.

> pip install aspose.slides

Types of Shapes in PowerPoint

You can create a variety of shapes in PowerPoint presentations using Aspose.Slides for Python. The most commonly used shapes include:

Add a Shape to PowerPoint Slides in Python

To add any shape including ellipse, line, rectangle, add_auto_shape(ShapeType, Single, Single, Single, Single) method is used to which you can pass the type of the shape and other necessary parameters. The ShapeType enum is used to specify the type of the shape. The following are the steps to add a shape to a PowerPoint slide in Python.

  1. Create a presentation or load an existing one using Presentation class.
  2. Obtain the reference of the desired slide from Presentation.slides collection.
  3. Add an Ellipse (or any other shape) using add_auto_shape(ShapeType, Single, Single, Single, Single) method.
  4. Save the presentation using Presentation.save(String, SaveFormat) method.

The following code sample shows how to add a shape to a PowerPoint slide in Python.

The following is the ellipse that we have added using the code sample above.

Add a Shape to PowerPoint PPT in Python

Connect PowerPoint Shapes with a Connector in Python

In diagrams, different shapes are connected to each other using a line called a connector. A connector could be a straight or curved line. Let’s see how to add a connector between two PowerPoint shapes in Python.

  1. First, create an instance of the Presentation class to create or load presentation.
  2. Then, obtain the reference of a slide using Presentation.slides collection.
  3. Add two shapes just like you have added in the previous section and get their references.
  4. Create a connector using add_connector(ShapeType, Single, Single, Single, Single) method.
  5. Join the shapes using Connector.start_shape_connected_to and Connector.end_shape_connected_to properties.
  6. Call Connector.reroute() method to create the shortest automatic connection path.
  7. Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to connect shapes in a PowerPoint slide in Python.

The following is the screenshot of the presentation after connecting shapes with a connector.

Connect PowerPoint Shapes with a Connector in Python

Clone PowerPoint Shapes in Python

Sometimes, you opt to clone shapes instead of creating a new one. To achieve that, Aspose.Slides supports shape cloning. The following are the steps to clone a shape in PowerPoint using Python.

  1. First, use Presentation class to load presentation file.
  2. Obtain the reference of a slide from Presentation.slides collection.
  3. Access the source and destination slide shapes from ISlide.shapes collection.
  4. Clone shapes from the source slide shape collection to destination slide using add_clone() method.
  5. Finally, save the updated presentation file.

The following code sample shows how to clone shapes within PowerPoint slides in Python.

Remove Shapes from PowerPoint Slides in Python

You can also remove shapes from PowerPoint presentations by following the steps below.

  1. Create an instance of the Presentation class to load the PPT/PPTX file.
  2. Access the desired slide from Presentation.slides collection.
  3. Filter the desired shape with a specific text using IShape.alternative_text property.
  4. Remove the shape using ISlide.shapes.remove(IShape) method.
  5. Finally, save the updated presentation file.

The following code sample shows how to remove shapes from a PowerPoint slide in Python.

Get a Free License

You can get a free temporary license to use Aspose.Slides for Python without evaluation limitations.

Conclusion

MS PowerPoint supports a range of shapes to create different types of diagrams such as flow charts. In this article, we have covered how to create PowerPoint shapes such as ellipses, rectangles, and connect them using connectors in Python. Moreover, you have learned how to clone and remove shapes in PowerPoint slides programmatically. You can explore more about Aspose.Slides for Python using the documentation. Also, you can feel free to let us know about your queries via our forum.

See Also