There are many presentation document formats — PPT, PPTX, ODP and many more. They all have one common thing — the slides. Each presentation document has one or more slides, and these slides may be treated as containers of different content: text, geometric shapes, raster and vector images, animations, video, audio, comments, embedded objects and many more. The GroupDocs.Editor from its beginning had an ability to edit presentations, but only on a per-slide basis: a user can edit only the content of one slide at a time.
Initially, when the content of the slide was edited, it was only possible to save it as a new one-slide presentation. In particular, the pipeline was the next: user loads a presentation to the Editor class, selects the slide to edit with help of PresentationEditOptions, obtains EditableDocument, edits the HTML-content of the slide, saves this content back to the EditableDocument, and finally generates a new presentation file with one slide using the editor.save() method.
Later this pipeline was expanded — it became possible to insert an edited slide into the original presentation using two ways:
by replacing the original slide, that was initially set for editing, onto its edited version,
and by putting the edited slide to “co-exist” together with its original version before edit.
It is possible not only to edit and replace slides in the resultant presentation, but also delete them from it. The PresentationSaveOptions class contains a slide_numbers_to_delete property, which basically is a list of integers, where each integer represents a number of one slide that should be removed. Slide numbers here are 1-based, so the 1st slide has number 1, not 0.
With the slides removal feature the GroupDocs.Editor performs the next algorithm during saving the presentation:
If the value of the slide_number property in the PresentationSaveOptions instance is set to the default value 0, the GroupDocs.Editor generates a new presentation with one slide inside it. The slide_numbers_to_delete property is ignored regardless of its value.
If the value of the slide_number property in the PresentationSaveOptions instance has some non-zero value, the GroupDocs.Editor treats it as a command to insert the edited slide into the original presentation.
Depending on the insert_as_new_slide property, the GroupDocs.Editor replaces old slide, specified by the value of the slide_number property, on the new one, taken from the EditableDocument instance, or puts the new slide to be placed among existing slides without replacing any of them.
Finally, when all slide insertions and rearrangements are finished, the GroupDocs.Editor reads the value of the slide_numbers_to_delete property, iterates over all numbers and removes specified slides from the presentation.
The final presentation, with updated and deleted slides, is written to the output stream or file.
The example below shows a full roundtrip of an input PPTX file: a presentation is loaded, its first slide is converted to the EditableDocument, then HTML-markup is emitted from the EditableDocument instance, modified, and the edited HTML-markup is converted back to another EditableDocument instance. This edited slide is then inserted into the original presentation, while the slide_numbers_to_delete property is set to remove the original second slide during saving.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.optionsimportPresentationLoadOptions,PresentationEditOptions,PresentationSaveOptionsfromgroupdocs.editor.formatsimportPresentationFormatsdefdeleting_slides_from_presentation():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)# Load input presentation to the Editor and specify loading optionswithEditor("./sample-presentation.pptx",PresentationLoadOptions())aseditor:# Prepare edit options and set the 1st slide to editedit_options=PresentationEditOptions()edit_options.show_hidden_slides=Trueedit_options.slide_number=0# index is 0-based, so this is the 1st slide# Generate EditableDocument with the original content of the slideslide_opened_for_edit=editor.edit(edit_options)# Get the HTML-markup from the EditableDocument with the original contentoriginal_html=slide_opened_for_edit.get_embedded_html()# Emulate HTML content editing in a WYSIWYG-editor in the browser or somewhere elseedited_html=original_html.replace("</body>","<p>Edited content</p></body>")# Generate EditableDocument with the edited contentslide_after_edit=EditableDocument.from_markup(edited_html)# Prepare save options that delete a slide during savingsave_options=PresentationSaveOptions(PresentationFormats.PPTX)# A non-zero slide_number is required so the presentation is rebuilt and deletions are appliedsave_options.slide_number=1# 1-based; insert the edited slide at the 1st positionsave_options.insert_as_new_slide=True# keep the edited slide alongside the original onessave_options.slide_numbers_to_delete=[1]# delete the 1st original slide (1-based)# Save the presentation with the deleted slideeditor.save(slide_after_edit,"./edited-presentation.pptx",save_options)slide_opened_for_edit.dispose()slide_after_edit.dispose()print("Saved presentation with a deleted slide to edited-presentation.pptx")if__name__=="__main__":deleting_slides_from_presentation()
sample-presentation.pptx is the sample file used in this example. Click here to download it.