Python: Rearrange Slides in a PowerPoint Document

2024-05-30 09:42:59 Written by  support iceblue
Rate this item
(0 votes)

Rearranging slides in a PowerPoint presentation is a simple but essential skill. Whether you need to change the order of your points, group related slides together, or move a slide to a different location, the ability to efficiently reorganize your slides can help you create a more coherent and impactful presentation.

In this article, you will learn how to rearrange slides in a PowerPoint document in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your system through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Rearrange Slides in a PowerPoint Document in Python

To reorder the slides in PowerPoint, two Presentation objects were created - one for loading the original document, and one for creating a new document. By copying the slides from the original document to the new one in the desired sequence, the slide order could be easily rearranged.

The following are the steps to rearrange slides in a PowerPoint document using Python.

  • Create a Presentation object.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Specify the slide order within a list.
  • Create another Presentation object for creating a new presentation.
  • Add the slides from the original document to the new presentation in the specified order using Presentation.Slides.AppendBySlide() method.
  • Save the new presentation to a PPTX file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()

# Load a PowerPoint file
presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx")

# Specify the new slide order within a list
newSlideOrder = [4,2,1,3]

# Create another Presentation object
new_presentation =  Presentation()

# Remove the default slide
new_presentation.Slides.RemoveAt(0)

# Iterate through the list
for i in range(len(newSlideOrder)):

    # Add the slides from the original PowerPoint file to the new PowerPoint document in the new order
    new_presentation.Slides.AppendBySlide(presentation.Slides[newSlideOrder[i] - 1])

# Save the new presentation to file
new_presentation.SaveToFile("output/NewOrder.pptx", FileFormat.Pptx2019)

# Dispose resources
presentation.Dispose()
new_presentation.Dispose()

Python: Rearrange Slides in a PowerPoint Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.