Python PPTX | How to manipulate with PowerPoint presentation
If you want to know how to create, read, customize, combine, clone, or convert PowerPoint and OpenOffice presentations using Python without the need for other external software, we got you covered.
Aspose.Slides for Python via .NET is a class library for manipulating presentations, offering a lot of key features such as creating presentations from scratch, converting presentations, managing texts, shapes, tables, animations, previewing slides, exporting slides to PDF, TIFF, XPS, HTML, etc., and many more features. Aspose.Slides for Python via .NET provide demos and working examples to help you get a better understanding of our API.
Our API offers the following features:
• Creating or cloning slides from templates
• Working with PowerPoint tables
• Removing or adding protection on shapes
• Adding MS Excel charts as OleObjects
• Generating presentations from the database
• Protecting presentations
• Creating and modifying charts
• Exporting presentations to PDF, XPS, HTML, JPEG, PNG, SVG, and many more
Supported file formats:
Aspose.Slides for Python via .NET can both load and save the following file formats: PPT, POT, PPS, PPTX, POTX, PPSX, PPTM, PPSM, POTM, ODP, OTP, and save in the following formats: TIFF, EMF, PDF, XPS, JPEG, PNG, GIFF, BMP, SVG, SWF, HTML, XAML.
Below, we’ll show you how to create, open, merge, and save a presentation, and provide code examples.
How to create a PowerPoint (PPTX) presentation using Python
To add a new line within a slide using Aspose.Slides for Python via .NET, follow these steps:
- Make a Presentation class instance
- Get a slide reference using its Indeks
add_auto_shapemethod exposed by
shapesobject add an AutoShape of
- Save the presentation as a PPTX file
Using these steps, we added a line to the first slide of the presentation:
import aspose.slides as slides # Instantiate a Presentation object that represents a presentation file with slides.Presentation() as presentation: slide = presentation.slides slide.shapes.add_auto_shape(slides.ShapeType.LINE, 50, 150, 300, 0) presentation.save("NewPresentation_out.pptx", slides.export.SaveFormat.PPTX)
How to open a presentation using Python
Using Aspose.Slides for Python via .NET, developers can access or modify existing PowerPoint presentations.
Our API provides a Presentation class for opening an existing presentation, and we can use one of the suitable constructors of the Presentation class to create its object based on an existing PowerPoint presentation. In the example given below, we will show how to open a Presentation. We have passed the presentation file name to the constructor of the Presentation class, and we get the total number of slides to print on the screen.
import aspose.slides as slides # Opening the presentation file by passing the file path to the constructor of Presentation class with slides.Presentation("pres.pptx") as pres: # Printing the total number of slides present in the presentation print(pres.slides.length)
And if you have very large presentations (let’s say the presentation size is 3 GB), you can open them with the sample code provided below:
import aspose.slides as slides import os loadOptions = slides.LoadOptions() loadOptions.blob_management_options = slides.BlobManagementOptions() loadOptions.blob_management_options.presentation_locking_behavior = slides.PresentationLockingBehavior.KEEP_LOCKED with slides.Presentation("pres.pptx", loadOptions) as pres: # the huge presentation is loaded and ready to use, but the memory consumption is still low. # make any changes to the presentation. pres.slides.name = "Very large presentation" # presentation will be saved to the other file, the memory consumptions still low during saving. pres.save("veryLargePresentation-copy.pptx", slides.export.SaveFormat.PPTX) # can't do that! IO exception will be thrown, because the file is locked while pres objects will # not be disposed os.remove("pres.pptx") # it's ok to do it here, the source file is not locked by pres object os.remove("pres.pptx")
*If you are creating a presentation that has large objects (very large images, video, audio, etc.), use the Blob facility to reduce memory consumption.
How to merge PowerPoint presentations using Python
Aspose.Slides for Python via .NET allow you to merge presentations in different ways. You can merge PowerPoint presentations with their shapes, formatting, texts, styles, comments, etc. without losing quality or data. With our API, you can merge entire presentations, specific slides, and presentations in one format (PPTX to PPTX, PPT to PPT, etc.), or in different formats (PPTX to PPT, PPTX to ODP, etc.).
To merge presentations you can use
add_clone methods (from the
ISlideCollection interface). Presentation object contains a slides collection, and you can call an
add_clone method from the selected presentation, and in that presentation the slides will be merged.
AddClone (ISlide) method, you can combine slides without worrying that the slides will lose their appearance and styles (default parameters).
In the code below you can see how to merge presentations:
import aspose.slides as slides with slides.Presentation("pres.pptx") as pres1: with slides.Presentation("Presentation1.pptx") as pres2: for slide in pres2.slides: pres1.slides.add_clone(slide) pres1.save("combined.pptx", slides.export.SaveFormat.PPTX)
Merge the presentation and change the style of the slides
If you want to change the style of slides in the output presentation while merging slides, the add_clone (ISlide, IMasterSlide, Boolean) method allows you this.
import aspose.slides as slides with slides.Presentation("pres.pptx") as pres1: with slides.Presentation("Presentation1.pptx") as pres2: for slide in pres2.slides: pres1.slides.add_clone(slide, pres1.masters, allow_clone_missing_layout = True) pres1.save("combined_with_master.pptx", slides.export.SaveFormat.PPTX)
Merge specific slides
Using the code below, you can select and combine specific slides from different presentations, resulting in a single output presentation:
import aspose.slides as slides with slides.Presentation("pres.pptx") as pres1: with slides.Presentation("Presentation1.pptx") as pres2: for slide in pres2.slides: pres1.slides.add_clone(slide, pres1.layout_slides) pres1.save("combined_with_layout.pptx", slides.export.SaveFormat.PPTX)
For more ways to merge presentations, you can see the Merge Presentation section in the documentation.
How to save PowerPoint presentations using Python
With Aspose.Slides for Python via .NET you can save a presentation as a file or stream. Below we will provide a sample code for both methods.
Saving presentation as a files
You can save a presentation as a files by calling the Presentation class Save method.
In the code below you can see how to save a presentation with Aspose.Slides for Python via .NET using Python by passing the file name and saving format to the save method.
import aspose.slides as slides # Instantiate a Presentation object that represents a PPT file with slides.Presentation() as presentation: #...do some work here... # Save your presentation to a file presentation.save("Saved_out.pptx", slides.export.SaveFormat.PPTX)
Saving presentation to streams
One way to save presentations is to save to stream. Simply pass an output stream to the Presentation class Save method. In the code shown below, we’ve created a new Presentation file, added text to the shape, and Save the presentation to the stream.
import aspose.slides as slides # Instantiate a Presentation object that represents a PPT file with slides.Presentation() as presentation: shape = presentation.slides.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 200, 200, 200, 200) # Save your presentation to a stream with open("Save_As_Stream_out.pptx", "bw") as stream: presentation.save(stream, slides.export.SaveFormat.PPTX)
In our documentation, you can find out how to save presentations with predefined View Type, save presentations to Strict Open XML spreadsheet format, or save progress updates in percentage.
Aspose.Slides for Python via .NET are compatible with Python 3.5, 3.6, 3.7, 3.8, 3.9, and in case you write Python code on Linux, you can check additional requirements for Linux.
If you need help with your project, we have experts who will work with you on your project, design a solution, and implement our API according to your needs. We’ll do the hard work so you can focus on your business.