10

It seems this issue was brought up and requested back in 2015 but I cannot find any updates on it. I'm trying to copy an entire slide (including its text and images) from another presentation into my working presentation by doing something like the following:

prs = Presentation('CurrentPresentation.pptx')
prs1 = Presentation('OtherPresentation.pptx')
# Wanted_Slide = prs1.slides[0]
New_Slide = prs.slides.add_slide(prs1.slide_layout[0])

But all this does is add a totally blank slide (with the wanted slide's background) with slide layout 0, which totally makes sense. I know that's not the right way to do it. I tried the below and it did add a slide, but it was just a duplicate one of what was already in the prs presentation (I guess I found a way to duplicate a slide already in the presentation inadvertently):

def Add_Slide(self):
    xml_slides = prs.slides._sldIdLst
    xml_slides1 = prs1.slides._sldIdLst
    slides = list(xml_slides)
    slides1 = list(xml_slides1)
    xml_slides.append(slides1[0])

The above code is a manipulation of a slide delete method I found online.

Or does anyone have any sort of recommendation on how to completely copy a slide and all of its contents over to a working presentation?

2
  • Did you find out any way of doing this? Commented Jul 11, 2018 at 9:09
  • I did not unfortunately. I tried just about everything I could think of and everything I found online. I'm sorry. If you figure something out, please answer this. :) Commented Jul 11, 2018 at 22:15

2 Answers 2

5

There's an issue in the library's repo that has some code to do this, but it's rough, it doesn't work for all cases.

Sign up to request clarification or add additional context in comments.

Comments

2
from pptx.parts.chart import ChartPart
from pptx.parts.embeddedpackage import EmbeddedXlsxPart
from pptx import Presentation
import copy

def _get_blank_slide_layout(pres):
     layout_items_count = [len(layout.placeholders) for layout in pres.slide_layouts]
     min_items = min(layout_items_count)
     blank_layout_id = layout_items_count.index(min_items)
     return pres.slide_layouts[blank_layout_id]
def move_slide(pres1, pres, index):
    """Duplicate the slide with the given index in pres1 and "moves" it into pres.
    Adds slide to the end of the presentation"""
    source = pres1.slides[index]
    blank_slide_layout = _get_blank_slide_layout(pres)
    dest = pres.slides.add_slide(blank_slide_layout)

    for shape in source.shapes:
        newel = copy.deepcopy(shape.element)
        dest.shapes._spTree.insert_element_before(newel, 'p:extLst')

    for key, value in source.part.rels.items():
        # Make sure we don't copy a notesSlide relation as that won't exist
        if "notesSlide" not in value.reltype:
            target = value._target
            # if the relationship was a chart, we need to duplicate the embedded chart part and xlsx
            if "chart" in value.reltype:
                partname = target.package.next_partname(
                    ChartPart.partname_template)
                xlsx_blob = target.chart_workbook.xlsx_part.blob
                target = ChartPart(partname, target.content_type,
                               copy.deepcopy(target._element), package=target.package)

                target.chart_workbook.xlsx_part = EmbeddedXlsxPart.new(
                    xlsx_blob, target.package)

            dest.part.rels.add_relationship(value.reltype, target,value.rId)

This function is an alteration of what is on the github comments at the location cited above. It works well for me, haven't tested it with charts or anything. It's slightly altered from what was posted on the site, because there was the rough example noted by the original answer, and there was a more thorough answer for duplicating within a presentation. Figured I'd post since it took me a while to get this all put together.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.