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

How to remove slides #10

Open
awoerl opened this issue Nov 8, 2020 · 12 comments
Open

How to remove slides #10

awoerl opened this issue Nov 8, 2020 · 12 comments

Comments

@awoerl
Copy link

awoerl commented Nov 8, 2020

Hello,
I have the usecase that I want to remove a slide. Would this be possible and how?

@TZK-
Copy link
Member

TZK- commented Nov 9, 2020

Hello,

At the moment there are no elegant way to do that.

However what I think should work is to proceed like below:

<?php

$basePPTX = new PPTX(__DIR__.'/base.pptx');
$emptyPPTX = new PPTX(__DIR__.'/empty.pptx');

$slides = $basePPTX->getSlides();

// Assuming you want to remove the slide N°2
unset($slides[1]);

$emptyPPTX->addSlides($slides);

// Overwrite the source PPTX
$emptyPPTX->saveAs(__DIR__.'/base.pptx');

But if you want, you can try to implement a PPTX::removeSlide(Slide $slide) method and send us a PR :)

@awoerl
Copy link
Author

awoerl commented Nov 11, 2020 via email

@afischerbrandies
Copy link

Does "empty.pptx" represent an empty PowerPoint file and how does that look like?

@TZK-
Copy link
Member

TZK- commented Nov 11, 2020

With Powerpoint you can create empty PPTX which has no slides in it just by removing every slides.

I think with a file like that, my example should work (I did not test so let me know if it doesn't).

@afischerbrandies
Copy link

The issue is that the addResource() method throws an error, because the p:sldIdLst node does not exist. It also assumes that there are current slides available already to determine the Ids for the new slides.

@afischerbrandies
Copy link

afischerbrandies commented Nov 11, 2020

$currentSlides = $this->content->xpath('p:sldIdLst/p:sldId');
$ref = $this->content->xpath('p:sldIdLst')[0]->addChild('sldId');
$ref->addAttribute('id', (int)end($currentSlides)['id'] + 1);

This part of the code causes the error.

@afischerbrandies
Copy link

I tried to manually add an empty <p:sldIdLst> node to the presentation.xml in the emty.pptx (by unzipping it, adding the node, and zipping again).

And changed the code to

        if ($resource instanceof Slide) {
            $rId = parent::addResource($resource);

            $currentSlides = $this->content->xpath('p:sldIdLst/p:sldId');

            $ref = $this->content->xpath('p:sldIdLst')[0]->addChild('sldId');
            $id = $currentSlides ? (int)end($currentSlides)['id'] + 1 : 256;
            $ref->addAttribute('id', $id);
            $ref->addAttribute('r:id', $rId, $this->namespaces['r']);

            return $rId;
        }

which almost works. The resulting document can be opened and looks fine, but still the following warning appears when opening it in PowerPoint:

_PowerPoint found a problem with content in Test.pptx.
PowerPoint can attempt to repair the presentation.

If you trust the source of this presentation, click Repair._

Any idea of what the issue might be?

@TZK-
Copy link
Member

TZK- commented Nov 15, 2020

PowerPoint is a little capricious when you try to modify these files by hand ... It is also complicated to debug this kind of errors.

I don't have a lot of time at the moment to look into the problem but if you find a suitable solution, don't hesitate to share it with us!

Otherwise, we'll have to wait until we got time to search for a solution.

(Maybe it is easier to implement the deleteSlide instead of trying to load an empty pptx as base).

@afischerbrandies
Copy link

Hi @TZK-

yes, working with those PowerPoint appears messy.
What we require is a function that allows us to take one .pttx file and to create one new .pptx file for each slide in the presentation.
This means that for a .pttx file with three slides, we would have to generate 1.pptx, 2.pptx, 3.pptx, with each new file containing just one slide of the original file.
The styles of each slide must remain unchanged. Also, each generated should just contain the necessary resources for that slide (e.g. images, etc.).

Do you think you could implement that?
If yes, what effort would you estimate for that and what's your hourly rate?
If you are interested in providing that function you can also contact me, alexander.fischerbrandies@contentserv.com

Regards

@wtrv
Copy link

wtrv commented Mar 29, 2023

Hi @TZK- , @alexfibr,

Did you manage to figure out a solution for this problem?
I've been trying to get my head around this but haven't figured out a solution yet.

Thanks!

@afischerbrandies
Copy link

Hello @wtrv,

We haven't solved the requirement with this library; we could not remove or extract individual slides.

The commercial phpdocx library (phpdocx.com) includes some .pptx functionality for removing slides. We were able to solve some of our requirements with that.

@wtrv
Copy link

wtrv commented Apr 13, 2023

No worries, I managed to solve my problem using a custom implementation specific for my needs.
I don't think this is something that can be easily be integrated into this library without a major rewrite however.

Thanks for the reply!

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