CBK
CBK

Reputation: 700

Python PPTX: Adding Entire Slide from Another Presentation

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?

I apologize if no developments have been made and this post is a rehash. Thank you in advance.

Upvotes: 10

Views: 9431

Answers (3)

PennyFarthing
PennyFarthing

Reputation: 7

This solution has mainly worked with one exception. After copying the slide over to the destination presentation when I open the destination presentation I get the following warning:

PowerPoint couldn't read some content in xxx.pptx and removed it. Please check your presentation to see if the rest of it looks ok.

The rest of the presentation does look fine and after saving and reopening the warning goes away. Do you know of a way to make it so this warning doesn't appear or if there is a way to resolve it with Python (saving/closing again?).

Upvotes: -2

bwp8nt
bwp8nt

Reputation: 113

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.

Upvotes: 1

Saleh
Saleh

Reputation: 333

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.

Upvotes: 4

Related Questions