-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: reorder a slide #68
Comments
also inserting a slide rather than just adding it to the end of |
This would be very useful for my application as well. I would like to auto-generate reports from my analysis program based on powerpoint templates. It would be ideal if I could provide the user with tools to insert and reorder slides within my report generation interface, rather than requiring them open the finished product and shuffle the slides around. |
👍 |
@scanny Do you have any tips on how I could develop this ? |
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]) |
@blaze33 Those seem to be working for me as well. Thanks for sharing! |
👍 |
@blaze33 I think you'll find that even though a slide deleted this way doesn't appear in the UI, that it remains in the presentation package (although perhaps PowerPoint clears that up on a subsequent save; that's probably worth a quick experiment). This could be verified with a quick To remove the actual slide{n}.xml "file" from the package (zip archive) you would also need to delete the relationship from the presentation to the slide in question. Something like this perhaps: slide_rId = sldIdLst[slide_idx].rId
slide_part = slide.part
slide_part.drop_rel(slide_rId) This would bear testing thoroughly with slides containing pictures, hyperlinks, etc. to make sure it worked in the general case. I don't believe a slide has any inbound relationships, but if it did you'd need to find and remove those as well. I suppose an internal jump link counts as one of those come to think of it. On the other hand, if just getting it to not show does the trick for you, I can't argue with that :) |
Indeed I'm aware that removing a slide ID from Obviously I'm not working with sensitive data that should really be deleted from the final presentation ! |
What happened with the above? Thanks |
I understand the issue with deleting, but shouldn't moving work the way blaze33 suggested? I implemented something similar: def move_slide(self, slide: Slide, new_index: int):
"""Moves the given slide to position new_index."""
_sldIdLst = self.prs.slides._sldIdLst
old_index = None
for index, entry in enumerate((_sldIdLst.sldId_lst)):
if entry.id == slide.slide_id:
old_index = index
if old_index is not None:
to_move = _sldIdLst[old_index]
list(_sldIdLst).pop(old_index)
_sldIdLst.insert(new_index, to_move) It seems to work so far, and moving slides around doesnt change filesize. Are there edge cases, that would need more testing? |
Because you're working with def move_slide(slides, slide, new_idx):
slides._sldIdLst.insert(new_idx, slide._element) The basic insight here is that 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. |
Hey! |
is this still active? I see the issue open, but no contribution or update in 3 years |
Looks like its still open. I tried all these suggestions and it still doesn't reflect in the PPTX. Can someone help, please. |
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. |
Could anyone show me how to actually call the function to move the slide? Any working small example code would really help. Thanks alot |
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:
The text was updated successfully, but these errors were encountered: