How to Prevent Duplication when Merging Presentation Slides in Python?

Aspose.Slides for Python via .
When merging slides with the same slide master layout, duplicate layouts are added to the slide master.

For example, this happens when you write code like the following

import aspose.slides as slides

with slides.Presentation("Presentation1.pptx") as pres1: as slides.
    with slides.Presentation("Presentation2.pptx") as pres2:
        for slide in pres2.slides:
            pres1.slides.add_clone(slide)
        pres1.save("combined.pptx", slides.export.SaveFormat.PPTX)

pres1 and pres2 have the same slide master.
How can I prevent duplicates of the same slide master from appearing?

@yuhino,
Thank you for posting the question.

When cloning a slide from one presentation to another, the layout and master slides related to the slide are also copied. Unfortunately, Aspose.Slides cannot prevent the duplication you described. You can read the name and type of layouts (the name and layout_type properties in the LayoutSlide class respectively), compare them with other layout slides and remove duplications. You can use a similar way for master slides.

In addition, you can remove unused layout and master slides as follows:

presentation.layout_slides.remove_unused()
presentation.masters.remove_unused(True)

Slide Layout|Aspose.Slides for Python Documentation
Slide Master|Aspose.Slides for Python Documentation

Thank you for your response.

It helped me a lot.
I would like to ask one more question as well.
Is it possible to change the layout of other slides using the layout name I got?

@yuhino,
Yes, you can change the layout of a slide by the layout name. The following code example shows you how to do this.

import aspose.slides as slides

slide_index = 1
layout_name = "MyLayout"  # for example

with slides.Presentation("sample.pptx") as presentation:
    slide = presentation.slides[slide_index]

    new_layout_slide = next((s for s in presentation.layout_slides if s.name == layout_name), None)

    if new_layout_slide is not None:
        slide.layout_slide = new_layout_slide

    presentation.save("output.pptx", slides.export.SaveFormat.PPTX)

Thank you so much for taking the time to let me know!
I will give it a try!

@yuhino,
Thank you for using Aspose.Slides.