-
Notifications
You must be signed in to change notification settings - Fork 541
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
I need move a slide from one ppt to another ppt #1036
Comments
The supported way is to build a presentation based off the one with the slides already in. I'm not the developer (@scanny is) but I'm sure copying in is not supported. It's actually a rather fiddly process to get all the parts right - which is probably why Powerpoint is unhappy. |
You can copy presentations slides, here is an example on how you can do this: def copy_slide(self, source_slide: object, target_presentation: Presentation) -> None:
"""Copies a slide from one presentation to another."""
source_layout = source_slide.slide_layout
dest_slide = target_presentation.slides.add_slide(source_layout)
for shape in source_slide.shapes:
new_element = deepcopy(shape.element)
dest_slide.shapes._spTree.insert_element_before(new_element, "p:extLst")
for rel in source_slide.part.rels.values():
if "notesSlide" not in rel.reltype:
target = rel._target
if "chart" in rel.reltype:
partname = target.package.next_partname(ChartPart.partname_template)
xlsx_blob = target.chart_workbook.xlsx_part.blob
target = ChartPart(
partname, target.content_type, deepcopy(target._element), package=target.package,
)
target.chart_workbook.xlsx_part = EmbeddedXlsxPart.new(xlsx_blob, target.package)
dest_slide.part.rels._add_relationship(rel.reltype, target, rel.rId) I also created a program which allows you to copy slides, have a look here: This tool lets you copy slides between PowerPoint presentations with ease, saving you time and effort. Whether you need to extract specific slides, consolidate content, or build a new presentation from scratch, this program has got you covered! Features 🚀
How to Use 🛠️
Requirements 📋
Example Output 🌟 |
|
I believe this happens because the ID is copied as well, you can try and change the name of the slide, also you can ignore the error and still open the presentation. |
The below code has worked for me. Here you can split the presentation into chunks - you can specify the number of chunks and the amount of slides you want for each chunk. Hence if you want the whole presentation - specify 1 chunk with the total number of slides. This should also work regardless of your operating system (I am on Linux). from pptx import Presentation
import os
def split_pptx(file: str, dest: str, n: int) -> int:
"""
Presentation file splitter
"""
prs = Presentation(file)
splits = [Presentation(file) for _ in range(0, len(prs.slides), n)]
slides = [s.slides._sldIdLst[i * n:n * (i + 1)] for i, s in enumerate(splits)]
for x, rl in enumerate(slides):
part = splits[x]
for slide in part.slides._sldIdLst:
if slide in rl:
continue
rId = slide.rId
part.part.drop_rel(rId)
del part.slides._sldIdLst[part.slides._sldIdLst.index(slide)]
part.save(f'{dest}/{x}_{os.path.basename(file)}')
return len(splits)
if __name__ == '__main__':
split_pptx(
file='/path/to/input/presentation.pptx',
dest='/path/to/output/directory',
n=1
) Hope this helps! |
I've a project and one of the requirements is to move a slide from one presentation to another!
I use this code, but generated PPT shows an error when opened in PowerPoint.
The text was updated successfully, but these errors were encountered: