Read complete article on how to add, remove and clone shapes in PowerPoint using Python: https://blog.aspose.com/2022/02/21/work-with-shapes-in-powerpoint-in-python/
Last active
February 22, 2022 07:41
-
-
Save aspose-com-gists/53925cebedbb57b8fd3e405561a70c25 to your computer and use it in GitHub Desktop.
Add or Remove Shapes in PowerPoint Slides using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# Create presentation | |
with slides.Presentation() as presentation: | |
# Access shapes collection for selected slide | |
shapes = presentation.slides[0].shapes | |
# Add ellipse | |
ellipse = shapes.add_auto_shape(slides.ShapeType.ELLIPSE, 0, 100, 100, 100) | |
# Add auto shape rectangle | |
rectangle = shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 100, 300, 100, 100) | |
# Add connector to connect shapes | |
connector = shapes.add_connector(slides.ShapeType.BENT_CONNECTOR2, 0, 0, 10, 10) | |
# Join shapes using connector | |
connector.start_shape_connected_to = ellipse | |
connector.end_shape_connected_to = rectangle | |
# Call reroute to set the automatic shortest path between shapes | |
connector.reroute() | |
# Save presenation | |
presentation.save("add-connector.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
import aspose.pydrawing as drawing | |
# Create a new presentation | |
with slides.Presentation() as pres: | |
# Get the first slide | |
sld = pres.slides[0] | |
# Add auto shape of ellipse type | |
shp = sld.shapes.add_auto_shape(slides.ShapeType.ELLIPSE, 50, 150, 150, 50) | |
# Apply some formatting to ellipse shape | |
shp.fill_format.fill_type = slides.FillType.SOLID | |
shp.fill_format.solid_fill_color.color = drawing.Color.pink | |
# Apply some formatting to the line of Ellipse | |
shp.line_format.fill_format.fill_type = slides.FillType.SOLID | |
shp.line_format.fill_format.solid_fill_color.color = drawing.Color.purple | |
shp.line_format.width = 5 | |
# Save the PPTX file to disk | |
pres.save("add-ellipse.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.slides as slides | |
# Load presentation | |
with slides.Presentation("add-ellipse.pptx") as presentation: | |
# Access shapes of the source slide | |
sourceShapes = presentation.slides[0].shapes | |
# Add a new blank slide | |
blankLayout = presentation.masters[0].layout_slides.get_by_type(slides.SlideLayoutType.BLANK) | |
destSlide = presentation.slides.add_empty_slide(blankLayout) | |
# Access shapes of destination slide | |
destShapes = destSlide.shapes | |
# Clone shapes | |
destShapes.add_clone(sourceShapes[0], 50, 150 + sourceShapes[0].height) | |
destShapes.add_clone(sourceShapes[0]) | |
destShapes.insert_clone(0, sourceShapes[0], 50, 150) | |
# Save file | |
presentation.save("clone-shapes.pptx", slides.export.SaveFormat.PPTX) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load presentation | |
with slides.Presentation("add-ellipse.pptx") as presentation: | |
# Get the first slide | |
sld = presentation.slides[0] | |
alttext = "User Defined" | |
# Loop through shapes | |
for i in range(len(sld.shapes)): | |
# Find shape by alternative text | |
ashp = sld.shapes[0] | |
if ashp.alternative_text == alttext: | |
sld.shapes.remove(ashp) | |
# Save file | |
presentation.save("remove-shapes.pptx", slides.export.SaveFormat.PPTX) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment