question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feature: reorder a slide

See original GitHub issue

In order to repurpose an existing presentation As a developer using python-pptx I need the ability to change the ordering of slides in a deck

Candidate protocol:

>>> slides = prs.slides
>>> len(slides)
6
>>> slide = slides[2]
>>> slides.index(slide)
2
>>> slide.move_to(5)
>>> slides.index(slide)
5

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:3
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
blaze33commented, Aug 10, 2015

The following works fine for me:

def move_slide(self, presentation, old_index, new_index):
        xml_slides = presentation.slides._sldIdLst  # pylint: disable=W0212
        slides = list(xml_slides)
        xml_slides.remove(slides[old_index])
        xml_slides.insert(new_index, slides[old_index])

def delete_slide(self, presentation,  index):
        xml_slides = presentation.slides._sldIdLst  # pylint: disable=W0212
        slides = list(xml_slides)
        xml_slides.remove(slides[index])
7reactions
Amazinzaycommented, Feb 17, 2021

Because you’re working with lxml.etree._Element objects there, the code can be a little simpler:

def move_slide(slides, slide, new_idx):
    slides._sldIdLst.insert(new_idx, slide._element)

The basic insight here is that lxml does not make copies unless you tell it to. If you stick an element somewhere else, it comes out of wherever it was before.

I can’t think of any edge cases off the top of my head. This is the approach I would use if I was doing it myself.

I was able to get this working with a slight modification.

def move_slide(slides, slide, new_idx):
        slides._sldIdLst.insert(new_idx, slides._sldIdLst[slides.index(slide)])

The slide element is not what is being moved, but rather the sldId Element.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the order in which stacked objects, placeholders, or ...
To reorder objects on a slide, right-click the object (or set of grouped objects), and then click Bring to Front or Send to...
Read more >
How to Rearrange & Navigate PPT & Move a Slide - Multiple ...
This Microsoft PowerPoint 2016 tutorial shows you how to arrange slides within your presentation. I show how to select slides in the slide...
Read more >
Reorder Objects More Easily in PowerPoint's Selection Pane
On the Home tab, in the Editing group, click Select, Selection Pane to display the Selection task pane on the right. · Either...
Read more >
How to Rearrange Slides in PowerPoint 2016's Slide Sorter ...
The Slide Layout task pane appears so that you can select the layout for the new slide. To edit the contents of the...
Read more >
Modifying Your PowerPoint Presentation's Slide Order
To move a slide using the Slides Pane, use the same steps that you would in the Slide Sorter. Click, hold and drag...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found