Demo
Readme
Changelog
Releases RSS Feed
Compatibility : nodejs and browser

Summary (version 3.1.8) Buy slides module

This module makes it possible to create multiple slides with a loop in a pptx. For example, you could clone slides for each of your products. The whole order of the slides can be declared from the data.

This module is available as part of the docxtemplater PRO plan.

README

Installation:

You will need docxtemplater v3: npm install docxtemplater

Install this module with npm install --save "$url"

Usage

There are two ways of using the slides module. Explicit, where the developper specifies which slides to repeat, and Tagging, where the template creator chooses which slides to repeat from the template itself by tagging slides with a specific tag.

After installing the module, you can use a working demo by running node sample.js.

Explicit

If you use the explicit method, you write from the code which slides you want to repeat :

const SlidesModule=require('docxtemplater-slides-module')

const slidesModule=new SlidesModule()

const data= {
    "slides": [
        {"$slide":1,name:'Franck',phone:"+665432131"},
        {"$slide":1,name:'Jim',phone:"+6234468468"},
        {"$slide":1,name:'Joe',phone:"+78788787"},
        {"$slide":1,name:'Hugh',phone:"03566456465"},
        {"$slide":3,title:'The Title'},
        {"$slide":2,content:'Lorem ipsum dolor sit amet, consectetur adipisicing elit...'},
    ]
}

docx
    .attachModule(slidesModule)
    .loadZip(zip)
    .setData(data)
    .render()

const buffer= docx
        .getZip()
        .generate({type:"nodebuffer", compression: "DEFLATE"})

fs.writeFile("test.docx",buffer);

And in your template, write :

Slide1
------

{name} {phone}

Slide 2
-------

{content}

Slide 3
-------

{title}

This means that : you want to use the $slide 1 from the template to generate 4 slides in your output document, then you want to use the slide 3 of your template and put it at position 5 in your output, and use the slide 2 of your template to put it at position 6 in your output. All generated slides also have different data (name, phone, title and content in this example).

With the explicit method, it is possible to reorder slides, duplicate them or remove them.

Tagging

In that case, it is the template creator that will tag certain slides to express that some slides must be repeated or removed depending on conditions or loops.

To tag a slide with the text "users", you write {:users} anywher on that slide. Those tags will be removed after generation by docxtemplater.

All slides that are untagged are shown exactly once.

All slides that are tagged are shown depending on the value of the tag.

If the value is an array, the slide is repeated for each element in an array.

If the value is false or falsy, the slide is removed.

If the value is true or truthy, the slide is shown once.

const SlidesModule=require('docxtemplater-slides-module')

const slidesModule=new SlidesModule()

const data= {
    mainTitle: "My title",
    users: [
        {name:'Franck',phone:"+665432131"},
        {name:'Jim',phone:"+6234468468"},
        {name:'Joe',phone:"+78788787"},
        {name:'Hugh',phone:"03566456465"},
    ],
    foo: "bar",
}

docx
    .attachModule(slidesModule)
    .loadZip(zip)
    .setData(data)
    .render()

const buffer= docx
        .getZip()
        .generate({type:"nodebuffer", compression: "DEFLATE"})

fs.writeFile("test.docx",buffer);

And in your template, write :

Slide1
------

{mainTitle}
Slide2
------

{:users}
{name} {phone}

In this case, the first slide will be shown once, and the second slide will be repeated 4 times (since users is an array of 4 elements).

With the tagging module, it is possible to remove slides conditionnally. To do that, you have to tag the slide with the text {:isShown} and set {isShown:false} in your setData() call.

Special Features

Everything that is outside of the "slides" key is automatically accessible from all templated slides.

You can use the special variables {$count} and {$index} in your template to get the index of the current slide, and the total count of the slides (in the expected output).

FAQ

Is it possible to manipulate slides within a presentation ?

Yes, either with the explicit method or the tagging method explained above.

Is it possible to reorder slides ?

Yes, with the explicit method that is possible. It is however not possible with the tagging method, since tagging will keep the same order of the slides as in the template document.

Is it possible to combine two presentations from two pptx files ?

No, this is currently not possible, but could be implemented, contact me if you're interested.

Is it possible to get a JSON representation of the slides ?

Yes, you can do the following to get a JSON data structure :

var zip = new JSZip(content);
var doc = new Docxtemplater();
doc.loadZip(zip);
doc.compile();
console.log(require("util").inspect(this.doc.compiled, {showHidden: true, depth: 2}));

it gives you following JSON object :

{
    "ppt/slides/slide1.xml": {
        "content": "<?xml......",
        ...
    },
    "ppt/slides/slide2.xml": {
        "content": "<?xml......",
        ...
    },
    ...
}

CHANGELOG

3.1.8

  • Move docxtemplater from devDependencies to dependencies

Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-slides-module, the installation will generate a tree of node_modules that puts the module on a level where it has no access to docxtemplater. By explicitly asking it as a dependency, this issue is avoided.

3.1.7

Fix bug of notes being "moved" from one slide to the other during generation

3.1.6

Include notes for each of the rendered slide

3.1.5

Fail properly when a slide does not exist

3.1.4

Add support for boolean values true/false and objects in loops

3.1.3

Bugfix : SlideMasters are now correctly templated

3.1.2

Bugfix when having notes in pptx

3.1.1

Bugfix : Slide 10 and following slides are no longer removed

3.1.0

Add possibility to tag slides for repetition with {:users}

3.0.3

Add compatibility with image-module

3.0.2

Exported files are now ES5

3.0.1

Bugfix : Slides module doesn't throw an error anymore when templating docx files

3.0.0

Initial release

Edgar Hipp

I'm the creator of docxtemplater. I work on making docxtemplater great since 2013.