Skip to content
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

Open
a6708051 opened this issue Dec 18, 2024 · 5 comments
Open

I need move a slide from one ppt to another ppt #1036

a6708051 opened this issue Dec 18, 2024 · 5 comments

Comments

@a6708051
Copy link

a6708051 commented Dec 18, 2024

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.

for shape in slide_to_copy.shapes:
    shape_type = str(shape.shape_type)
    if shape_type.startswith('PICTURE'):
        with io.BytesIO(shape.image.blob) as image_stream:
            left = shape.left
            top = shape.top
            width = shape.width
            height = shape.height
            
            try:
                target_slide.shapes.add_picture(image_stream, left, top, width=width, height=height)
            except Exception as e:
                print(f"An error occurred while adding the picture: {e}")
    else:
        new_shape = deepcopy(shape)
        new_shape.left = shape.left
        new_shape.top = shape.top
        new_shape.width = shape.width
        new_shape.height = shape.height
        target_slide.shapes._spTree.insert_element_before(new_shape.element, 'p:extLst')
@a6708051 a6708051 reopened this Dec 18, 2024
@MartinPacker
Copy link

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.

@pulgamecanica
Copy link

pulgamecanica commented Dec 24, 2024

You can copy presentations slides, here is an example on how you can do this:
(tested with: python-pptx 1.0.2 on arch-linux)

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:
https://gist.github.com/pulgamecanica/0eecf91f92d30724839c0ec85f627352

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 🚀

  • Load Multiple Presentations: Seamlessly load PowerPoint presentations (.pptx files).
  • View Slides: Browse through the slides in any loaded presentation.
  • Copy Slides: Copy specific slides to another presentation.
  • Beautiful Console Interface: Thanks to the rich library, enjoy an interactive and visually appealing command-line experience.

How to Use 🛠️

  1. Run the Program: Start the program by executing the Python script.
  2. Load Presentations: Select "Load a Presentation" and provide the path to your .pptx file.
  3. Copy Slides: Choose a source presentation, select the slide to copy, and decide whether to add it to an existing presentation or create a new one.
  4. Repeat as Needed: Keep loading, copying, and saving to your heart's content!

Requirements 📋

  • Required Libraries: pptx, rich

Example Output 🌟

Screenshot From 2024-12-24 16-26-42


Screenshot From 2024-12-24 16-27-47

@a6708051
Copy link
Author

a6708051 commented Jan 6, 2025

You can copy presentations slides, here is an example on how you can do this: (tested with: python-pptx 1.0.2 on arch-linux)

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: https://gist.github.com/pulgamecanica/0eecf91f92d30724839c0ec85f627352

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 🚀

  • Load Multiple Presentations: Seamlessly load PowerPoint presentations (.pptx files).
  • View Slides: Browse through the slides in any loaded presentation.
  • Copy Slides: Copy specific slides to another presentation.
  • Beautiful Console Interface: Thanks to the rich library, enjoy an interactive and visually appealing command-line experience.

How to Use 🛠️

  1. Run the Program: Start the program by executing the Python script.
  2. Load Presentations: Select "Load a Presentation" and provide the path to your .pptx file.
  3. Copy Slides: Choose a source presentation, select the slide to copy, and decide whether to add it to an existing presentation or create a new one.
  4. Repeat as Needed: Keep loading, copying, and saving to your heart's content!

Requirements 📋

  • Required Libraries: pptx, rich

Example Output 🌟

Screenshot From 2024-12-24 16-26-42

Screenshot From 2024-12-24 16-27-47

You can copy presentations slides, here is an example on how you can do this: (tested with: python-pptx 1.0.2 on arch-linux)

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: https://gist.github.com/pulgamecanica/0eecf91f92d30724839c0ec85f627352

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 🚀

  • Load Multiple Presentations: Seamlessly load PowerPoint presentations (.pptx files).
  • View Slides: Browse through the slides in any loaded presentation.
  • Copy Slides: Copy specific slides to another presentation.
  • Beautiful Console Interface: Thanks to the rich library, enjoy an interactive and visually appealing command-line experience.

How to Use 🛠️

  1. Run the Program: Start the program by executing the Python script.
  2. Load Presentations: Select "Load a Presentation" and provide the path to your .pptx file.
  3. Copy Slides: Choose a source presentation, select the slide to copy, and decide whether to add it to an existing presentation or create a new one.
  4. Repeat as Needed: Keep loading, copying, and saving to your heart's content!

Requirements 📋

  • Required Libraries: pptx, rich

Example Output 🌟

Screenshot From 2024-12-24 16-26-42

Screenshot From 2024-12-24 16-27-47

I have try! but when i open pptx file, it show me a error!
image

@pulgamecanica
Copy link

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.

@sashaKorovkina
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants