Ziv-Barber / officegen

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.
MIT License
2.65k stars 471 forks source link

Add slide from one pptx to another? #311

Open mszbot opened 5 years ago

mszbot commented 5 years ago

Just stumbled across this lib.

Is it possible to add a slide from an existing pptx to another pptx?

Something like:

add_slide_from_pptx(slide_number, copy_from_pptx, add_to_pptx)

Ziv-Barber commented 5 years ago

The short answer is yes and no. The long answer is:

After creating a new slide you can use the addDirectXmlCode method to add xml code directly:

mySlide.addDirectXmlCode('... some xml code ...')

So you can take the code from existing slide, but only the code inside the p:spTree xml element excluding both p:nvGrpSpPr and p:grpSpPr, and feed it into another slide using the addDirectXmlCode method of the slide object that accepting only one parameter - xml code.

It'll not work for you in case that your slide depending on additional resources, like images, etc.

Another option, that can be perfect in some cases, is to use custom layouts:

pptx.makeNewLayout('some layout', {
  display: 'my layout display name',
  xmlCode: myLayoutCode // Same code as you pass into addDirectXmlCode.
})

Example how to use custom layout:

let slide = pptx.makeNewSlide({
  useLayout: 'layout name'
})

// Change a placeholder (title in this case):
slide.addText('Some text', {
  font_size: 44,
  ph: 'title'
})

Hope that it'll help you for now.

mszbot commented 5 years ago

Thanks for the detailed reply Ziv-Barber, much appreciated.

Unfortunately my slides have charts, tables, images and hyperlinks. :(