|
1 | 1 | # pptx-automizer: A Powerful PPTX Modifier for Node.js |
2 | 2 |
|
3 | | -pptx-automizer is a Node.js-based PowerPoint (.pptx) generator that automates the manipulation of existing .pptx files. With pptx-automizer, you can merge templates, customize slide content, and maintain your library of .pptx templates. `pptx-automizer` will not write files from scratch, but edit and merge existing pptx files. You can style template slides within PowerPoint, and these templates will be seamlessly integrated into the output presentation. Most of the content can be modified by using callbacks with [xmldom](https://github.com/xmldom/xmldom). |
| 3 | +`pptx-automizer` is a Node.js-based PowerPoint (.pptx) generator that automates the manipulation of existing .pptx files. With pptx-automizer, you can merge templates, customize slide content, and maintain your library of .pptx templates. `pptx-automizer` will not write files from scratch, but edit and merge existing pptx files. You can style template slides within PowerPoint, and these templates will be seamlessly integrated into the output presentation. Most of the content can be modified by using callbacks with [xmldom](https://github.com/xmldom/xmldom). |
4 | 4 |
|
5 | 5 | `pptx-automizer` will fit best to users who try to maintain their own library of pptx template files. This is perfect to anyone who uses complex and well-styled customized layouts. Any existing slide and even a single element can be a data driven template for output pptx files. |
6 | 6 |
|
@@ -130,23 +130,31 @@ const automizer = new Automizer({ |
130 | 130 | // any existing slide in RootTemplate.pptx. Otherwise, we are going to start |
131 | 131 | // with a truncated root template. |
132 | 132 | let pres = automizer |
133 | | - .loadRoot(`RootTemplate.pptx`) |
| 133 | + .loadRoot('RootTemplate.pptx') |
134 | 134 | // We want to make some more files available and give them a handy label. |
135 | | - .load(`SlideWithShapes.pptx`, 'shapes') |
136 | | - .load(`SlideWithGraph.pptx`, 'graph') |
| 135 | + .load('SlideWithShapes.pptx', 'shapes') |
| 136 | + .load('SlideWithGraph.pptx', 'graph') |
137 | 137 | // Skipping the second argument will not set a label. |
138 | | - .load(`SlideWithImages.pptx`); |
| 138 | + .load('SlideWithImages.pptx'); |
| 139 | + |
| 140 | +// Get useful information about loaded templates: |
| 141 | +/* |
| 142 | +const presInfo = await pres.getInfo(); |
| 143 | +const mySlides = presInfo.slidesByTemplate('shapes'); |
| 144 | +const mySlide = presInfo.slideByNumber('shapes', 2); |
| 145 | +const myShape = presInfo.elementByName('shapes', 2, 'Cloud'); |
| 146 | +*/ |
139 | 147 |
|
140 | 148 | // addSlide takes two arguments: The first will specify the source |
141 | 149 | // presentation's label to get the template from, the second will set the |
142 | 150 | // slide number to require. |
143 | 151 | pres |
144 | 152 | .addSlide('graph', 1) |
145 | 153 | .addSlide('shapes', 1) |
146 | | - .addSlide(`SlideWithImages.pptx`, 2); |
| 154 | + .addSlide('SlideWithImages.pptx', 2); |
147 | 155 |
|
148 | 156 | // Finally, we want to write the output file. |
149 | | -pres.write(`myPresentation.pptx`).then((summary) => { |
| 157 | +pres.write('myPresentation.pptx').then((summary) => { |
150 | 158 | console.log(summary); |
151 | 159 | }); |
152 | 160 |
|
@@ -478,12 +486,12 @@ pres.addSlide('charts', 2, (slide) => { |
478 | 486 | ``` |
479 | 487 |
|
480 | 488 | Find out more about modifying charts: |
| 489 | + |
481 | 490 | - [Modify chart axis](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-axis.test.ts) |
482 | 491 | - [Dealing with bubble charts](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-bubbles.test.ts) |
483 | 492 | - [Vertical line charts](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-vertical-lines.test.ts) |
484 | 493 | - [Style chart series and data points](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-existing-chart-styled.test.ts) |
485 | 494 |
|
486 | | - |
487 | 495 | ## Modify extended charts |
488 | 496 |
|
489 | 497 | If you need to modify extended chart types, such like waterfall or map charts, you need to use `modify.setExtendedChartData`. |
@@ -526,6 +534,94 @@ pres |
526 | 534 | }); |
527 | 535 | ``` |
528 | 536 |
|
| 537 | +# Examples |
| 538 | + |
| 539 | +## Loop through an existing presentation |
| 540 | + |
| 541 | +If you would like to modify some elements in a single .pptx file, it is important to that `pptx-automizer` is not able to directly "jump" to a shape and modify. |
| 542 | + |
| 543 | +This is how it works internally: |
| 544 | + |
| 545 | +- Load a root template to append slides to |
| 546 | +- (Probably) load root template again to modify slides |
| 547 | +- Load other templates |
| 548 | +- Append a loaded slide to (probably truncated) root template |
| 549 | +- Modify the recently added slide |
| 550 | +- Write root template and appended slides as output presentation. |
| 551 | + |
| 552 | +In case you need to apply modifications to the root template, you need to load it as a normal template: |
| 553 | + |
| 554 | +```ts |
| 555 | +import Automizer, { |
| 556 | + CmToDxa, |
| 557 | + ISlide, |
| 558 | + ModifyColorHelper, |
| 559 | + ModifyShapeHelper, |
| 560 | + ModifyTextHelper, |
| 561 | +} from 'pptx-automizer'; |
| 562 | + |
| 563 | +const run = async () => { |
| 564 | + const automizer = new Automizer({ |
| 565 | + templateDir: `path/to/pptx-templates`, |
| 566 | + outputDir: `path/to/pptx-output`, |
| 567 | + // this is required to start with no slides: |
| 568 | + removeExistingSlides: true, |
| 569 | + }); |
| 570 | + |
| 571 | + let pres = automizer |
| 572 | + .loadRoot(`SlideWithShapes.pptx`) |
| 573 | + // We load it twice to make it available for modifying slides. |
| 574 | + // Defining a "name" as second params makes it a little easier |
| 575 | + .load(`SlideWithShapes.pptx`, 'myTemplate'); |
| 576 | + |
| 577 | + // This is brandnew: get useful information about loaded templates: |
| 578 | + const myTemplates = await pres.getInfo(); |
| 579 | + const mySlides = myTemplates.slidesByTemplate(`myTemplate`); |
| 580 | + |
| 581 | + // Feel free to create some functions to pre-define all modifications |
| 582 | + // you need to apply to your slides. |
| 583 | + type CallbackBySlideNumber = { |
| 584 | + slideNumber: number; |
| 585 | + callback: (slide: ISlide) => void; |
| 586 | + }; |
| 587 | + const callbacks: CallbackBySlideNumber[] = [ |
| 588 | + { |
| 589 | + slideNumber: 2, |
| 590 | + callback: (slide: ISlide) => { |
| 591 | + slide.modifyElement('Cloud', [ |
| 592 | + ModifyTextHelper.setText('My content'), |
| 593 | + ModifyShapeHelper.setPosition({ |
| 594 | + h: CmToDxa(5), |
| 595 | + }), |
| 596 | + ModifyColorHelper.solidFill({ |
| 597 | + type: 'srgbClr', |
| 598 | + value: 'cccccc', |
| 599 | + }), |
| 600 | + ]); |
| 601 | + }, |
| 602 | + }, |
| 603 | + ]; |
| 604 | + const getCallbacks = (slideNumber: number) => { |
| 605 | + return callbacks.find((callback) => callback.slideNumber === slideNumber) |
| 606 | + ?.callback; |
| 607 | + }; |
| 608 | + |
| 609 | + // We can loop all slides an apply the callbacks if defined |
| 610 | + mySlides.forEach((mySlide) => { |
| 611 | + pres.addSlide('myTemplate', mySlide.number, getCallbacks(mySlide.number)); |
| 612 | + }); |
| 613 | + |
| 614 | + // This will result to an output presentation containing all slides of "SlideWithShapes.pptx" |
| 615 | + pres.write(`myOutputPresentation.pptx`).then((summary) => { |
| 616 | + console.log(summary); |
| 617 | + }); |
| 618 | +}; |
| 619 | + |
| 620 | +run().catch((error) => { |
| 621 | + console.error(error); |
| 622 | +}); |
| 623 | +``` |
| 624 | + |
529 | 625 | ## Sort output slides |
530 | 626 |
|
531 | 627 | There are three ways to arrange slides in an output presentation. |
@@ -701,7 +797,6 @@ This project is deeply inspired by: |
701 | 797 | - [node-pptx](https://github.com/heavysixer/node-pptx) |
702 | 798 | - [docxtemplater](https://github.com/open-xml-templating/docxtemplater) |
703 | 799 |
|
704 | | - |
705 | 800 | ### Commercial Support |
706 | 801 |
|
707 | 802 | If you need commercial support on complex .pptx automation, please take a look at [ensembl.io](https://ensembl.io). |
|
0 commit comments