heavysixer/node-pptx

Can't remove slide from presentation

coder-abhiwagh opened this issue · 6 comments

I am trying to remove slide from my presentation using code

`const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();

async function load() {
await pptx.load('/app/go/src/ppt2svgconverter/Node-PPTX/hello-world.pptx'); // load a pre-existing PPTX
}

async function removeSlide() {
await pptx.compose(pres => {
pres.removeSlide(pres.getSlide("slide1")); // remove the first slide from the PPTX
// OR ---> pres.removeSlide(pres.getSlide(1)); <--- example of getting a slide by intege
});
}
load();
removeSlide();`

But getting error like node:10115) UnhandledPromiseRejectionWarning: Error: Invalid slide name in Presentation.getSlide(): slide1

PPT I used is attached here
hello-world.pptx

@abhiwagh I'll take a look.

@abhiwagh - The reason for this is because you're calling two async functions back-to-back before waiting for the first one to finish. So initially, when PPTX is instantiated, the pptx it has in memory is one with no slides, and when you call removeSlide() (before the load finishes) you're actually removing slide1 from the blank template, not the file you're trying to load (so the exception of "slide1" doesn't exist, is correct at that point in time). Instead, the code should be:

load().then(() => removeSlide());

This ensures your file finishes loading in PPTX's internal structure, before you call removeSlide() on it. Now it won't crash.

@gregdolley @abhiwagh ok to close this issue?

@abhiwagh Is the size of the presentation decreasing when you remove the slides and save it?

@abhiwagh - The reason for this is because you're calling two async functions back-to-back before waiting for the first one to finish. So initially, when PPTX is instantiated, the pptx it has in memory is one with no slides, and when you call removeSlide() (before the load finishes) you're actually removing slide1 from the blank template, not the file you're trying to load (so the exception of "slide1" doesn't exist, is correct at that point in time). Instead, the code should be:

load().then(() => removeSlide());

This ensures your file finishes loading in PPTX's internal structure, before you call removeSlide() on it. Now it won't crash.

I followed this same approach and actually waited for the file to be loaded with then but nothing was removed from my file, what could be wrong please, I tried several pptx file but to no avail

const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();

async function load() {
await pptx.load('./Investment.pptx'); // load a pre-existing PPTX
}

async function removeSlide() {
await pptx.compose(pres => {
// pres.removeSlide(pres.getSlide("slide1")); // remove the first slide from the PPTX
pres.removeSlide(pres.getSlide(1));
});
}

load().then(() => removeSlide());

this is my code giving me error

node-pptx\node_modules\nodejs-pptx\lib\factories\doc-props\app.js:42
if (this.content['docProps/app.xml']['Properties']['Slides'] !== undefined) {
^

TypeError: Cannot read properties of undefined (reading 'Properties')

Any idea ?