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
- Types of PowerPoint Shapes
- Add Shape to PowerPoint Slides
- Add Connector to Shapes in PowerPoint
- Clone a Shape in PowerPoint Slides
- Remove Shapes from PowerPoint Slides
Python Library to Work with PowerPoint Shapes
Aspose.Slides for Python via .NET is designed to create and manipulate PowerPoint presentations from within 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 PPT 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.
- Create a presentation or load an existing one using Presentation class.
- Obtain the reference of the desired slide from Presentation.slides collection.
- Add an Ellipse (or any other shape) using add_auto_shape(ShapeType, Single, Single, Single, Single) method.
- 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.
Connect PowerPoint Shapes with a Connector in Python
In diagrams, different shapes are connected 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.
- First, create an instance of the Presentation class to create or load the presentation.
- Then, obtain the reference of a slide using Presentation.slides collection.
- Add two shapes just like you have added in the previous section and get their references.
- Create a connector using add_connector(ShapeType, Single, Single, Single, Single) method.
- Join the shapes using Connector.start_shape_connected_to and Connector.end_shape_connected_to properties.
- Call Connector.reroute() method to create the shortest automatic connection path.
- 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.
Clone PowerPoint PPT Shapes in Python
Sometimes, you opt to clone shapes instead of creating new ones. To achieve that, Aspose.Slides supports shape cloning. The following are the steps to clone a shape in PowerPoint using Python.
- First, use Presentation class to load the presentation file.
- Obtain the reference of a slide from Presentation.slides collection.
- Access the source and destination slide shapes from ISlide.shapes collection.
- Clone shapes from the source slide shape collection to destination slide using add_clone() method.
- Finally, save the updated presentation file.
The following code sample shows how to clone shapes within PowerPoint slides in Python.
Remove PPT Shapes from PowerPoint Slides in Python
You can also remove shapes from PowerPoint presentations by following the steps below.
- Create an instance of the Presentation class to load the PPT/PPTX file.
- Access the desired slide from Presentation.slides collection.
- Filter the desired shape with a specific text using IShape.alternative_text property.
- Remove the shape using ISlide.shapes.remove(IShape) method.
- Finally, save the updated presentation file.
The following code sample shows how to remove shapes from a PowerPoint slide in Python.
Work with Shapes in PowerPoint PPT - Get a Free License
You can get a free temporary license to manipulate PowerPoint shapes 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 and rectangles, and connect them using connectors in Python. Moreover, you have learned how to clone and remove shapes in PowerPoint slides programmatically.
Python PowerPoint API - Read More
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.