heavysixer / node-pptx

Generate PPTX files on the server-side with JavaScript.
MIT License
161 stars 44 forks source link

Can't remove slide from presentation #55

Open coder-abhiwagh opened 4 years ago

coder-abhiwagh commented 4 years ago

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

gregdolley commented 4 years ago

@abhiwagh I'll take a look.

gregdolley commented 4 years ago

@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.

heavysixer commented 4 years ago

@gregdolley @abhiwagh ok to close this issue?

harishankar0301 commented 4 years ago

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

abdulloooh commented 4 years ago

@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

rajuy5697 commented 1 year ago

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 ?