pptx-templates 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AI-INSTRUCTOR.md +2253 -0
- package/CHANGELOG.md +56 -0
- package/LICENSE +23 -0
- package/README.md +84 -2
- package/dist/automizer.d.ts +224 -0
- package/dist/automizer.js +704 -0
- package/dist/automizer.js.map +1 -0
- package/dist/classes/content-type-registry.d.ts +46 -0
- package/dist/classes/content-type-registry.js +170 -0
- package/dist/classes/content-type-registry.js.map +1 -0
- package/dist/classes/element-importer.d.ts +49 -0
- package/dist/classes/element-importer.js +254 -0
- package/dist/classes/element-importer.js.map +1 -0
- package/dist/classes/has-shapes.d.ts +286 -0
- package/dist/classes/has-shapes.js +385 -0
- package/dist/classes/has-shapes.js.map +1 -0
- package/dist/classes/layout.d.ts +42 -0
- package/dist/classes/layout.js +113 -0
- package/dist/classes/layout.js.map +1 -0
- package/dist/classes/master.d.ts +45 -0
- package/dist/classes/master.js +146 -0
- package/dist/classes/master.js.map +1 -0
- package/dist/classes/placeholder-normalizer.d.ts +42 -0
- package/dist/classes/placeholder-normalizer.js +136 -0
- package/dist/classes/placeholder-normalizer.js.map +1 -0
- package/dist/classes/related-content-copier.d.ts +18 -0
- package/dist/classes/related-content-copier.js +114 -0
- package/dist/classes/related-content-copier.js.map +1 -0
- package/dist/classes/shape.d.ts +62 -0
- package/dist/classes/shape.js +194 -0
- package/dist/classes/shape.js.map +1 -0
- package/dist/classes/slide-master-remover.d.ts +44 -0
- package/dist/classes/slide-master-remover.js +201 -0
- package/dist/classes/slide-master-remover.js.map +1 -0
- package/dist/classes/slide-notes-copier.d.ts +26 -0
- package/dist/classes/slide-notes-copier.js +76 -0
- package/dist/classes/slide-notes-copier.js.map +1 -0
- package/dist/classes/slide.d.ts +91 -0
- package/dist/classes/slide.js +286 -0
- package/dist/classes/slide.js.map +1 -0
- package/dist/classes/template.d.ts +137 -0
- package/dist/classes/template.js +311 -0
- package/dist/classes/template.js.map +1 -0
- package/dist/constants/constants.d.ts +11 -0
- package/dist/constants/constants.js +182 -0
- package/dist/constants/constants.js.map +1 -0
- package/dist/enums/chart-type.d.ts +11 -0
- package/dist/enums/chart-type.js +16 -0
- package/dist/enums/chart-type.js.map +1 -0
- package/dist/enums/content-type-map.d.ts +16 -0
- package/dist/enums/content-type-map.js +23 -0
- package/dist/enums/content-type-map.js.map +1 -0
- package/dist/enums/element-type.d.ts +14 -0
- package/dist/enums/element-type.js +20 -0
- package/dist/enums/element-type.js.map +1 -0
- package/dist/errors.d.ts +81 -0
- package/dist/errors.js +100 -0
- package/dist/errors.js.map +1 -0
- package/dist/extensions/index.d.ts +5 -0
- package/dist/extensions/index.js +31 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.d.ts +5 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.js +9 -0
- package/dist/extensions/pptxgenjs-compat/img-broken-png.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/index.d.ts +5 -0
- package/dist/extensions/pptxgenjs-compat/index.js +10 -0
- package/dist/extensions/pptxgenjs-compat/index.js.map +1 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.d.ts +14 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js +139 -0
- package/dist/extensions/pptxgenjs-compat/svg-fallback.js.map +1 -0
- package/dist/extensions/run-merge/index.d.ts +13 -0
- package/dist/extensions/run-merge/index.js +89 -0
- package/dist/extensions/run-merge/index.js.map +1 -0
- package/dist/extensions/template-logic/commands.d.ts +22 -0
- package/dist/extensions/template-logic/commands.js +83 -0
- package/dist/extensions/template-logic/commands.js.map +1 -0
- package/dist/extensions/template-logic/expression.d.ts +61 -0
- package/dist/extensions/template-logic/expression.js +499 -0
- package/dist/extensions/template-logic/expression.js.map +1 -0
- package/dist/extensions/template-logic/images.d.ts +26 -0
- package/dist/extensions/template-logic/images.js +183 -0
- package/dist/extensions/template-logic/images.js.map +1 -0
- package/dist/extensions/template-logic/index.d.ts +14 -0
- package/dist/extensions/template-logic/index.js +64 -0
- package/dist/extensions/template-logic/index.js.map +1 -0
- package/dist/extensions/template-logic/list-commands.d.ts +9 -0
- package/dist/extensions/template-logic/list-commands.js +72 -0
- package/dist/extensions/template-logic/list-commands.js.map +1 -0
- package/dist/extensions/template-logic/renderer.d.ts +85 -0
- package/dist/extensions/template-logic/renderer.js +867 -0
- package/dist/extensions/template-logic/renderer.js.map +1 -0
- package/dist/extensions/template-logic/types.d.ts +62 -0
- package/dist/extensions/template-logic/types.js +3 -0
- package/dist/extensions/template-logic/types.js.map +1 -0
- package/dist/helper/archive/archive-fs.d.ts +45 -0
- package/dist/helper/archive/archive-fs.js +206 -0
- package/dist/helper/archive/archive-fs.js.map +1 -0
- package/dist/helper/archive/archive-jszip.d.ts +31 -0
- package/dist/helper/archive/archive-jszip.js +173 -0
- package/dist/helper/archive/archive-jszip.js.map +1 -0
- package/dist/helper/archive/archive.d.ts +27 -0
- package/dist/helper/archive/archive.js +78 -0
- package/dist/helper/archive/archive.js.map +1 -0
- package/dist/helper/cell-id-helper.d.ts +25 -0
- package/dist/helper/cell-id-helper.js +95 -0
- package/dist/helper/cell-id-helper.js.map +1 -0
- package/dist/helper/compute-src-rect.d.ts +9 -0
- package/dist/helper/compute-src-rect.js +52 -0
- package/dist/helper/compute-src-rect.js.map +1 -0
- package/dist/helper/content-tracker.d.ts +31 -0
- package/dist/helper/content-tracker.js +227 -0
- package/dist/helper/content-tracker.js.map +1 -0
- package/dist/helper/count-helper.d.ts +33 -0
- package/dist/helper/count-helper.js +140 -0
- package/dist/helper/count-helper.js.map +1 -0
- package/dist/helper/css-style-parser.d.ts +52 -0
- package/dist/helper/css-style-parser.js +246 -0
- package/dist/helper/css-style-parser.js.map +1 -0
- package/dist/helper/file-helper.d.ts +36 -0
- package/dist/helper/file-helper.js +150 -0
- package/dist/helper/file-helper.js.map +1 -0
- package/dist/helper/general-helper.d.ts +6 -0
- package/dist/helper/general-helper.js +34 -0
- package/dist/helper/general-helper.js.map +1 -0
- package/dist/helper/generate/generate-pptxgenjs.d.ts +37 -0
- package/dist/helper/generate/generate-pptxgenjs.js +134 -0
- package/dist/helper/generate/generate-pptxgenjs.js.map +1 -0
- package/dist/helper/html-to-multitext-helper.d.ts +84 -0
- package/dist/helper/html-to-multitext-helper.js +490 -0
- package/dist/helper/html-to-multitext-helper.js.map +1 -0
- package/dist/helper/hyperlink-processor.d.ts +54 -0
- package/dist/helper/hyperlink-processor.js +289 -0
- package/dist/helper/hyperlink-processor.js.map +1 -0
- package/dist/helper/image-dimensions.d.ts +21 -0
- package/dist/helper/image-dimensions.js +202 -0
- package/dist/helper/image-dimensions.js.map +1 -0
- package/dist/helper/jszip-helper.d.ts +21 -0
- package/dist/helper/jszip-helper.js +162 -0
- package/dist/helper/jszip-helper.js.map +1 -0
- package/dist/helper/layout-index.d.ts +55 -0
- package/dist/helper/layout-index.js +155 -0
- package/dist/helper/layout-index.js.map +1 -0
- package/dist/helper/logger.d.ts +53 -0
- package/dist/helper/logger.js +73 -0
- package/dist/helper/logger.js.map +1 -0
- package/dist/helper/media-deduplicator.d.ts +42 -0
- package/dist/helper/media-deduplicator.js +88 -0
- package/dist/helper/media-deduplicator.js.map +1 -0
- package/dist/helper/modify-background-helper.d.ts +19 -0
- package/dist/helper/modify-background-helper.js +59 -0
- package/dist/helper/modify-background-helper.js.map +1 -0
- package/dist/helper/modify-chart-helper.d.ts +159 -0
- package/dist/helper/modify-chart-helper.js +642 -0
- package/dist/helper/modify-chart-helper.js.map +1 -0
- package/dist/helper/modify-cleanup-helper.d.ts +69 -0
- package/dist/helper/modify-cleanup-helper.js +343 -0
- package/dist/helper/modify-cleanup-helper.js.map +1 -0
- package/dist/helper/modify-color-helper.d.ts +35 -0
- package/dist/helper/modify-color-helper.js +211 -0
- package/dist/helper/modify-color-helper.js.map +1 -0
- package/dist/helper/modify-helper.d.ts +23 -0
- package/dist/helper/modify-helper.js +61 -0
- package/dist/helper/modify-helper.js.map +1 -0
- package/dist/helper/modify-hyperlink-element.d.ts +9 -0
- package/dist/helper/modify-hyperlink-element.js +34 -0
- package/dist/helper/modify-hyperlink-element.js.map +1 -0
- package/dist/helper/modify-hyperlink-helper.d.ts +42 -0
- package/dist/helper/modify-hyperlink-helper.js +258 -0
- package/dist/helper/modify-hyperlink-helper.js.map +1 -0
- package/dist/helper/modify-image-helper.d.ts +23 -0
- package/dist/helper/modify-image-helper.js +157 -0
- package/dist/helper/modify-image-helper.js.map +1 -0
- package/dist/helper/modify-presentation-helper.d.ts +55 -0
- package/dist/helper/modify-presentation-helper.js +177 -0
- package/dist/helper/modify-presentation-helper.js.map +1 -0
- package/dist/helper/modify-shape-helper.d.ts +111 -0
- package/dist/helper/modify-shape-helper.js +340 -0
- package/dist/helper/modify-shape-helper.js.map +1 -0
- package/dist/helper/modify-table-helper.d.ts +11 -0
- package/dist/helper/modify-table-helper.js +100 -0
- package/dist/helper/modify-table-helper.js.map +1 -0
- package/dist/helper/modify-text-helper.d.ts +62 -0
- package/dist/helper/modify-text-helper.js +244 -0
- package/dist/helper/modify-text-helper.js.map +1 -0
- package/dist/helper/modify-xml-helper.d.ts +52 -0
- package/dist/helper/modify-xml-helper.js +305 -0
- package/dist/helper/modify-xml-helper.js.map +1 -0
- package/dist/helper/multitext-helper.d.ts +116 -0
- package/dist/helper/multitext-helper.js +462 -0
- package/dist/helper/multitext-helper.js.map +1 -0
- package/dist/helper/ppt-paths.d.ts +76 -0
- package/dist/helper/ppt-paths.js +116 -0
- package/dist/helper/ppt-paths.js.map +1 -0
- package/dist/helper/shape-type-detector.d.ts +34 -0
- package/dist/helper/shape-type-detector.js +99 -0
- package/dist/helper/shape-type-detector.js.map +1 -0
- package/dist/helper/text-replace-helper.d.ts +30 -0
- package/dist/helper/text-replace-helper.js +149 -0
- package/dist/helper/text-replace-helper.js.map +1 -0
- package/dist/helper/xml/lnLRTB.d.ts +1 -0
- package/dist/helper/xml/lnLRTB.js +62 -0
- package/dist/helper/xml/lnLRTB.js.map +1 -0
- package/dist/helper/xml/prstGeom.d.ts +1 -0
- package/dist/helper/xml/prstGeom.js +11 -0
- package/dist/helper/xml/prstGeom.js.map +1 -0
- package/dist/helper/xml-elements.d.ts +134 -0
- package/dist/helper/xml-elements.js +538 -0
- package/dist/helper/xml-elements.js.map +1 -0
- package/dist/helper/xml-helper.d.ts +120 -0
- package/dist/helper/xml-helper.js +495 -0
- package/dist/helper/xml-helper.js.map +1 -0
- package/dist/helper/xml-placeholder-helper.d.ts +92 -0
- package/dist/helper/xml-placeholder-helper.js +454 -0
- package/dist/helper/xml-placeholder-helper.js.map +1 -0
- package/dist/helper/xml-pretty-print.d.ts +17 -0
- package/dist/helper/xml-pretty-print.js +88 -0
- package/dist/helper/xml-pretty-print.js.map +1 -0
- package/dist/helper/xml-relationship-helper.d.ts +103 -0
- package/dist/helper/xml-relationship-helper.js +397 -0
- package/dist/helper/xml-relationship-helper.js.map +1 -0
- package/dist/helper/xml-slide-helper.d.ts +107 -0
- package/dist/helper/xml-slide-helper.js +676 -0
- package/dist/helper/xml-slide-helper.js.map +1 -0
- package/dist/helper/xml-template-helper.d.ts +42 -0
- package/dist/helper/xml-template-helper.js +252 -0
- package/dist/helper/xml-template-helper.js.map +1 -0
- package/dist/helper/xmldom-sax-patch.d.ts +18 -0
- package/dist/helper/xmldom-sax-patch.js +58 -0
- package/dist/helper/xmldom-sax-patch.js.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.js +177 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/iarchive.d.ts +43 -0
- package/dist/interfaces/iarchive.js +3 -0
- package/dist/interfaces/iarchive.js.map +1 -0
- package/dist/interfaces/ichart.d.ts +7 -0
- package/dist/interfaces/ichart.js +3 -0
- package/dist/interfaces/ichart.js.map +1 -0
- package/dist/interfaces/icounter.d.ts +7 -0
- package/dist/interfaces/icounter.js +3 -0
- package/dist/interfaces/icounter.js.map +1 -0
- package/dist/interfaces/igenerator.d.ts +4 -0
- package/dist/interfaces/igenerator.js +3 -0
- package/dist/interfaces/igenerator.js.map +1 -0
- package/dist/interfaces/iimage.d.ts +7 -0
- package/dist/interfaces/iimage.js +3 -0
- package/dist/interfaces/iimage.js.map +1 -0
- package/dist/interfaces/ilayout.d.ts +14 -0
- package/dist/interfaces/ilayout.js +3 -0
- package/dist/interfaces/ilayout.js.map +1 -0
- package/dist/interfaces/imaster.d.ts +15 -0
- package/dist/interfaces/imaster.js +3 -0
- package/dist/interfaces/imaster.js.map +1 -0
- package/dist/interfaces/imulti-text.d.ts +50 -0
- package/dist/interfaces/imulti-text.js +3 -0
- package/dist/interfaces/imulti-text.js.map +1 -0
- package/dist/interfaces/ipptxgenjs-slide.d.ts +40 -0
- package/dist/interfaces/ipptxgenjs-slide.js +3 -0
- package/dist/interfaces/ipptxgenjs-slide.js.map +1 -0
- package/dist/interfaces/ipresentation-props.d.ts +16 -0
- package/dist/interfaces/ipresentation-props.js +3 -0
- package/dist/interfaces/ipresentation-props.js.map +1 -0
- package/dist/interfaces/ishape-action.d.ts +18 -0
- package/dist/interfaces/ishape-action.js +3 -0
- package/dist/interfaces/ishape-action.js.map +1 -0
- package/dist/interfaces/ishape.d.ts +5 -0
- package/dist/interfaces/ishape.js +3 -0
- package/dist/interfaces/ishape.js.map +1 -0
- package/dist/interfaces/islide.d.ts +71 -0
- package/dist/interfaces/islide.js +3 -0
- package/dist/interfaces/islide.js.map +1 -0
- package/dist/interfaces/itemplate.d.ts +9 -0
- package/dist/interfaces/itemplate.js +3 -0
- package/dist/interfaces/itemplate.js.map +1 -0
- package/dist/interfaces/pres-template.d.ts +9 -0
- package/dist/interfaces/pres-template.js +3 -0
- package/dist/interfaces/pres-template.js.map +1 -0
- package/dist/interfaces/root-pres-template.d.ts +38 -0
- package/dist/interfaces/root-pres-template.js +3 -0
- package/dist/interfaces/root-pres-template.js.map +1 -0
- package/dist/modify/modify-chart.d.ts +74 -0
- package/dist/modify/modify-chart.js +753 -0
- package/dist/modify/modify-chart.js.map +1 -0
- package/dist/modify/modify-table.d.ts +64 -0
- package/dist/modify/modify-table.js +435 -0
- package/dist/modify/modify-table.js.map +1 -0
- package/dist/shapes/chart.d.ts +91 -0
- package/dist/shapes/chart.js +437 -0
- package/dist/shapes/chart.js.map +1 -0
- package/dist/shapes/diagram.d.ts +33 -0
- package/dist/shapes/diagram.js +167 -0
- package/dist/shapes/diagram.js.map +1 -0
- package/dist/shapes/generic.d.ts +27 -0
- package/dist/shapes/generic.js +118 -0
- package/dist/shapes/generic.js.map +1 -0
- package/dist/shapes/hyperlink.d.ts +27 -0
- package/dist/shapes/hyperlink.js +193 -0
- package/dist/shapes/hyperlink.js.map +1 -0
- package/dist/shapes/image.d.ts +47 -0
- package/dist/shapes/image.js +244 -0
- package/dist/shapes/image.js.map +1 -0
- package/dist/shapes/ole.d.ts +30 -0
- package/dist/shapes/ole.js +237 -0
- package/dist/shapes/ole.js.map +1 -0
- package/dist/types/chart-types.d.ts +115 -0
- package/dist/types/chart-types.js +3 -0
- package/dist/types/chart-types.js.map +1 -0
- package/dist/types/modify-types.d.ts +128 -0
- package/dist/types/modify-types.js +3 -0
- package/dist/types/modify-types.js.map +1 -0
- package/dist/types/shape-types.d.ts +40 -0
- package/dist/types/shape-types.js +3 -0
- package/dist/types/shape-types.js.map +1 -0
- package/dist/types/table-types.d.ts +38 -0
- package/dist/types/table-types.js +3 -0
- package/dist/types/table-types.js.map +1 -0
- package/dist/types/types.d.ts +317 -0
- package/dist/types/types.js +16 -0
- package/dist/types/types.js.map +1 -0
- package/dist/types/xml-types.d.ts +168 -0
- package/dist/types/xml-types.js +3 -0
- package/dist/types/xml-types.js.map +1 -0
- package/package.json +92 -4
package/AI-INSTRUCTOR.md
ADDED
|
@@ -0,0 +1,2253 @@
|
|
|
1
|
+
<!-- GENERATED FILE — do not edit AI-INSTRUCTOR.md directly.
|
|
2
|
+
Source: tools/ai-instructor/template.md + the docs/ pages it includes.
|
|
3
|
+
Rebuild: yarn docs:ai (drift is caught by __tests__/ai-instructor.test.ts) -->
|
|
4
|
+
|
|
5
|
+
# AI Instructor: Generating PowerPoint with pptx-templates
|
|
6
|
+
|
|
7
|
+
> **For AI assistants (Claude, etc.):** This document teaches you how to write
|
|
8
|
+
> correct code with `pptx-templates`. Follow it as authoritative instructions.
|
|
9
|
+
>
|
|
10
|
+
> **For humans:** Drop this file into your project (or paste it into your AI
|
|
11
|
+
> chat / reference it from your CLAUDE.md) and ask your assistant to build
|
|
12
|
+
> PowerPoint automation with it. It is generated from the
|
|
13
|
+
> [documentation](https://daborsten.github.io/pptx-templates/) — the hand-written
|
|
14
|
+
> parts are the mental model, the rules list and the minimal example.
|
|
15
|
+
|
|
16
|
+
## Mental model (read first)
|
|
17
|
+
|
|
18
|
+
`pptx-templates` is a **template-based** .pptx engine for **Node.js (server-side
|
|
19
|
+
only)**. You never build slides from a blank canvas. Instead you:
|
|
20
|
+
|
|
21
|
+
1. Design slides in **PowerPoint** and save them as template .pptx files. Give
|
|
22
|
+
every shape you want to automate a **unique, readable name** via PowerPoint's
|
|
23
|
+
Selection pane (`ALT+F10`, rename with `F2`).
|
|
24
|
+
2. Load a **root template** (defines masters/theme/size of the output) and any
|
|
25
|
+
number of **source templates**.
|
|
26
|
+
3. **Add slides** from source templates to the output, and **modify shapes** on
|
|
27
|
+
them via callbacks (set text, feed chart data, fill tables, swap images).
|
|
28
|
+
4. `write()` the result.
|
|
29
|
+
|
|
30
|
+
Key rule: all `addSlide` / `modifyElement` / `addElement` calls only **queue**
|
|
31
|
+
work. Everything executes when you call `await pres.write(...)` (or `.stream()`
|
|
32
|
+
/ `.getJSZip()`). Nothing is applied before that.
|
|
33
|
+
|
|
34
|
+
If the user has no template file yet, tell them to create one in PowerPoint
|
|
35
|
+
first — that is the intended workflow. Only generate shapes from scratch via the
|
|
36
|
+
PptxGenJS bridge (see below) when there is no reasonable template shape.
|
|
37
|
+
|
|
38
|
+
## Concepts
|
|
39
|
+
|
|
40
|
+
Four things explain almost everything about how `pptx-templates` behaves. Read this page before anything else.
|
|
41
|
+
|
|
42
|
+
### Deferred execution
|
|
43
|
+
|
|
44
|
+
The single most important thing to know: calls like `addSlide()`, `addElement()`
|
|
45
|
+
and `modifyElement()` only **queue** work — including every modification
|
|
46
|
+
callback you pass. Nothing is applied to the output presentation until you call
|
|
47
|
+
`await pres.write(...)` (or `.stream()` / `.getJSZip()`), which executes the
|
|
48
|
+
whole queue in order. Two practical consequences:
|
|
49
|
+
|
|
50
|
+
- A throwing callback or an unresolvable element selector surfaces as a rejected
|
|
51
|
+
`write()` (with a typed error such as `CallbackError`), not at the line where
|
|
52
|
+
you queued it. Set `continueOnError: true` to log a warning and skip the
|
|
53
|
+
failing modification instead.
|
|
54
|
+
- Anything you compute *inside* a callback runs at `write()` time; variables it
|
|
55
|
+
closes over will have their values from that moment, not from when the
|
|
56
|
+
callback was queued.
|
|
57
|
+
|
|
58
|
+
### The template/root model
|
|
59
|
+
|
|
60
|
+
Every build starts from a **root template** (`loadRoot`) that the output
|
|
61
|
+
presentation is based on, plus any number of further templates (`load`) that
|
|
62
|
+
serve as shape and slide sources. This is how it works internally:
|
|
63
|
+
|
|
64
|
+
- Load a root template to append slides to
|
|
65
|
+
- (Probably) load root template again to modify slides
|
|
66
|
+
- Load other templates
|
|
67
|
+
- Append a loaded slide to (probably truncated) root template
|
|
68
|
+
- Modify the recently added slide
|
|
69
|
+
- Write root template and appended slides as output presentation
|
|
70
|
+
|
|
71
|
+
`pptx-templates` is currently limited to _adding_ things to the output
|
|
72
|
+
presentation. If you require the ability to, for instance, modify a specific
|
|
73
|
+
element on a slide within an existing presentation and leave the rest
|
|
74
|
+
untouched, you will need to include all the other slides in the process — see
|
|
75
|
+
[looping through the slides of a presentation](https://daborsten.github.io/pptx-templates/slide-management.md#loop-through-the-slides-of-a-presentation).
|
|
76
|
+
|
|
77
|
+
### 1-based numbering
|
|
78
|
+
|
|
79
|
+
Slide numbers are **1-based**: `addSlide('shapes', 1)` takes the first slide of
|
|
80
|
+
the template labelled `shapes`. The number addresses the slide file inside the
|
|
81
|
+
.pptx — it is not necessarily the position in the final deck. Template files
|
|
82
|
+
are addressed by filename or the label given to `.load()`.
|
|
83
|
+
|
|
84
|
+
### One instance, one output
|
|
85
|
+
|
|
86
|
+
Use **one Automizer instance per presentation build**, and call the output
|
|
87
|
+
method (`write`/`stream`/`getJSZip`) **once** per instance. If you need
|
|
88
|
+
several output files, create a fresh `new Automizer(...)` per file. Separate
|
|
89
|
+
instances are isolated from each other — running several builds concurrently
|
|
90
|
+
in the same process (e.g. `Promise.all` in a server) is supported and covered
|
|
91
|
+
by a regression test.
|
|
92
|
+
|
|
93
|
+
## Basic Example
|
|
94
|
+
|
|
95
|
+
This is a basic example on how to use `pptx-templates` in your code. Before you dive in, make sure you know about [deferred execution](https://daborsten.github.io/pptx-templates/concepts.md) — nothing is applied until `write()` is called. Most of the examples in these docs make use of [these template files](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/pptx-templates).
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import Automizer from 'pptx-templates';
|
|
99
|
+
|
|
100
|
+
// First, let's set some preferences!
|
|
101
|
+
const automizer = new Automizer({
|
|
102
|
+
// this is where your template pptx files are coming from:
|
|
103
|
+
templateDir: `my/pptx/templates`,
|
|
104
|
+
|
|
105
|
+
// use a fallback directory for e.g. generic templates:
|
|
106
|
+
templateFallbackDir: `my/pptx/fallback-templates`,
|
|
107
|
+
|
|
108
|
+
// specify the directory to write your final pptx output files:
|
|
109
|
+
outputDir: `my/pptx/output`,
|
|
110
|
+
|
|
111
|
+
// turn this to true if you want to generally use
|
|
112
|
+
// PowerPoint's creationIds instead of slide numbers
|
|
113
|
+
// or shape names:
|
|
114
|
+
useCreationIds: false,
|
|
115
|
+
|
|
116
|
+
// Always use the original slideMaster and slideLayout of any
|
|
117
|
+
// imported slide:
|
|
118
|
+
autoImportSlideMasters: true,
|
|
119
|
+
|
|
120
|
+
// truncate root presentation and start with zero slides
|
|
121
|
+
removeExistingSlides: true,
|
|
122
|
+
|
|
123
|
+
// activate `cleanup` to eventually remove unused files:
|
|
124
|
+
cleanup: false,
|
|
125
|
+
|
|
126
|
+
// Set a value from 0-9 to specify the zip-compression level.
|
|
127
|
+
// The lower the number, the faster your output file will be ready.
|
|
128
|
+
// Higher compression levels produce smaller files.
|
|
129
|
+
compression: 0,
|
|
130
|
+
|
|
131
|
+
// Please note: An image that is imported more than once (e.g. a logo on
|
|
132
|
+
// several slides) is stored only once in the output file. There is no
|
|
133
|
+
// setting for that, identical media files are always shared.
|
|
134
|
+
|
|
135
|
+
// You can enable 'archiveType' and set mode: 'fs'.
|
|
136
|
+
// This will extract all templates and output to disk.
|
|
137
|
+
// It will not improve performance, but it can help debugging:
|
|
138
|
+
// You don't have to manually extract pptx contents, which can
|
|
139
|
+
// be annoying if you need to look inside your files.
|
|
140
|
+
// archiveType: {
|
|
141
|
+
// mode: 'fs',
|
|
142
|
+
// baseDir: `${__dirname}/../__tests__/pptx-cache`,
|
|
143
|
+
// workDir: 'tmpWorkDir',
|
|
144
|
+
// cleanupWorkDir: true,
|
|
145
|
+
// },
|
|
146
|
+
|
|
147
|
+
// use a callback function to track pptx generation process.
|
|
148
|
+
// statusTracker: myStatusTracker,
|
|
149
|
+
|
|
150
|
+
// Console log verbosity: 0 shows errors only, 1 adds warnings (default),
|
|
151
|
+
// 2 adds info & debug output. You can also inject a custom logger
|
|
152
|
+
// implementing ILogger (or NullLogger for complete silence):
|
|
153
|
+
// logger: new NullLogger(),
|
|
154
|
+
verbosity: 1,
|
|
155
|
+
|
|
156
|
+
// By default, a throwing modification callback or an unresolvable
|
|
157
|
+
// element selector rejects write() with a typed error (CallbackError,
|
|
158
|
+
// ElementNotFoundError). Set to true to log a warning and skip the
|
|
159
|
+
// failing modification instead:
|
|
160
|
+
// continueOnError: false,
|
|
161
|
+
|
|
162
|
+
// Remove all unused placeholders to prevent unwanted overlays:
|
|
163
|
+
cleanupPlaceholders: false,
|
|
164
|
+
|
|
165
|
+
// Use a customized version of PptxGenJS if required:
|
|
166
|
+
// pptxGenJs: PptxGenJS,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Now we can start and load a pptx template.
|
|
170
|
+
// With removeExistingSlides set to 'false', each addSlide will append to
|
|
171
|
+
// any existing slide in RootTemplate.pptx. Otherwise, we are going to start
|
|
172
|
+
// with a truncated root template.
|
|
173
|
+
let pres = automizer
|
|
174
|
+
.loadRoot('RootTemplate.pptx')
|
|
175
|
+
// We want to make some more files available and give them a handy label.
|
|
176
|
+
.load('SlideWithShapes.pptx', 'shapes')
|
|
177
|
+
.load('SlideWithGraph.pptx', 'graph')
|
|
178
|
+
// Skipping the second argument will not set a label.
|
|
179
|
+
.load('SlideWithImages.pptx');
|
|
180
|
+
|
|
181
|
+
// Get useful information about loaded templates:
|
|
182
|
+
/*
|
|
183
|
+
const presInfo = await pres.getInfo();
|
|
184
|
+
// The visible slides of a template, in presentation order. Slides that were
|
|
185
|
+
// removed from the presentation (e.g. by 'removeExistingSlides') are skipped,
|
|
186
|
+
// even if their slide file is still contained in the .pptx.
|
|
187
|
+
const mySlides = presInfo.slidesByTemplate('shapes');
|
|
188
|
+
// 'slide.number' is the number of the slide file inside the .pptx and can be
|
|
189
|
+
// passed to addSlide(); it is not necessarily the position in the deck.
|
|
190
|
+
const mySlide = presInfo.slideByNumber('shapes', 2);
|
|
191
|
+
const myShape = presInfo.elementByName('shapes', 2, 'Cloud');
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
// addSlide takes two arguments: The first will specify the source
|
|
195
|
+
// presentation's label to get the template from, the second will set the
|
|
196
|
+
// slide number to require.
|
|
197
|
+
pres
|
|
198
|
+
.addSlide('graph', 1)
|
|
199
|
+
.addSlide('shapes', 1)
|
|
200
|
+
.addSlide('SlideWithImages.pptx', 2);
|
|
201
|
+
|
|
202
|
+
// Finally, we want to write the output file.
|
|
203
|
+
pres.write('myPresentation.pptx').then((summary) => {
|
|
204
|
+
console.log(summary);
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Templates don't have to come from disk: `load` and `loadRoot` also accept a
|
|
209
|
+
`Buffer` (e.g. from a database or an upload). A name is required for buffered
|
|
210
|
+
templates:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
declare const rootBuffer: Buffer;
|
|
214
|
+
declare const contentBuffer: Buffer;
|
|
215
|
+
|
|
216
|
+
const pres = automizer
|
|
217
|
+
.loadRoot(rootBuffer)
|
|
218
|
+
.load(contentBuffer, 'content');
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
See [Output](https://daborsten.github.io/pptx-templates/output.md) for the other output formats — a `ReadableStream` or the underlying JSZip instance.
|
|
222
|
+
|
|
223
|
+
## Selecting slides and shapes
|
|
224
|
+
|
|
225
|
+
`pptx-templates` needs a selector to find the required shape on a template slide. While an imported .pptx file is identified by filename or custom label, there are different ways to address its slides and shapes.
|
|
226
|
+
|
|
227
|
+
### Select slide by number and shape by name
|
|
228
|
+
|
|
229
|
+
If your .pptx-templates are more or less static and you do not expect them to evolve a lot, it's ok to use the slide number and the shape name to find the proper source of automation.
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
// This will take slide #2 from 'SlideWithGraph.pptx' and expect it
|
|
233
|
+
// to contain a shape called 'ColumnChart':
|
|
234
|
+
pres.addSlide('SlideWithGraph.pptx', 2, (slide) => {
|
|
235
|
+
// `slide` is slide #2 of 'SlideWithGraph.pptx'
|
|
236
|
+
slide.modifyElement('ColumnChart', [
|
|
237
|
+
/* ... */
|
|
238
|
+
]);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// This example will take slide #1 from 'RootTemplate.pptx' and place
|
|
242
|
+
// 'ColumnChart' from slide #2 of 'SlideWithGraph.pptx' on it.
|
|
243
|
+
pres.addSlide('RootTemplate.pptx', 1, (slide) => {
|
|
244
|
+
// `slide` is slide #1 of 'RootTemplate.pptx'
|
|
245
|
+
slide.addElement('SlideWithGraph.pptx', 2, 'ColumnChart', [
|
|
246
|
+
/* ... */
|
|
247
|
+
]);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// In case you have two or more shapes on your template slide with the same name,
|
|
251
|
+
// you can address the target by nameIdx:
|
|
252
|
+
pres.addSlide('SlideWithGraph.pptx', 1, (slide) => {
|
|
253
|
+
// Starting from 0, this will find the second shape named 'ColumnChart'
|
|
254
|
+
// on the slide.
|
|
255
|
+
slide.modifyElement(
|
|
256
|
+
{
|
|
257
|
+
name: 'ColumnChart',
|
|
258
|
+
nameIdx: 1,
|
|
259
|
+
},
|
|
260
|
+
[
|
|
261
|
+
/* ... */
|
|
262
|
+
],
|
|
263
|
+
);
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
> You can display and manage shape names directly in PowerPoint by opening the "Selection"-pane for your current slide. Hit `ALT+F10` and PowerPoint will give you a (nested) list including all (grouped) shapes. You can edit a shape name by double-click or by hitting `F2` after selecting a shape from the list. [See MS-docs for more info.](https://support.microsoft.com/en-us/office/use-the-selection-pane-to-manage-objects-in-documents-a6b2fd3e-d769-46c1-9b9c-b94e04a72550)
|
|
268
|
+
|
|
269
|
+
But be aware: Whenever your template slides are rearranged or a template shape is renamed, you need to update your code as well.
|
|
270
|
+
|
|
271
|
+
Please also make sure that each shape to add or modify has a unique name on its slide. Otherwise, only the last matching shape will be taken as target.
|
|
272
|
+
|
|
273
|
+
### Select slides by creationId
|
|
274
|
+
|
|
275
|
+
Additionally, each slide and shape is stored together with a (more or less) unique `creationId`. In XML, it looks like this:
|
|
276
|
+
|
|
277
|
+
```xml
|
|
278
|
+
<p:cNvPr name="MyPicture" id="64">
|
|
279
|
+
<a:extLst>
|
|
280
|
+
<a:ext uri="{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}">
|
|
281
|
+
<a16:creationId id="{0980FF19-E7E7-493C-8D3E-15B2100EA940}" xmlns:a16="http://schemas.microsoft.com/office/drawing/2014/main"/>
|
|
282
|
+
</a:ext>
|
|
283
|
+
</a:extLst>
|
|
284
|
+
</p:cNvPr>
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
This is where `name` and `creationId` are coupled together for each shape.
|
|
288
|
+
|
|
289
|
+
While our shape could now be identified by both, `MyPicture` or by `{0980FF19-E7E7-493C-8D3E-15B2100EA940}`, `creationIds` for slides consist of an integer value, e.g. `501735012` below:
|
|
290
|
+
|
|
291
|
+
```xml
|
|
292
|
+
<p:extLst>
|
|
293
|
+
<p:ext uri="{BB962C8B-B14F-4D97-AF65-F5344CB8AC3E}">
|
|
294
|
+
<p14:creationId val="501735012" xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main"/>
|
|
295
|
+
</p:ext>
|
|
296
|
+
</p:extLst>
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
You can add a simple code snippet to get a list of the available `creationIds` of your loaded templates:
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
const pres = automizer
|
|
303
|
+
.loadRoot(`RootTemplate.pptx`)
|
|
304
|
+
.load(`SlideWithShapes.pptx`, 'shapes');
|
|
305
|
+
|
|
306
|
+
const creationIds = await pres.setCreationIds();
|
|
307
|
+
|
|
308
|
+
// This is going to print the slide creationId and a list of all
|
|
309
|
+
// shapes from slide #1 in `SlideWithShapes.pptx` (aka `shapes`).
|
|
310
|
+
console.log(
|
|
311
|
+
creationIds
|
|
312
|
+
.find((template) => template.name === 'shapes')
|
|
313
|
+
.slides.find((slide) => slide.number === 1),
|
|
314
|
+
);
|
|
315
|
+
// Find the corresponding slide-creationId and -number on top of this list.
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
If your templates are not final and if you expect to have new slides and shapes added in the future, it is worth the effort and use `creationId` in general:
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
const automizer = new Automizer({
|
|
322
|
+
templateDir: `${__dirname}/pptx-templates`,
|
|
323
|
+
outputDir: `${__dirname}/pptx-output`,
|
|
324
|
+
// turn this to true and use creationIds for both, slides and shapes
|
|
325
|
+
useCreationIds: true,
|
|
326
|
+
});
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
Regarding shapes, it is also possible to use a `creationId` and the shape name as a fallback. These are the different types of a `FindElementSelector`:
|
|
330
|
+
|
|
331
|
+
```ts
|
|
332
|
+
import { FindElementSelector } from 'pptx-templates';
|
|
333
|
+
|
|
334
|
+
// This is default when set up with `useCreationIds: true`:
|
|
335
|
+
const myShapeSelectorCreationId: FindElementSelector =
|
|
336
|
+
'{E43D12C3-AD5A-4317-BC00-FDED287C0BE8}';
|
|
337
|
+
|
|
338
|
+
// pptx-generator will try to find the shape even if one of the given keys
|
|
339
|
+
// won't match any shape on the target slide:
|
|
340
|
+
const myShapeSelectorFallback: FindElementSelector = {
|
|
341
|
+
creationId: '{E43D12C3-AD5A-4317-BC00-FDED287C0BE8}',
|
|
342
|
+
name: 'Drum',
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
// Use this only if `useCreationIds: false`:
|
|
346
|
+
const myShapeSelectorName: FindElementSelector = 'Drum';
|
|
347
|
+
|
|
348
|
+
// Whenever `useCreationIds` was set to true, you need to replace slide numbers
|
|
349
|
+
// by `creationId`, too:
|
|
350
|
+
await pres.addSlide('shapes', 4167997312, (slide) => {
|
|
351
|
+
// slide is now #1 of `SlideWithShapes.pptx`
|
|
352
|
+
slide.addElement('shapes', 273148976, {
|
|
353
|
+
creationId: '{E43D12C3-AD5A-4317-BC00-FDED287C0BE8}',
|
|
354
|
+
name: 'Drum',
|
|
355
|
+
});
|
|
356
|
+
// 'Drum' is from #2 of `SlideWithShapes.pptx`, see __tests__ dir for an
|
|
357
|
+
// example.
|
|
358
|
+
});
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
If you decide to use the `creationId` method, you are safe to add, remove and rearrange slides in your templates. It is also no problem to update shape names, and you also don't need to pay attention to unique shape names per slide.
|
|
362
|
+
|
|
363
|
+
> Please note: PowerPoint is going to update a shape's `creationId` only in case the shape was copied & pasted on a slide with an already existing identical shape `creationId`. If you were copying a slide, each shape `creationId` will be copied, too. As a result, you have unique shape ids, but different slide `creationIds`. If you are now going to paste a shape an such a slide, a new creationId will be given to the pasted shape. As a result, slide ids are unique throughout a presentation, but shape ids are unique only on one slide.
|
|
364
|
+
|
|
365
|
+
### Find and Modify Shapes
|
|
366
|
+
|
|
367
|
+
There are basically two ways to access a target shape on a slide:
|
|
368
|
+
|
|
369
|
+
- `slide.modifyElement(...)` requires an existing shape on the current slide,
|
|
370
|
+
- `slide.addElement(...)` adds a shape from another slide to the current slide.
|
|
371
|
+
|
|
372
|
+
Modifications can be applied to both in the same way:
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
import { modify, CmToDxa } from 'pptx-templates';
|
|
376
|
+
|
|
377
|
+
pres.addSlide('shapes', 2, (slide) => {
|
|
378
|
+
// This will only work if there is a shape called 'Drum'
|
|
379
|
+
// on slide #2 of the template labelled 'shapes'.
|
|
380
|
+
slide.modifyElement('Drum', [
|
|
381
|
+
// You can use some of the builtin modifiers to edit a shape's xml:
|
|
382
|
+
modify.setPosition({
|
|
383
|
+
// set position from the left to 5 cm
|
|
384
|
+
x: CmToDxa(5),
|
|
385
|
+
// or use a number in DXA unit
|
|
386
|
+
h: 5000000,
|
|
387
|
+
w: 5000000,
|
|
388
|
+
}),
|
|
389
|
+
// Log your target xml into the console:
|
|
390
|
+
modify.dump,
|
|
391
|
+
]);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
pres.addSlide('shapes', 1, (slide) => {
|
|
395
|
+
// This will import the 'Drum' shape from
|
|
396
|
+
// slide #2 of the template labelled 'shapes'.
|
|
397
|
+
slide.addElement('shapes', 2, 'Drum', [
|
|
398
|
+
// add modifiers as seen in the example above
|
|
399
|
+
]);
|
|
400
|
+
});
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### Inspect a slide from inside the callback
|
|
404
|
+
|
|
405
|
+
Besides `pres.getInfo()` (see the [Basic Example](https://daborsten.github.io/pptx-templates/getting-started.md#basic-example)),
|
|
406
|
+
an added slide can describe itself at `write()` time. Prefer inspecting over
|
|
407
|
+
guessing shape names — an unresolvable selector rejects `write()` (or is
|
|
408
|
+
skipped with `continueOnError: true`), so listing what is actually there beats
|
|
409
|
+
retrying name variations:
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
pres.addSlide('myTemplate.pptx', 1, async (slide) => {
|
|
413
|
+
// All shapes on this slide, with name, id, position and type:
|
|
414
|
+
const elements = await slide.getAllElements();
|
|
415
|
+
// Only certain tags, e.g. text-bearing shapes:
|
|
416
|
+
const shapes = await slide.getAllElements(['sp']);
|
|
417
|
+
// The slide's dimensions:
|
|
418
|
+
const dimensions = await slide.getDimensions();
|
|
419
|
+
});
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
### Find all text elements on a slide
|
|
423
|
+
|
|
424
|
+
When processing an added slide, you might want to apply a modifier to any existing text element. Call `slide.getAllTextElementIds()` for this:
|
|
425
|
+
|
|
426
|
+
```ts
|
|
427
|
+
import Automizer, { modify } from 'pptx-templates';
|
|
428
|
+
|
|
429
|
+
pres.addSlide('myTemplate.pptx', 1, async (slide) => {
|
|
430
|
+
const elements = await slide.getAllTextElementIds();
|
|
431
|
+
elements.forEach((element) => {
|
|
432
|
+
// element has a text body:
|
|
433
|
+
slide.modifyElement(element, [modify.setText('my text')]);
|
|
434
|
+
// ... or use the tag replace function:
|
|
435
|
+
slide.modifyElement(element, [
|
|
436
|
+
modify.replaceText([
|
|
437
|
+
{
|
|
438
|
+
replace: 'TAG',
|
|
439
|
+
by: {
|
|
440
|
+
text: 'my tag text',
|
|
441
|
+
},
|
|
442
|
+
},
|
|
443
|
+
]),
|
|
444
|
+
]);
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
```
|
|
448
|
+
|
|
449
|
+
## Modify Text
|
|
450
|
+
|
|
451
|
+
You can select and import generic shapes from any loaded template. It is possible to update the containing text in several ways:
|
|
452
|
+
|
|
453
|
+
```ts
|
|
454
|
+
import { ModifyTextHelper, XmlElement } from 'pptx-templates';
|
|
455
|
+
|
|
456
|
+
pres.addSlide('SlideWithImages.pptx', 1, (slide) => {
|
|
457
|
+
// You can directly modify the child nodes of <p:sp>
|
|
458
|
+
slide.addElement('shapes', 2, 'Arrow', (element: XmlElement) => {
|
|
459
|
+
element.getElementsByTagName('a:t').item(0).textContent = 'Custom content';
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
// You might prefer a built-in function to set text:
|
|
463
|
+
slide.addElement('shapes', 2, 'Arrow', [
|
|
464
|
+
ModifyTextHelper.setText('This is my text'),
|
|
465
|
+
]);
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
### Replace tagged text
|
|
470
|
+
|
|
471
|
+
`pptx-templates` also provides a powerful helper to replace tagged text. You can use e.g. `{{myTag}}` on your slide and apply a modifier to insert dynamic text. Font style can be inherited from template or updated by the modifier.
|
|
472
|
+
|
|
473
|
+
```ts
|
|
474
|
+
import { modify } from 'pptx-templates';
|
|
475
|
+
|
|
476
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
477
|
+
slide.modifyElement(
|
|
478
|
+
// This is the name of the target element on slide #1 of
|
|
479
|
+
// 'TextReplace.pptx
|
|
480
|
+
'replaceText',
|
|
481
|
+
// This will look for a string `{{replace}}` inside the text
|
|
482
|
+
// contents of 'replaceText' shape
|
|
483
|
+
modify.replaceText([
|
|
484
|
+
{
|
|
485
|
+
replace: 'replace',
|
|
486
|
+
by: {
|
|
487
|
+
text: 'Apples',
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
]),
|
|
491
|
+
);
|
|
492
|
+
});
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
### MultiText: styled paragraphs and lists
|
|
496
|
+
|
|
497
|
+
You can use `modify.setMultiText` to replace all text contents of an existing textfield by styled paragraphs, bulleted lists and text runs:
|
|
498
|
+
|
|
499
|
+
```ts
|
|
500
|
+
import { modify } from 'pptx-templates';
|
|
501
|
+
|
|
502
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
503
|
+
slide.modifyElement(
|
|
504
|
+
'setText',
|
|
505
|
+
modify.setMultiText([
|
|
506
|
+
{
|
|
507
|
+
paragraph: {
|
|
508
|
+
bullet: true,
|
|
509
|
+
level: 0,
|
|
510
|
+
marginLeft: 41338,
|
|
511
|
+
indent: -87325,
|
|
512
|
+
alignment: 'l',
|
|
513
|
+
},
|
|
514
|
+
textRuns: [
|
|
515
|
+
{
|
|
516
|
+
text: 'Bullet point level 0',
|
|
517
|
+
style: {
|
|
518
|
+
isItalics: true,
|
|
519
|
+
color: {
|
|
520
|
+
type: 'srgbClr',
|
|
521
|
+
value: 'CCCCCC',
|
|
522
|
+
},
|
|
523
|
+
},
|
|
524
|
+
},
|
|
525
|
+
],
|
|
526
|
+
},
|
|
527
|
+
]),
|
|
528
|
+
);
|
|
529
|
+
});
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
Within a text run, `\n` and `\v` (U+000B, the character PowerPoint itself uses for
|
|
533
|
+
a soft line break created with Shift+Enter) are converted into an `<a:br/>` line
|
|
534
|
+
break inside the same paragraph. Add another entry to the array if you need a real
|
|
535
|
+
paragraph with its own bullet, level and alignment.
|
|
536
|
+
|
|
537
|
+
`paragraph.lineSpacing`, `spaceBefore` and `spaceAfter` take points as a plain
|
|
538
|
+
number, or `{ percent: 100 }` for spacing relative to the line height (100 =
|
|
539
|
+
one line) — the relative form scales with the paragraph's font size.
|
|
540
|
+
|
|
541
|
+
### Convert HTML to text contents
|
|
542
|
+
|
|
543
|
+
It is also possible to directly convert an HTML page into pptx text contents. HTML code will be flattened and converted into a MultiText array.
|
|
544
|
+
|
|
545
|
+
```ts
|
|
546
|
+
import { modify } from 'pptx-templates';
|
|
547
|
+
|
|
548
|
+
const html =
|
|
549
|
+
'<html><body>' +
|
|
550
|
+
'<h2 style="text-align: center">Quarterly report</h2>' +
|
|
551
|
+
'<p>Plain text with <strong>bold</strong>, <em>italics</em> and ' +
|
|
552
|
+
'<span style="color: #ff0000; font-size: 12pt">styling</span>.</p>' +
|
|
553
|
+
'<ul>' +
|
|
554
|
+
'<li>bullet level 0' +
|
|
555
|
+
'<ul><li>bullet level 1</li></ul>' +
|
|
556
|
+
'</li>' +
|
|
557
|
+
'</ul>' +
|
|
558
|
+
'<ol><li>numbered</li><li>list</li></ol>' +
|
|
559
|
+
'<p><a href="https://example.com">external link</a> and ' +
|
|
560
|
+
'<a href="3">a link to slide 3</a></p>' +
|
|
561
|
+
'</body></html>';
|
|
562
|
+
|
|
563
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
564
|
+
slide.modifyElement('setText', modify.htmlToMultiText(html));
|
|
565
|
+
});
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
#### What HTML is supported
|
|
569
|
+
|
|
570
|
+
PPTX text is strictly flat: a text body is a list of paragraphs, each a list of
|
|
571
|
+
text runs, with no nesting anywhere. HTML hierarchy is therefore *projected*
|
|
572
|
+
onto that — nested inline tags become one run with accumulated character
|
|
573
|
+
properties, nested lists become paragraphs with a 0-based level, and a block
|
|
574
|
+
inside a block (`<li><p>…</p></li>`) yields a single paragraph, with the
|
|
575
|
+
innermost block winning.
|
|
576
|
+
|
|
577
|
+
| | Supported |
|
|
578
|
+
|---|---|
|
|
579
|
+
| Paragraphs | `<p>`, `<div>`, `<h1>`–`<h6>`, `<blockquote>`, `<pre>`, `<section>` & friends |
|
|
580
|
+
| Lists | `<ul>`, `<ol>`, `<li>`, nested to 9 levels. `<ol>` renders as real automatic numbering (1. / a. / i. per level) |
|
|
581
|
+
| Inline | `<strong>`/`<b>`, `<em>`/`<i>`, `<u>`/`<ins>`, `<s>`/`<strike>`/`<del>`, `<sub>`, `<sup>`, `<code>`/`<kbd>`/`<samp>` (monospace), `<mark>`, `<br>`, `<span>`, `<a>`, `<font>` |
|
|
582
|
+
| Links | `<a href="https://…">` external, `<a href="3">` to slide 3 |
|
|
583
|
+
| CSS (on any element) | `font-size` (`px` converted at 96dpi, or `pt`), `color`, `background-color` (highlight), `font-weight`, `font-style`, `text-decoration`, `font-family`, `text-align` |
|
|
584
|
+
|
|
585
|
+
Both list-nesting styles work and produce identical output — properly nested
|
|
586
|
+
(`<li>text<ul>…</ul></li>`) and the sibling form CKEditor emits
|
|
587
|
+
(`<ul><li/><ul>…</ul></ul>`).
|
|
588
|
+
|
|
589
|
+
Good to know:
|
|
590
|
+
|
|
591
|
+
- The input has to be wrapped in `<html><body>…</body></html>`, and is parsed as
|
|
592
|
+
XML: quote your attributes and close your container tags, as WYSIWYG editors
|
|
593
|
+
do. ` `, `&` and an unclosed `<br>` are fine.
|
|
594
|
+
- Colors can be written in any CSS notation (`#f00`, `red`, `rgb(255,0,0)`) and
|
|
595
|
+
are normalized to the 6-digit hex OOXML requires.
|
|
596
|
+
- Relative font sizes (`em`, `%`) are ignored rather than guessed, leaving the
|
|
597
|
+
size inherited from the template.
|
|
598
|
+
- Whitespace collapses the way a browser collapses it; ` ` survives.
|
|
599
|
+
- Vertical spacing mirrors the browser's default stylesheet: `<p>`, headings
|
|
600
|
+
and the outer edges of lists get one collapsed gap of one line height
|
|
601
|
+
(`spaceBefore: { percent: 100 }`, so it scales with each paragraph's font
|
|
602
|
+
size), while items of the same list sit tight. A trailing `<br>` before a
|
|
603
|
+
closing block tag is dropped, exactly as a browser renders it; a deliberate
|
|
604
|
+
`<br><br>` keeps its one empty line.
|
|
605
|
+
- Alignment is only written when the HTML asks for it — otherwise the target
|
|
606
|
+
shape's layout keeps deciding.
|
|
607
|
+
- Font size and color of the target shape's existing text are used as the
|
|
608
|
+
fallback style, so generated text keeps the template's look.
|
|
609
|
+
- `<table>` markup has no equivalent in a single text shape: the cell text is
|
|
610
|
+
kept, but flattened into one paragraph. Use `modify.setTableData` for
|
|
611
|
+
[tables](https://daborsten.github.io/pptx-templates/tables.md).
|
|
612
|
+
- `<a href="4">` is a slide **number in the finished output deck**, counting the
|
|
613
|
+
root template's existing slides — not the index of your `addSlide()` calls.
|
|
614
|
+
The target slide has to exist, or the relationship dangles and PowerPoint
|
|
615
|
+
shows the text underlined but unlinked, without a warning.
|
|
616
|
+
- A `color` on an `<a>` element is written to the run, but PowerPoint paints
|
|
617
|
+
hyperlink text in the theme's `<a:hlink>` color regardless. To restyle links,
|
|
618
|
+
change that theme color; per-link colors are not achievable in PPTX.
|
|
619
|
+
|
|
620
|
+
### Text helpers (MultiText/HTML)
|
|
621
|
+
|
|
622
|
+
Generate complex text (multiple runs, links, bullets) either from a structured value or directly from HTML.
|
|
623
|
+
|
|
624
|
+
```ts
|
|
625
|
+
// From structured paragraphs
|
|
626
|
+
slide.modifyElement('TextBox', [
|
|
627
|
+
ModifyTextHelper.setMultiText([
|
|
628
|
+
{
|
|
629
|
+
paragraph: { bullet: false },
|
|
630
|
+
textRuns: [
|
|
631
|
+
{ text: 'Hello ', style: { isBold: true } },
|
|
632
|
+
{ text: 'World' },
|
|
633
|
+
],
|
|
634
|
+
},
|
|
635
|
+
]),
|
|
636
|
+
]);
|
|
637
|
+
|
|
638
|
+
// From HTML - note the required <html><body> wrapper
|
|
639
|
+
const html =
|
|
640
|
+
'<html><body><p><b>Bold</b> and ' +
|
|
641
|
+
'<a href="https://example.com">link</a></p></body></html>';
|
|
642
|
+
slide.modifyElement('TextBox', [ModifyTextHelper.htmlToMultiText(html)]);
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
`HtmlToMultiTextHelper` and `MultiTextHelper` also support hyperlinks: an
|
|
646
|
+
external target (`<a href="https://...">`) or a slide number for an internal
|
|
647
|
+
link (`<a href="3">`). See [What HTML is supported](#what-html-is-supported)
|
|
648
|
+
above for the full tag and CSS coverage, and the tests:
|
|
649
|
+
|
|
650
|
+
- [Replace text by MultiText objects](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-multi-text.test.ts)
|
|
651
|
+
- [Replace text by HTML](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-multi-text-html.test.ts)
|
|
652
|
+
- [HTML conversion rules, unit level](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/html-to-multitext-converter.test.ts)
|
|
653
|
+
|
|
654
|
+
### Find out more
|
|
655
|
+
|
|
656
|
+
- [Replace and style by tags](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-tagged-text.test.ts)
|
|
657
|
+
- [Modify text elements using getAllTextElementIds](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/get-all-text-element-ids.test.ts)
|
|
658
|
+
- [Replace text by multitext objects](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-multi-text.test.ts)
|
|
659
|
+
- [Soft line breaks inside a text run](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-multi-text-linebreaks.test.ts)
|
|
660
|
+
- [Replace text by HTML](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/replace-multi-text-html.test.ts)
|
|
661
|
+
|
|
662
|
+
## Template Logic
|
|
663
|
+
|
|
664
|
+
`modify.renderTemplate` goes further than `replaceText`. You write the logic
|
|
665
|
+
straight into the text of your slide, in the style of
|
|
666
|
+
[docx-templates](https://github.com/guigrpa/docx-templates), and pass the data
|
|
667
|
+
when you build the deck:
|
|
668
|
+
|
|
669
|
+
```text
|
|
670
|
+
Hello {{user.name}}!
|
|
671
|
+
{{IF user.orders > 10}}
|
|
672
|
+
Thank you for being a loyal customer.
|
|
673
|
+
{{ELSE}}
|
|
674
|
+
Have a look at our new offers.
|
|
675
|
+
{{END-IF}}
|
|
676
|
+
{{FOR item IN items}}
|
|
677
|
+
{{$idx + 1}}. {{item.name}}: {{item.price.toFixed(2)}} €
|
|
678
|
+
{{END-FOR}}
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
```ts
|
|
682
|
+
import { modify } from 'pptx-templates';
|
|
683
|
+
|
|
684
|
+
const data = {
|
|
685
|
+
user: { name: 'Ada', orders: 12 },
|
|
686
|
+
items: [
|
|
687
|
+
{ name: 'Apples', price: 1.5 },
|
|
688
|
+
{ name: 'Pears', price: 2 },
|
|
689
|
+
],
|
|
690
|
+
upper: (text: string) => text.toUpperCase(),
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
694
|
+
// A single shape (or table) ...
|
|
695
|
+
slide.modifyElement('setText', modify.renderTemplate(data));
|
|
696
|
+
});
|
|
697
|
+
|
|
698
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
699
|
+
// ... or every shape and table on the slide
|
|
700
|
+
slide.modify(modify.renderTemplate(data));
|
|
701
|
+
});
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
### Commands
|
|
705
|
+
|
|
706
|
+
| Tag | Effect |
|
|
707
|
+
|---|---|
|
|
708
|
+
| `{{expr}}`, `{{= expr}}`, `{{INS expr}}` | Insert the value. `null`/`undefined` insert nothing, arrays are joined with `, `, and `\n` becomes a line break |
|
|
709
|
+
| `{{IF expr}}` … `{{ELSE-IF expr}}` … `{{ELSE}}` … `{{END-IF}}` | Keep the first branch whose condition is truthy |
|
|
710
|
+
| `{{FOR item IN expr}}` … `{{END-FOR}}` | Repeat the content for each entry. Inside the loop, `item` and `$item` are the entry and `$idx` is the 0-based index |
|
|
711
|
+
| `{{EXEC name = expr; other = expr}}`, `{{! …}}` | Set variables for the rest of the slide. Nothing is inserted |
|
|
712
|
+
| `{{ALIAS name command}}` … `{{*name}}` | Define a shorthand for a command, e.g. `{{ALIAS fullName INS user.first + ' ' + user.last}}` |
|
|
713
|
+
| `{{HTML expr}}` | Insert HTML as formatted paragraphs (bold, italic, lists, links, …) through the [HTML converter](https://daborsten.github.io/pptx-templates/text.md) |
|
|
714
|
+
| `{{LINK expr}}` | Insert a hyperlink. `expr` is a URL or `({ url, label })`. A number as `url` links to that slide of the output |
|
|
715
|
+
| `{{IMAGE expr}}` | Place a picture where the shape is (see [Images](#images)). Needs `slide.modify` |
|
|
716
|
+
|
|
717
|
+
Keywords are case-insensitive. A block can sit inside one paragraph
|
|
718
|
+
(`Status: {{IF ok}}fine{{ELSE}}broken{{END-IF}}`) or span paragraphs. A
|
|
719
|
+
paragraph that holds nothing but control tags (`IF`, `ELSE`, `END-FOR`,
|
|
720
|
+
`EXEC`, `ALIAS`, `HTML`, …) is removed from the output, so you can put each
|
|
721
|
+
tag on its own line, as in the example above.
|
|
722
|
+
|
|
723
|
+
Variables set with `EXEC` are visible in all later tags of the same render
|
|
724
|
+
call, also in later shapes, and inside loops they update the variable of the
|
|
725
|
+
enclosing scope:
|
|
726
|
+
|
|
727
|
+
```text
|
|
728
|
+
{{EXEC total = 0}}
|
|
729
|
+
{{FOR item IN items}}{{! total = total + item.price}}{{END-FOR}}
|
|
730
|
+
Total: {{total.toFixed(2)}}
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
Your `data` is never changed: an `EXEC` that assigns a name from `data`
|
|
734
|
+
defines a new variable that hides it.
|
|
735
|
+
|
|
736
|
+
### Tables
|
|
737
|
+
|
|
738
|
+
If a block opens in a cell and is not closed in that same cell, it spans
|
|
739
|
+
table rows. `{{FOR row IN rows}}` in the first cell and `{{END-FOR}}` in the
|
|
740
|
+
last cell of a row repeats that row once per entry. An `IF` placed the same
|
|
741
|
+
way keeps or removes the row. A row that holds nothing but control tags is
|
|
742
|
+
dropped. `ELSE` cannot span rows. Merged cells (`rowSpan`) are not adjusted,
|
|
743
|
+
and the table frame's height is set to the sum of its row heights.
|
|
744
|
+
|
|
745
|
+
### Images
|
|
746
|
+
|
|
747
|
+
`{{IMAGE …}}` takes an object like in docx-templates:
|
|
748
|
+
|
|
749
|
+
```ts
|
|
750
|
+
import fs from 'fs';
|
|
751
|
+
import { modify } from 'pptx-templates';
|
|
752
|
+
|
|
753
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
754
|
+
// IMAGE writes media files, so it only works through slide.modify()
|
|
755
|
+
slide.modify(
|
|
756
|
+
modify.renderTemplate({
|
|
757
|
+
logo: fs.readFileSync('images/logo.png'),
|
|
758
|
+
photos: ['data:image/jpeg;base64,/9j/4AAQ…'],
|
|
759
|
+
}),
|
|
760
|
+
);
|
|
761
|
+
});
|
|
762
|
+
```
|
|
763
|
+
|
|
764
|
+
```text
|
|
765
|
+
{{IMAGE ({ data: logo, extension: '.png', width: 4, alt: 'Logo' })}}
|
|
766
|
+
{{FOR p IN photos}}{{IMAGE ({ data: p })}}{{END-FOR}}
|
|
767
|
+
```
|
|
768
|
+
|
|
769
|
+
| Property | Effect |
|
|
770
|
+
|---|---|
|
|
771
|
+
| `data` | `Buffer`, `ArrayBuffer`, base64 string or data URL (`data:image/png;base64,…`) |
|
|
772
|
+
| `extension` | `.png`, `.jpg`, `.jpeg` or `.gif`. Optional for data URLs |
|
|
773
|
+
| `width`, `height` | Size in cm. With only one of them, the other keeps the aspect ratio. Without both, the picture fits into the shape, keeping its aspect ratio |
|
|
774
|
+
| `alt` | Alternative text |
|
|
775
|
+
| `rotation` | Rotation in degrees, clockwise |
|
|
776
|
+
|
|
777
|
+
PPTX text cannot contain pictures, so the picture is placed **at the shape
|
|
778
|
+
that holds the tag**, centered in its frame. Several pictures from one shape,
|
|
779
|
+
for example from a `FOR` loop, are placed side by side, each centered in an
|
|
780
|
+
equal share of the frame. If no text is left in the shape after rendering,
|
|
781
|
+
the shape is removed and only the pictures remain.
|
|
782
|
+
|
|
783
|
+
- The shape needs its own position and size. Placeholders that inherit
|
|
784
|
+
them from the layout are not supported.
|
|
785
|
+
- `IMAGE` in table cells is not supported.
|
|
786
|
+
- Identical images are stored only once in the output.
|
|
787
|
+
- `IMAGE` writes a media file, so it needs `slide.modify(modify.renderTemplate(…))`.
|
|
788
|
+
A `modifyElement()` callback cannot add files and rejects with an error.
|
|
789
|
+
|
|
790
|
+
### Expressions
|
|
791
|
+
|
|
792
|
+
Expressions support property paths (`a.b`, `a[0]`, `a['key']`, `a?.b`),
|
|
793
|
+
string/number/boolean literals, object and array literals
|
|
794
|
+
(`({ url: x, label: 'Home' })`, `[1, 2]`), arrow functions
|
|
795
|
+
(`items.filter(i => i.active)`, `(a, b) => a - b`), `! - + * / %`,
|
|
796
|
+
comparisons, `== != === !==`, `&& || ??` and `cond ? a : b`. You can call
|
|
797
|
+
your own functions (passed in `data`) and methods of the values
|
|
798
|
+
(`name.toUpperCase()`).
|
|
799
|
+
|
|
800
|
+
They are evaluated by a small built-in interpreter, not with `eval`, so a
|
|
801
|
+
template cannot run arbitrary code: there are no assignments outside `EXEC`,
|
|
802
|
+
no `new`, and keys like `constructor`, `prototype` or `__proto__` are
|
|
803
|
+
rejected. Typographic quotes inserted by PowerPoint's AutoCorrect (`“Ada”`)
|
|
804
|
+
are valid string delimiters.
|
|
805
|
+
|
|
806
|
+
Wrap an object literal in parentheses: `{{LINK ({ url: x })}}`. Without them,
|
|
807
|
+
the `}}` of the object closes the tag.
|
|
808
|
+
|
|
809
|
+
### Async data
|
|
810
|
+
|
|
811
|
+
`data` can be a Promise, and functions in `data` can return Promises. The
|
|
812
|
+
result of every tag is awaited, so `{{FOR p IN loadProjects()}}` works. To use
|
|
813
|
+
properties of an async result, store it first:
|
|
814
|
+
`{{EXEC user = loadUser()}}{{user.name}}`.
|
|
815
|
+
|
|
816
|
+
### Options
|
|
817
|
+
|
|
818
|
+
```ts
|
|
819
|
+
import { modify } from 'pptx-templates';
|
|
820
|
+
|
|
821
|
+
pres.addSlide('TextReplace.pptx', 1, (slide) => {
|
|
822
|
+
slide.modify(
|
|
823
|
+
modify.renderTemplate(
|
|
824
|
+
{ price: 2 },
|
|
825
|
+
{
|
|
826
|
+
cmdDelimiter: ['[[', ']]'], // or openingTag / closingTag
|
|
827
|
+
additionalJsContext: {
|
|
828
|
+
money: (n: number) => `${n.toFixed(2)} €`,
|
|
829
|
+
},
|
|
830
|
+
rejectNullish: true,
|
|
831
|
+
errorHandler: (error, command) => `#ERR ${command}`,
|
|
832
|
+
},
|
|
833
|
+
),
|
|
834
|
+
);
|
|
835
|
+
});
|
|
836
|
+
```
|
|
837
|
+
|
|
838
|
+
| Option | Default | Effect |
|
|
839
|
+
|---|---|---|
|
|
840
|
+
| `openingTag`, `closingTag` | `{{`, `}}` | Tag delimiters |
|
|
841
|
+
| `cmdDelimiter` | | docx-templates spelling: one string for both sides (`'+++'`) or `[open, close]` |
|
|
842
|
+
| `additionalJsContext` | | Extra helpers and constants. `data` takes precedence |
|
|
843
|
+
| `strict` | `false` | Throw on unknown top-level names instead of treating them as `undefined` |
|
|
844
|
+
| `rejectNullish` | `false` | Throw when an insertion is `null` or `undefined` |
|
|
845
|
+
| `errorHandler(error, command)` | | Called when a command fails. Its return value is used instead (inserted text, condition, list, …) |
|
|
846
|
+
| `failFast` | `true` | `false`: keep going and throw one error listing all failed commands at the end |
|
|
847
|
+
| `processLineBreaks` | `true` | Turn `\n` in inserted values into line breaks |
|
|
848
|
+
|
|
849
|
+
### Listing the commands of a template
|
|
850
|
+
|
|
851
|
+
`listTemplateCommands` reads a .pptx and returns every tag, slide by slide,
|
|
852
|
+
the way `listCommands` does in docx-templates. Use it to check a template, or
|
|
853
|
+
to find out which data it expects:
|
|
854
|
+
|
|
855
|
+
```ts
|
|
856
|
+
import { listTemplateCommands } from 'pptx-templates';
|
|
857
|
+
|
|
858
|
+
const commands = await listTemplateCommands('templates/Offer.pptx');
|
|
859
|
+
// [{ slide: 1, shape: 'Title', raw: '{{IF user.vip}}', type: 'IF', code: 'user.vip' }, …]
|
|
860
|
+
```
|
|
861
|
+
|
|
862
|
+
### Good to know
|
|
863
|
+
|
|
864
|
+
- PowerPoint often splits text into several runs, even inside a tag (spell
|
|
865
|
+
check, partial edits). Tags are joined across runs, and the inserted value
|
|
866
|
+
takes the formatting of the run where the tag starts.
|
|
867
|
+
- An unknown name evaluates to `undefined`, so `{{IF user.vip}}` is simply
|
|
868
|
+
false when `vip` is missing.
|
|
869
|
+
- Errors (an unclosed `{{IF}}`, an `END-FOR` without `FOR`, invalid syntax)
|
|
870
|
+
reject `write()` with an error message that names the tag.
|
|
871
|
+
- Text inserted from your data is never rendered again, so a value that
|
|
872
|
+
contains `{{` stays literal.
|
|
873
|
+
|
|
874
|
+
## Modify Tables
|
|
875
|
+
|
|
876
|
+
You can use a PowerPoint table and add/modify data and style. It is also possible to add rows and columns and to style cells.
|
|
877
|
+
|
|
878
|
+
```ts
|
|
879
|
+
const pres = automizer
|
|
880
|
+
.loadRoot(`RootTemplate.pptx`)
|
|
881
|
+
.load(`SlideWithTables.pptx`, 'tables');
|
|
882
|
+
|
|
883
|
+
const result = await pres.addSlide('tables', 3, (slide) => {
|
|
884
|
+
slide.modifyElement('TableWithEmptyCells', [
|
|
885
|
+
modify.setTable({
|
|
886
|
+
// Use an array of rows to insert data.
|
|
887
|
+
// use `label` key for your information only
|
|
888
|
+
body: [
|
|
889
|
+
{ label: 'item test r1', values: ['test1', 10, 16, 12, 11] },
|
|
890
|
+
{ label: 'item test r2', values: ['test2', 12, 18, 15, 12] },
|
|
891
|
+
{ label: 'item test r3', values: ['test3', 14, 12, 11, 14] },
|
|
892
|
+
],
|
|
893
|
+
}),
|
|
894
|
+
]);
|
|
895
|
+
});
|
|
896
|
+
```
|
|
897
|
+
|
|
898
|
+
Note that the table has to be a **native table** in the template — a grouped-shape "fake table" cannot be filled with `setTable`.
|
|
899
|
+
|
|
900
|
+
A `\n` in a cell value is a line break within the cell (`\v`, what PowerPoint
|
|
901
|
+
uses for Shift+Enter, works the same). It becomes a soft break (`<a:br/>`) in
|
|
902
|
+
the cell's paragraph, and every line keeps the cell's text style:
|
|
903
|
+
|
|
904
|
+
```ts
|
|
905
|
+
slide.modifyElement('TableDefault', [
|
|
906
|
+
modify.setTable({
|
|
907
|
+
body: [{ label: 'head', values: ['Stick\n(solid)', 'Spray'] }],
|
|
908
|
+
}),
|
|
909
|
+
]);
|
|
910
|
+
```
|
|
911
|
+
|
|
912
|
+
Data that is **narrower** than the template table trims the table to the data:
|
|
913
|
+
surplus grid columns and their cells are removed, and a row that is shorter than
|
|
914
|
+
the grid is padded with an empty clone of its last cell. Data that is **wider**
|
|
915
|
+
than the template adds cells by cloning the row's last cell *after* it was
|
|
916
|
+
filled, so a per-cell style applied to that neighbour (colour, bold) carries over
|
|
917
|
+
into the added column. Size template tables to the largest column count you fill,
|
|
918
|
+
or grow them with `expand` (below) before filling.
|
|
919
|
+
|
|
920
|
+
### Table helpers
|
|
921
|
+
|
|
922
|
+
`ModifyTableHelper` provides rich control over existing tables.
|
|
923
|
+
|
|
924
|
+
- Fill table data and auto-adjust size
|
|
925
|
+
|
|
926
|
+
```ts
|
|
927
|
+
slide.modifyElement('MyTable', [
|
|
928
|
+
ModifyTableHelper.setTable({
|
|
929
|
+
body: [
|
|
930
|
+
{ label: 'r1', values: ['A', 1] },
|
|
931
|
+
{ label: 'r2', values: ['B', 2] },
|
|
932
|
+
],
|
|
933
|
+
}),
|
|
934
|
+
]);
|
|
935
|
+
```
|
|
936
|
+
|
|
937
|
+
- Expand rows/columns by tag before filling
|
|
938
|
+
|
|
939
|
+
```ts
|
|
940
|
+
slide.modifyElement('MyTable', [
|
|
941
|
+
ModifyTableHelper.setTable(
|
|
942
|
+
{
|
|
943
|
+
body: [ /* ... */ ],
|
|
944
|
+
},
|
|
945
|
+
{
|
|
946
|
+
expand: [
|
|
947
|
+
{ tag: '<<ROW>>', count: 3, mode: 'row' },
|
|
948
|
+
{ tag: '<<COL>>', count: 2, mode: 'column' },
|
|
949
|
+
],
|
|
950
|
+
adjustHeight: true,
|
|
951
|
+
adjustWidth: true,
|
|
952
|
+
}
|
|
953
|
+
),
|
|
954
|
+
]);
|
|
955
|
+
```
|
|
956
|
+
|
|
957
|
+
- Set fixed row heights / column widths
|
|
958
|
+
|
|
959
|
+
```ts
|
|
960
|
+
slide.modifyElement('MyTable', [
|
|
961
|
+
ModifyTableHelper.updateRowHeight(0, CmToDxa(1)),
|
|
962
|
+
ModifyTableHelper.updateColumnWidth(1, CmToDxa(3)),
|
|
963
|
+
]);
|
|
964
|
+
```
|
|
965
|
+
|
|
966
|
+
- Apply a table style and header/column banding flags
|
|
967
|
+
|
|
968
|
+
```ts
|
|
969
|
+
slide.modifyElement('MyTable', [
|
|
970
|
+
ModifyTableHelper.setTableStyle('TableStyleMedium2', [
|
|
971
|
+
'firstRow', 'bandRow',
|
|
972
|
+
]),
|
|
973
|
+
]);
|
|
974
|
+
```
|
|
975
|
+
|
|
976
|
+
Additional convenience methods:
|
|
977
|
+
|
|
978
|
+
- `ModifyTableHelper.setTableData(data)` – just set data without sizing
|
|
979
|
+
- `ModifyTableHelper.adjustHeight(data)` / `adjustWidth(data)` – recompute sizes only
|
|
980
|
+
|
|
981
|
+
### Find out more
|
|
982
|
+
|
|
983
|
+
- [Modify and style table cells](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-existing-table.test.ts)
|
|
984
|
+
- [Insert data into table with empty cells](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-existing-table-create-text.test.ts)
|
|
985
|
+
|
|
986
|
+
## Modify Charts
|
|
987
|
+
|
|
988
|
+
All data and styles of a chart can be modified. Please note that if your template contains more data than your data object, Automizer will remove these extra nodes. Conversely, if you provide more data, new nodes will be cloned from the first existing one in the template.
|
|
989
|
+
|
|
990
|
+
Note that the chart has to be a **native chart** in the template.
|
|
991
|
+
|
|
992
|
+
```ts
|
|
993
|
+
// Modify an existing chart on an added slide.
|
|
994
|
+
pres.addSlide('charts', 2, (slide) => {
|
|
995
|
+
slide.modifyElement('ColumnChart', [
|
|
996
|
+
// Use an object like this to inject the new chart data.
|
|
997
|
+
// Additional series and categories will be copied from
|
|
998
|
+
// previous sibling.
|
|
999
|
+
modify.setChartData({
|
|
1000
|
+
series: [
|
|
1001
|
+
{ label: 'series 1' },
|
|
1002
|
+
{ label: 'series 2' },
|
|
1003
|
+
{ label: 'series 3' },
|
|
1004
|
+
],
|
|
1005
|
+
categories: [
|
|
1006
|
+
{ label: 'cat 2-1', values: [50, 50, 20] },
|
|
1007
|
+
{ label: 'cat 2-2', values: [14, 50, 20] },
|
|
1008
|
+
{ label: 'cat 2-3', values: [15, 50, 20] },
|
|
1009
|
+
{ label: 'cat 2-4', values: [26, 50, 20] },
|
|
1010
|
+
],
|
|
1011
|
+
}),
|
|
1012
|
+
]);
|
|
1013
|
+
});
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
Find out more about modifying charts:
|
|
1017
|
+
|
|
1018
|
+
- [Modify chart axis](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-chart-axis.test.ts)
|
|
1019
|
+
- [Dealing with bubble charts](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-chart-bubbles.test.ts)
|
|
1020
|
+
- [Vertical line charts](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-chart-vertical-lines.test.ts)
|
|
1021
|
+
- [Style chart series and data points](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-existing-chart-styled.test.ts)
|
|
1022
|
+
|
|
1023
|
+
### Modify Extended Charts
|
|
1024
|
+
|
|
1025
|
+
If you need to modify extended chart types, such like waterfall or map charts, you need to use `modify.setExtendedChartData`.
|
|
1026
|
+
|
|
1027
|
+
```ts
|
|
1028
|
+
// Add and modify a waterfall chart on slide.
|
|
1029
|
+
pres.addSlide('charts', 2, (slide) => {
|
|
1030
|
+
slide.addElement('ChartWaterfall.pptx', 1, 'Waterfall 1', [
|
|
1031
|
+
modify.setExtendedChartData(<ChartData>{
|
|
1032
|
+
series: [{ label: 'series 1' }],
|
|
1033
|
+
categories: [
|
|
1034
|
+
{ label: 'cat 2-1', values: [100] },
|
|
1035
|
+
{ label: 'cat 2-3', values: [50] },
|
|
1036
|
+
{ label: 'cat 2-4', values: [-40] },
|
|
1037
|
+
// ...
|
|
1038
|
+
],
|
|
1039
|
+
}),
|
|
1040
|
+
]);
|
|
1041
|
+
});
|
|
1042
|
+
```
|
|
1043
|
+
|
|
1044
|
+
### Additional chart modifiers
|
|
1045
|
+
|
|
1046
|
+
Besides `modify.setChartData` and `modify.setExtendedChartData`, the `modify` namespace exposes a number of helpers to fine-tune chart appearance and special chart types. Each of them returns a modification callback that can be passed to `slide.modifyElement()`.
|
|
1047
|
+
|
|
1048
|
+
- Chart title (requires an already existing, manually edited title)
|
|
1049
|
+
|
|
1050
|
+
```ts
|
|
1051
|
+
slide.modifyElement('ColumnChart', [modify.setChartTitle('My new title')]);
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
- Axis range and number format. Only manually scaled (non-"Auto") min/max values can be altered.
|
|
1055
|
+
|
|
1056
|
+
```ts
|
|
1057
|
+
slide.modifyElement('ColumnChart', [
|
|
1058
|
+
modify.setAxisRange({
|
|
1059
|
+
axisIndex: 0, // index of c:valAx, defaults to 0
|
|
1060
|
+
min: 0,
|
|
1061
|
+
max: 100,
|
|
1062
|
+
majorUnit: 20,
|
|
1063
|
+
minorUnit: 5,
|
|
1064
|
+
formatCode: '0.0',
|
|
1065
|
+
sourceLinked: false,
|
|
1066
|
+
}),
|
|
1067
|
+
]);
|
|
1068
|
+
```
|
|
1069
|
+
|
|
1070
|
+
- Legend position and visibility. Legend coordinates are shares of the chart coordinates (e.g. `w: 0.5` means "half of chart width").
|
|
1071
|
+
|
|
1072
|
+
```ts
|
|
1073
|
+
slide.modifyElement('ColumnChart', [
|
|
1074
|
+
// move/resize the legend
|
|
1075
|
+
modify.setLegendPosition({ x: 0.8, y: 0.1, w: 0.2, h: 0.3 }),
|
|
1076
|
+
// set legend coordinates to zero so a user can maximize it easily
|
|
1077
|
+
modify.minimizeChartLegend(),
|
|
1078
|
+
// completely remove the legend (PowerPoint will maximize the chart space)
|
|
1079
|
+
modify.removeChartLegend(),
|
|
1080
|
+
]);
|
|
1081
|
+
```
|
|
1082
|
+
|
|
1083
|
+
- Plot area. Requires a `c:manualLayout` element, which only exists if the plot area was edited manually in PowerPoint before. Coordinates are shares of the chart coordinates.
|
|
1084
|
+
|
|
1085
|
+
```ts
|
|
1086
|
+
slide.modifyElement('ColumnChart', [
|
|
1087
|
+
modify.setPlotArea({ x: 0.1, y: 0.1, w: 0.8, h: 0.8 }),
|
|
1088
|
+
]);
|
|
1089
|
+
```
|
|
1090
|
+
|
|
1091
|
+
- Data labels
|
|
1092
|
+
|
|
1093
|
+
```ts
|
|
1094
|
+
import { LabelPosition } from 'pptx-templates';
|
|
1095
|
+
|
|
1096
|
+
slide.modifyElement('ColumnChart', [
|
|
1097
|
+
// format the data labels of all series (or a single series by index)
|
|
1098
|
+
modify.setDataLabelAttributes({
|
|
1099
|
+
applyToSeries: 0, // omit to apply to all series
|
|
1100
|
+
dLblPos: LabelPosition.OutsideEnd,
|
|
1101
|
+
formatCode: '0.0%',
|
|
1102
|
+
sourceLinked: false,
|
|
1103
|
+
showVal: true,
|
|
1104
|
+
showSerName: false,
|
|
1105
|
+
showCatName: false,
|
|
1106
|
+
showPercent: false,
|
|
1107
|
+
showLegendKey: false,
|
|
1108
|
+
}),
|
|
1109
|
+
// or remove all data labels
|
|
1110
|
+
modify.removeDataLabels(),
|
|
1111
|
+
]);
|
|
1112
|
+
```
|
|
1113
|
+
|
|
1114
|
+
- Waterfall total column. Mark the last column (or a specific index) of an extended waterfall chart as a total.
|
|
1115
|
+
|
|
1116
|
+
```ts
|
|
1117
|
+
slide.modifyElement('Waterfall 1', [
|
|
1118
|
+
modify.setWaterFallColumnTotalToLast(), // or pass an index, e.g. (3)
|
|
1119
|
+
]);
|
|
1120
|
+
```
|
|
1121
|
+
|
|
1122
|
+
- Special chart types. Use dedicated modifiers for scatter, combo, bubble and vertical line charts. They accept the same `ChartData` object as `setChartData`.
|
|
1123
|
+
|
|
1124
|
+
```ts
|
|
1125
|
+
slide.modifyElement('ScatterChart', [modify.setChartScatter(chartData)]);
|
|
1126
|
+
slide.modifyElement('ComboChart', [modify.setChartCombo(chartData)]);
|
|
1127
|
+
slide.modifyElement('BubbleChart', [modify.setChartBubbles(chartData)]);
|
|
1128
|
+
slide.modifyElement('VerticalLineChart', [
|
|
1129
|
+
modify.setChartVerticalLines(chartData),
|
|
1130
|
+
]);
|
|
1131
|
+
```
|
|
1132
|
+
|
|
1133
|
+
### Read chart data
|
|
1134
|
+
|
|
1135
|
+
The `read` namespace provides callbacks to read information out of a chart without modifying it. They populate the object you pass in (see `__tests__/read-chart-data.test.js`).
|
|
1136
|
+
|
|
1137
|
+
```ts
|
|
1138
|
+
import { read, WorkbookData, ChartInfo } from 'pptx-templates';
|
|
1139
|
+
|
|
1140
|
+
const workbookData: WorkbookData = [];
|
|
1141
|
+
const chartInfo: ChartInfo = { series: [] };
|
|
1142
|
+
|
|
1143
|
+
slide.modifyElement('ColumnChart', [
|
|
1144
|
+
// read the raw rows of the embedded workbook into workbookData
|
|
1145
|
+
read.readWorkbookData(workbookData),
|
|
1146
|
+
// read series colors and the detected chart type into chartInfo
|
|
1147
|
+
read.readChartInfo(chartInfo),
|
|
1148
|
+
]);
|
|
1149
|
+
```
|
|
1150
|
+
|
|
1151
|
+
## Modify Images
|
|
1152
|
+
|
|
1153
|
+
`pptx-templates` can extract images from loaded .pptx template files and add to your output presentation. You can use shape modifiers (e.g. for size and position) on images, too. Additionally, it is possible to load external media files directly and update relation `Target` of an existing image. This works on both, existing or added images.
|
|
1154
|
+
|
|
1155
|
+
```ts
|
|
1156
|
+
const automizer = new Automizer({
|
|
1157
|
+
// ...
|
|
1158
|
+
// Specify a directory to import external media files from:
|
|
1159
|
+
mediaDir: `path/to/media`,
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
const pres = automizer
|
|
1163
|
+
.loadRoot(`RootTemplate.pptx`)
|
|
1164
|
+
// load one or more files from mediaDir
|
|
1165
|
+
.loadMedia([`feather.png`, `test.png`] /* or use a custom dir */)
|
|
1166
|
+
// and/or use a custom dir
|
|
1167
|
+
.loadMedia(`icon.png`, 'path/to/icons')
|
|
1168
|
+
.load(`SlideWithImages.pptx`, 'images');
|
|
1169
|
+
|
|
1170
|
+
pres.addSlide('images', 2, (slide) => {
|
|
1171
|
+
slide.modifyElement('imagePNG', [
|
|
1172
|
+
// Override the original media source of element 'imagePNG'
|
|
1173
|
+
// by an imported file:
|
|
1174
|
+
ModifyImageHelper.setRelationTarget('feather.png'),
|
|
1175
|
+
|
|
1176
|
+
// You might need to update size
|
|
1177
|
+
ModifyShapeHelper.setPosition({
|
|
1178
|
+
w: CmToDxa(5),
|
|
1179
|
+
h: CmToDxa(3),
|
|
1180
|
+
}),
|
|
1181
|
+
]);
|
|
1182
|
+
});
|
|
1183
|
+
```
|
|
1184
|
+
|
|
1185
|
+
Media can also come from memory instead of `mediaDir`:
|
|
1186
|
+
`automizer.loadMediaBuffer('chart.png', myImageBuffer)` registers a `Buffer`
|
|
1187
|
+
under a filename, usable with `setRelationTarget` like any loaded file.
|
|
1188
|
+
|
|
1189
|
+
Note: media loading requires the root template to be loaded first. An image that is imported more than once (e.g. a logo on several slides) is stored only once in the output file — identical media files are always shared.
|
|
1190
|
+
|
|
1191
|
+
This will also auto-crop the image to the new width and height,
|
|
1192
|
+
based on the container aspect ratio, derived from the original image
|
|
1193
|
+
and using the new image width and height based on the files loaded into
|
|
1194
|
+
the presentation media folder using .loadMedia():
|
|
1195
|
+
|
|
1196
|
+
```ts
|
|
1197
|
+
pres.addSlide('images', 2, (slide) => {
|
|
1198
|
+
slide.modifyElement('Image Placeholder', [
|
|
1199
|
+
ModifyImageHelper.setRelationTargetCover('feather.png', pres),
|
|
1200
|
+
]);
|
|
1201
|
+
});
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
### Image helpers
|
|
1205
|
+
|
|
1206
|
+
Swap images and apply duotone overlays.
|
|
1207
|
+
|
|
1208
|
+
```ts
|
|
1209
|
+
// Point an existing image to a different media file loaded via .loadMedia()
|
|
1210
|
+
slide.modifyElement('Image Placeholder', [
|
|
1211
|
+
ModifyImageHelper.setRelationTarget('feather.png'),
|
|
1212
|
+
]);
|
|
1213
|
+
|
|
1214
|
+
// Auto-crop to cover based on new image aspect ratio (uses template media)
|
|
1215
|
+
slide.modifyElement('Image Placeholder', [
|
|
1216
|
+
ModifyImageHelper.setRelationTargetCover('feather.png', pres),
|
|
1217
|
+
]);
|
|
1218
|
+
|
|
1219
|
+
// Duotone overlay on images/icons
|
|
1220
|
+
slide.modifyElement('Icon', [
|
|
1221
|
+
ModifyImageHelper.setDuotoneFill({
|
|
1222
|
+
color: { type: 'srgbClr', value: '00AAFF' },
|
|
1223
|
+
tint: 70000, // 0-100000
|
|
1224
|
+
satMod: 90000, // 0-100000
|
|
1225
|
+
}),
|
|
1226
|
+
]);
|
|
1227
|
+
```
|
|
1228
|
+
|
|
1229
|
+
### Find out more
|
|
1230
|
+
|
|
1231
|
+
- [Add external image](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/add-external-image.test.ts)
|
|
1232
|
+
- [Modify duotone color overlay for images](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-image-duotone.test.ts)
|
|
1233
|
+
- [Swap image source on a slide master](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/modify-master-external-image.test.ts)
|
|
1234
|
+
|
|
1235
|
+
## Slide Masters and Layouts
|
|
1236
|
+
|
|
1237
|
+
`pptx-templates` supports importing slide masters and their associated slide layouts into the output presentation, modifying the masters and layouts that are already in the root template in place, and removing a master together with its layouts. Individual layouts cannot be imported on their own — a layout always comes with its master. A modification on a slideMaster appears on all related slideLayouts.
|
|
1238
|
+
|
|
1239
|
+
Please be aware that importing slide layouts containing complex content, such as charts and images, is currently not supported. For instance, if a slide layout includes an icon that is not present on the slide master, this icon will break when the slide master is auto-imported into an output presentation. To avoid this issue, ensure that all images and charts are placed exclusively on a slide master and not on a slide layout.
|
|
1240
|
+
|
|
1241
|
+
### Import and modify slide Masters
|
|
1242
|
+
|
|
1243
|
+
You can import, modify and use one or more slideMasters and the related slideLayouts.
|
|
1244
|
+
|
|
1245
|
+
To specify the target index of the required slide master to import, you need to count slideMasters in your _template_ presentation.
|
|
1246
|
+
To specify another slideLayout for an added output slide, you need to count slideLayouts in your _output_ presentation.
|
|
1247
|
+
|
|
1248
|
+
To add and modify shapes on a slide master, please take a look at [Find and Modify Shapes](https://daborsten.github.io/pptx-templates/selectors.md#find-and-modify-shapes) — masters use the same selectors and modifiers as slides.
|
|
1249
|
+
|
|
1250
|
+
```ts
|
|
1251
|
+
// Import another slide master and all its slide layouts.
|
|
1252
|
+
// Index 1 means, you want to import the first of all masters:
|
|
1253
|
+
pres.addMaster('SlidesWithAdditionalMaster.pptx', 1, (master) => {
|
|
1254
|
+
// Modify a certain shape on the slide master:
|
|
1255
|
+
master.modifyElement(
|
|
1256
|
+
`MasterRectangle`,
|
|
1257
|
+
ModifyTextHelper.setText('my text on master'),
|
|
1258
|
+
);
|
|
1259
|
+
// Add a shape from an imported templated to the current slideMaster.
|
|
1260
|
+
master.addElement('SlideWithShapes.pptx', 1, 'Cloud 1');
|
|
1261
|
+
});
|
|
1262
|
+
```
|
|
1263
|
+
|
|
1264
|
+
Any imported slideMaster will be appended to the existing ones in the root template. If you have already e.g. one master with five layouts, and you import a new master coming with seven slide layouts, the first new layout will be #6.
|
|
1265
|
+
|
|
1266
|
+
```ts
|
|
1267
|
+
// Import a slideMaster and its slideLayouts:
|
|
1268
|
+
pres.addMaster('SlidesWithAdditionalMaster.pptx', 1);
|
|
1269
|
+
|
|
1270
|
+
// Add a slide and switch to another layout:
|
|
1271
|
+
pres.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
|
|
1272
|
+
// use another master, e.g. the imported one from 'SlidesWithAdditionalMaster.pptx'
|
|
1273
|
+
// You need to pass the index of the desired layout after all
|
|
1274
|
+
// related layouts of all imported masters have been added to rootTemplate.
|
|
1275
|
+
slide.useSlideLayout(12);
|
|
1276
|
+
});
|
|
1277
|
+
|
|
1278
|
+
// It is also possible to use the original slideLayout of any added slide:
|
|
1279
|
+
pres.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
|
|
1280
|
+
// To use the original master from 'SlidesWithAdditionalMaster.pptx',
|
|
1281
|
+
// we can skip the argument:
|
|
1282
|
+
slide.useSlideLayout();
|
|
1283
|
+
// This will also auto-import the original slideMaster, if not done already,
|
|
1284
|
+
// and look for the created index of the source slideLayout.
|
|
1285
|
+
});
|
|
1286
|
+
```
|
|
1287
|
+
|
|
1288
|
+
### Select a layout by name
|
|
1289
|
+
|
|
1290
|
+
Instead of counting layouts in the output, you can address a layout by its
|
|
1291
|
+
name — the name PowerPoint shows in the layout gallery and in "Rename Layout"
|
|
1292
|
+
— and, if needed, by the name of its master and by the template it came
|
|
1293
|
+
from. Pass an object to `useSlideLayout`:
|
|
1294
|
+
|
|
1295
|
+
```ts
|
|
1296
|
+
pres.addSlide('SlideWithShapes.pptx', 1, (slide) => {
|
|
1297
|
+
// A layout of the root template, by name. Nothing is imported.
|
|
1298
|
+
slide.useSlideLayout({ layout: 'Nur Titel', template: 'root' });
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1301
|
+
pres
|
|
1302
|
+
.addMaster('SlidesWithAdditionalMaster.pptx', 1)
|
|
1303
|
+
.addMaster('SlidesWithAdditionalMaster.pptx', 2)
|
|
1304
|
+
.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
|
|
1305
|
+
// Both imported masters (and the root master) have a layout named
|
|
1306
|
+
// 'Titel und Inhalt': name the master to pick one.
|
|
1307
|
+
slide.useSlideLayout({ master: 'Orange Design', layout: 'Titel und Inhalt' });
|
|
1308
|
+
})
|
|
1309
|
+
.addSlide('SlidesWithAdditionalMaster.pptx', 3, (slide) => {
|
|
1310
|
+
// Only layouts imported from a given template:
|
|
1311
|
+
slide.useSlideLayout({
|
|
1312
|
+
layout: 'Nur Titel',
|
|
1313
|
+
template: 'SlidesWithAdditionalMaster.pptx',
|
|
1314
|
+
});
|
|
1315
|
+
});
|
|
1316
|
+
```
|
|
1317
|
+
|
|
1318
|
+
How the selector (`SlideLayoutSelector`) is resolved:
|
|
1319
|
+
|
|
1320
|
+
- `layout` is compared exactly (including line breaks) with the layout's
|
|
1321
|
+
name in the output presentation.
|
|
1322
|
+
- `master` is the master's name as PowerPoint shows it in the layout
|
|
1323
|
+
gallery: the name of the master's theme, or the master's own name if it
|
|
1324
|
+
has one. Renaming a master in PowerPoint's Slide Master view renames its
|
|
1325
|
+
theme.
|
|
1326
|
+
- `template` restricts the lookup: `'root'` means the layouts of the root
|
|
1327
|
+
template as loaded; the alias of a loaded template means the layouts that
|
|
1328
|
+
were imported from it (with `addMaster` or by auto-import). Without
|
|
1329
|
+
`template`, every master in the output is searched — root and imported.
|
|
1330
|
+
- The selector is resolved against the output at write time, after all
|
|
1331
|
+
masters have been imported; it never imports a master itself. Import the
|
|
1332
|
+
masters you need first (or use the no-argument form, which imports the
|
|
1333
|
+
source slide's own master).
|
|
1334
|
+
- No matching layout, or more than one, rejects `write()` with a
|
|
1335
|
+
`SlideLayoutNotFoundError` whose message lists the available (or the
|
|
1336
|
+
ambiguous) layouts as `"<master> / <layout>"`. With
|
|
1337
|
+
`continueOnError: true` the error is logged and the slide keeps its
|
|
1338
|
+
current layout.
|
|
1339
|
+
|
|
1340
|
+
A plain string still works as before — `slide.useSlideLayout('Nur Titel')`
|
|
1341
|
+
imports the source slide's master and looks the name up among the imported
|
|
1342
|
+
layouts — and now falls back to the root template's own layouts when the
|
|
1343
|
+
name is not found there. If the name is ambiguous, the first match wins and
|
|
1344
|
+
a warning names the candidates; use the object form to pick one.
|
|
1345
|
+
|
|
1346
|
+
### Modify masters and layouts of the root template in place
|
|
1347
|
+
|
|
1348
|
+
Text, logos or other shapes that live on a master of the root template can
|
|
1349
|
+
be edited without importing a copy: `pres.modifyMaster(n, callback)` applies
|
|
1350
|
+
the same `modifyElement` / `addElement` / `removeElement` vocabulary as
|
|
1351
|
+
`addMaster` to the root's own master. The master keeps its number, no part is
|
|
1352
|
+
added, and every slide using it keeps using it. `pres.modifyLayout(n,
|
|
1353
|
+
callback)` does the same for a single slide layout.
|
|
1354
|
+
|
|
1355
|
+
```ts
|
|
1356
|
+
pres
|
|
1357
|
+
.loadRoot('SlidesWithAdditionalMaster.pptx')
|
|
1358
|
+
.load('SlideWithShapes.pptx')
|
|
1359
|
+
// the first master of the root template, as loaded:
|
|
1360
|
+
.modifyMaster(1, (master) => {
|
|
1361
|
+
master.modifyElement('MasterRectangle', ModifyTextHelper.setText('Customer name'));
|
|
1362
|
+
master.addElement('SlideWithShapes.pptx', 1, 'Cloud 1');
|
|
1363
|
+
master.removeElement('Rechteck 6');
|
|
1364
|
+
})
|
|
1365
|
+
// the first layout of the root template, as loaded:
|
|
1366
|
+
.modifyLayout(1, (layout) => {
|
|
1367
|
+
layout.modifyElement('Titel 1', ModifyTextHelper.setText('Title placeholder'));
|
|
1368
|
+
});
|
|
1369
|
+
```
|
|
1370
|
+
|
|
1371
|
+
- `n` is the 1-based number of `ppt/slideMasters/slideMaster<n>.xml` /
|
|
1372
|
+
`ppt/slideLayouts/slideLayout<n>.xml` in the root template as loaded — the
|
|
1373
|
+
order of PowerPoint's Slide Master view. Masters imported with `addMaster`
|
|
1374
|
+
are modified through their own callback instead.
|
|
1375
|
+
- Deferred like everything else: the modifications run on `write()` before
|
|
1376
|
+
the slides are written, in call order with `addMaster` imports. Several
|
|
1377
|
+
`modifyMaster` calls for the same number share one queue.
|
|
1378
|
+
- A number that does not exist in the root template rejects `write()`.
|
|
1379
|
+
- An image replaced on a master (`ModifyImageHelper.setRelationTarget`) is
|
|
1380
|
+
copied into the archive like on a slide.
|
|
1381
|
+
|
|
1382
|
+
### Remove a slide master
|
|
1383
|
+
|
|
1384
|
+
`pres.removeMaster(n)` removes master `n` of the root template with its
|
|
1385
|
+
layouts, its theme (unless another part — usually the presentation itself,
|
|
1386
|
+
for the first theme — still refers to it), their relationships and their
|
|
1387
|
+
entries in `ppt/presentation.xml` and `[Content_Types].xml`:
|
|
1388
|
+
|
|
1389
|
+
```ts
|
|
1390
|
+
pres
|
|
1391
|
+
.loadRoot('SlidesWithAdditionalMaster.pptx')
|
|
1392
|
+
.load('SlideWithShapes.pptx')
|
|
1393
|
+
.addSlide('SlideWithShapes.pptx', 1, (slide) => {
|
|
1394
|
+
// this slide used a layout of master 1: switch it to master 2 first
|
|
1395
|
+
slide.useSlideLayout(13);
|
|
1396
|
+
})
|
|
1397
|
+
.removeMaster(1);
|
|
1398
|
+
```
|
|
1399
|
+
|
|
1400
|
+
- Runs on `write()` after all slides have been written, so layout switches
|
|
1401
|
+
are in effect. If a slide of the output still uses one of the master's
|
|
1402
|
+
layouts, `write()` rejects and names the slide and layout; point the
|
|
1403
|
+
slide at another layout with `useSlideLayout()` first, or drop the root
|
|
1404
|
+
template's own slides with `removeExistingSlides: true`.
|
|
1405
|
+
- The remaining masters and layouts keep their file numbers; the master and
|
|
1406
|
+
layout ids in `presentation.xml` are renumbered as usual (starting at
|
|
1407
|
+
2147483648, the smallest id PowerPoint accepts).
|
|
1408
|
+
- Media used only by the removed master and layouts stays in the archive
|
|
1409
|
+
unless `cleanup: true` is set.
|
|
1410
|
+
|
|
1411
|
+
Please notice: If your root template and your imported slides have an equal structure of slideMasters and slideLayouts, it won't be necessary to add slideMasters manually.
|
|
1412
|
+
|
|
1413
|
+
If you have trouble with messed up slideMasters, and if you don't worry about the impact on performance, you can try and set `autoImportSlideMasters: true` to always import all required files:
|
|
1414
|
+
|
|
1415
|
+
```ts
|
|
1416
|
+
import Automizer from 'pptx-templates';
|
|
1417
|
+
|
|
1418
|
+
const automizer = new Automizer({
|
|
1419
|
+
// ...
|
|
1420
|
+
|
|
1421
|
+
// Always use the original slideMaster and slideLayout of any
|
|
1422
|
+
// imported slide:
|
|
1423
|
+
autoImportSlideMasters: true,
|
|
1424
|
+
// ...
|
|
1425
|
+
});
|
|
1426
|
+
```
|
|
1427
|
+
|
|
1428
|
+
## Generate shapes with PptxGenJS
|
|
1429
|
+
|
|
1430
|
+
This library wraps around [PptxGenJS](https://github.com/gitbrent/PptxGenJS) to generate shapes from scratch. It is possible to use the `PptxGenJS` wrapper to generate shapes on a slide.
|
|
1431
|
+
|
|
1432
|
+
Here's an example of how to use `pptxGenJS` to add a text shape to a slide:
|
|
1433
|
+
|
|
1434
|
+
```ts
|
|
1435
|
+
pres.addSlide('empty', 1, (slide) => {
|
|
1436
|
+
// Use PptxGenJS to add text from scratch:
|
|
1437
|
+
slide.generate((pptxGenJSSlide) => {
|
|
1438
|
+
pptxGenJSSlide.addText('Test 1', {
|
|
1439
|
+
x: 1,
|
|
1440
|
+
y: 1,
|
|
1441
|
+
h: 5,
|
|
1442
|
+
w: 10,
|
|
1443
|
+
color: '363636',
|
|
1444
|
+
});
|
|
1445
|
+
}, 'custom object name');
|
|
1446
|
+
});
|
|
1447
|
+
```
|
|
1448
|
+
|
|
1449
|
+
You can as well create charts with `pptxGenJS`:
|
|
1450
|
+
|
|
1451
|
+
```ts
|
|
1452
|
+
const dataChartAreaLine = [
|
|
1453
|
+
{
|
|
1454
|
+
name: 'Actual Sales',
|
|
1455
|
+
labels: ['Jan', 'Feb', 'Mar'],
|
|
1456
|
+
values: [1500, 4600, 5156],
|
|
1457
|
+
},
|
|
1458
|
+
{
|
|
1459
|
+
name: 'Projected Sales',
|
|
1460
|
+
labels: ['Jan', 'Feb', 'Mar'],
|
|
1461
|
+
values: [1000, 2600, 3456],
|
|
1462
|
+
},
|
|
1463
|
+
];
|
|
1464
|
+
|
|
1465
|
+
pres.addSlide('empty', 1, (slide) => {
|
|
1466
|
+
// Use PptxGenJS to add generated content from scratch:
|
|
1467
|
+
slide.generate((pSlide, pptxGenJs) => {
|
|
1468
|
+
pSlide.addChart(pptxGenJs.ChartType.line, dataChartAreaLine, {
|
|
1469
|
+
x: 1,
|
|
1470
|
+
y: 1,
|
|
1471
|
+
w: 8,
|
|
1472
|
+
h: 4,
|
|
1473
|
+
});
|
|
1474
|
+
});
|
|
1475
|
+
});
|
|
1476
|
+
```
|
|
1477
|
+
|
|
1478
|
+
Note: inside `generate`, coordinates and sizes are in **inches** (PptxGenJS
|
|
1479
|
+
convention) — unlike `modify.setPosition`, which uses EMU/DXA values (see the
|
|
1480
|
+
[units reference](https://daborsten.github.io/pptx-templates/helpers.md#units-reference)).
|
|
1481
|
+
|
|
1482
|
+
You can use the following functions to generate shapes with `pptxGenJS`:
|
|
1483
|
+
|
|
1484
|
+
- addChart
|
|
1485
|
+
- addImage
|
|
1486
|
+
- addShape
|
|
1487
|
+
- addTable
|
|
1488
|
+
- addText
|
|
1489
|
+
|
|
1490
|
+
### Create a new hyperlinked text shape
|
|
1491
|
+
|
|
1492
|
+
It is also possible to create a new hyperlink from scratch with the `pptxGenJS` wrapper. This is useful if you want to add hyperlinks to shapes that are not part of the template. (To manage hyperlinks on *existing* template shapes, see [Hyperlink Management](https://daborsten.github.io/pptx-templates/hyperlinks.md).)
|
|
1493
|
+
|
|
1494
|
+
```ts
|
|
1495
|
+
// Generate a new text shape pointing to an external site
|
|
1496
|
+
slide.generate((pptxGenJSSlide) => {
|
|
1497
|
+
pptxGenJSSlide.addText(`External Link`, {
|
|
1498
|
+
hyperlink: { url: 'https://github.com' },
|
|
1499
|
+
x: 1,
|
|
1500
|
+
y: 1,
|
|
1501
|
+
w: 2.5,
|
|
1502
|
+
h: 0.5,
|
|
1503
|
+
fontSize: 12,
|
|
1504
|
+
});
|
|
1505
|
+
});
|
|
1506
|
+
|
|
1507
|
+
// Or generate an internal hyperlink
|
|
1508
|
+
slide.generate((pptxGenJSSlide) => {
|
|
1509
|
+
pptxGenJSSlide.addText(`Go to slide 3`, {
|
|
1510
|
+
hyperlink: { slide: 3 },
|
|
1511
|
+
x: 1,
|
|
1512
|
+
y: 1,
|
|
1513
|
+
w: 2.5,
|
|
1514
|
+
h: 0.5,
|
|
1515
|
+
fontSize: 12,
|
|
1516
|
+
});
|
|
1517
|
+
});
|
|
1518
|
+
```
|
|
1519
|
+
|
|
1520
|
+
See a complete example on [how to add a chart from scratch](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/generate-pptxgenjs-charts.test.ts).
|
|
1521
|
+
|
|
1522
|
+
## Hyperlink Management
|
|
1523
|
+
|
|
1524
|
+
PowerPoint presentations often use hyperlinks to connect to external websites or internal slides. The `pptx-templates` provides simple and powerful functions to manage hyperlinks in your presentations.
|
|
1525
|
+
|
|
1526
|
+
### Add Hyperlinks to existing shapes
|
|
1527
|
+
|
|
1528
|
+
You can add hyperlinks to template text shapes using the `addHyperlink` helper function. The function accepts either a URL string for external links or a slide number for internal slide links:
|
|
1529
|
+
|
|
1530
|
+
```ts
|
|
1531
|
+
import { XmlElement } from 'pptx-templates';
|
|
1532
|
+
|
|
1533
|
+
// Add an external hyperlink
|
|
1534
|
+
slide.modifyElement('TextShape', modify.addHyperlink('https://example.com'));
|
|
1535
|
+
|
|
1536
|
+
// Add an internal slide link (to slide 3)
|
|
1537
|
+
slide.modifyElement('TextShape', (element: XmlElement, relation?: XmlElement) => {
|
|
1538
|
+
modify.addHyperlink(3)(element, relation);
|
|
1539
|
+
});
|
|
1540
|
+
```
|
|
1541
|
+
|
|
1542
|
+
The `addHyperlink` function will automatically detect whether the target is an external URL or an internal slide number and set up the appropriate relationship type and attributes.
|
|
1543
|
+
|
|
1544
|
+
### Update or remove existing hyperlinks
|
|
1545
|
+
|
|
1546
|
+
Use `modify.setHyperlinkTarget` to change the target of a hyperlink that already exists on a shape. The second argument controls whether the new target is external (default, `true`) or an internal slide link (`false`):
|
|
1547
|
+
|
|
1548
|
+
```ts
|
|
1549
|
+
// Point an existing hyperlink to a new external URL
|
|
1550
|
+
slide.modifyElement('TextShape', modify.setHyperlinkTarget('https://example.com'));
|
|
1551
|
+
|
|
1552
|
+
// Point an existing hyperlink to an internal slide (e.g. slide 5)
|
|
1553
|
+
slide.modifyElement('TextShape', modify.setHyperlinkTarget(5, false));
|
|
1554
|
+
```
|
|
1555
|
+
|
|
1556
|
+
Use `modify.removeHyperlink` to strip the hyperlink from a shape while keeping its text:
|
|
1557
|
+
|
|
1558
|
+
```ts
|
|
1559
|
+
slide.modifyElement('TextShape', modify.removeHyperlink());
|
|
1560
|
+
```
|
|
1561
|
+
|
|
1562
|
+
### Related
|
|
1563
|
+
|
|
1564
|
+
- To create a **new** hyperlinked shape from scratch, use the PptxGenJS wrapper — see [Generate shapes with PptxGenJS](https://daborsten.github.io/pptx-templates/generation.md#create-a-new-hyperlinked-text-shape).
|
|
1565
|
+
- Inline hyperlinks inside generated text (external targets or slide numbers) are also supported by the [MultiText/HTML text helpers](https://daborsten.github.io/pptx-templates/text.md#text-helpers-multitexthtml).
|
|
1566
|
+
|
|
1567
|
+
## Slide Management
|
|
1568
|
+
|
|
1569
|
+
### Remove elements from a slide
|
|
1570
|
+
|
|
1571
|
+
You can as well remove elements from slides.
|
|
1572
|
+
|
|
1573
|
+
```ts
|
|
1574
|
+
// Remove existing charts, images or shapes from added slide.
|
|
1575
|
+
pres
|
|
1576
|
+
.addSlide('charts', 2, (slide) => {
|
|
1577
|
+
slide.removeElement('ColumnChart');
|
|
1578
|
+
})
|
|
1579
|
+
.addSlide('images', 2, (slide) => {
|
|
1580
|
+
slide.removeElement('imageJPG');
|
|
1581
|
+
slide.removeElement('Textfeld 5');
|
|
1582
|
+
slide.addElement('images', 2, 'imageJPG');
|
|
1583
|
+
});
|
|
1584
|
+
```
|
|
1585
|
+
|
|
1586
|
+
### Loop through the slides of a presentation
|
|
1587
|
+
|
|
1588
|
+
If you would like to modify elements in a single .pptx file, it is important to know that `pptx-templates` is not able to directly "jump" to a shape to modify it — see [the template/root model](https://daborsten.github.io/pptx-templates/concepts.md#the-templateroot-model) for how it works internally.
|
|
1589
|
+
|
|
1590
|
+
In case you need to apply modifications to the root template, you need to load it as a normal template:
|
|
1591
|
+
|
|
1592
|
+
```ts
|
|
1593
|
+
import Automizer, {
|
|
1594
|
+
CmToDxa,
|
|
1595
|
+
ISlide,
|
|
1596
|
+
ModifyColorHelper,
|
|
1597
|
+
ModifyShapeHelper,
|
|
1598
|
+
ModifyTextHelper,
|
|
1599
|
+
} from 'pptx-templates';
|
|
1600
|
+
|
|
1601
|
+
const run = async () => {
|
|
1602
|
+
const automizer = new Automizer({
|
|
1603
|
+
templateDir: `path/to/pptx-templates`,
|
|
1604
|
+
outputDir: `path/to/pptx-output`,
|
|
1605
|
+
// this is required to start with no slides:
|
|
1606
|
+
removeExistingSlides: true,
|
|
1607
|
+
});
|
|
1608
|
+
|
|
1609
|
+
let pres = automizer
|
|
1610
|
+
.loadRoot(`SlideWithShapes.pptx`)
|
|
1611
|
+
// We load it twice to make it available for modifying slides.
|
|
1612
|
+
// Defining a "name" as the second parameter makes it a little easier
|
|
1613
|
+
.load(`SlideWithShapes.pptx`, 'myTemplate');
|
|
1614
|
+
|
|
1615
|
+
// This is brand new: get useful information about loaded templates:
|
|
1616
|
+
const myTemplates = await pres.getInfo();
|
|
1617
|
+
const mySlides = myTemplates.slidesByTemplate(`myTemplate`);
|
|
1618
|
+
|
|
1619
|
+
// Feel free to create some functions to pre-define all modifications
|
|
1620
|
+
// you need to apply to your slides.
|
|
1621
|
+
type CallbackBySlideNumber = {
|
|
1622
|
+
slideNumber: number;
|
|
1623
|
+
callback: (slide: ISlide) => void;
|
|
1624
|
+
};
|
|
1625
|
+
const callbacks: CallbackBySlideNumber[] = [
|
|
1626
|
+
{
|
|
1627
|
+
slideNumber: 2,
|
|
1628
|
+
callback: (slide: ISlide) => {
|
|
1629
|
+
slide.modifyElement('Cloud', [
|
|
1630
|
+
ModifyTextHelper.setText('My content'),
|
|
1631
|
+
ModifyShapeHelper.setPosition({
|
|
1632
|
+
h: CmToDxa(5),
|
|
1633
|
+
}),
|
|
1634
|
+
ModifyColorHelper.solidFill({
|
|
1635
|
+
type: 'srgbClr',
|
|
1636
|
+
value: 'cccccc',
|
|
1637
|
+
}),
|
|
1638
|
+
]);
|
|
1639
|
+
},
|
|
1640
|
+
},
|
|
1641
|
+
];
|
|
1642
|
+
const getCallbacks = (slideNumber: number) => {
|
|
1643
|
+
return callbacks.find((callback) => callback.slideNumber === slideNumber)
|
|
1644
|
+
?.callback;
|
|
1645
|
+
};
|
|
1646
|
+
|
|
1647
|
+
// We can loop all slides and apply the callbacks if defined
|
|
1648
|
+
mySlides.forEach((mySlide) => {
|
|
1649
|
+
pres.addSlide('myTemplate', mySlide.number, getCallbacks(mySlide.number));
|
|
1650
|
+
});
|
|
1651
|
+
|
|
1652
|
+
// This will result in an output presentation containing all slides of "SlideWithShapes.pptx"
|
|
1653
|
+
pres.write(`myOutputPresentation.pptx`).then((summary) => {
|
|
1654
|
+
console.log(summary);
|
|
1655
|
+
});
|
|
1656
|
+
};
|
|
1657
|
+
|
|
1658
|
+
run().catch((error) => {
|
|
1659
|
+
console.error(error);
|
|
1660
|
+
});
|
|
1661
|
+
```
|
|
1662
|
+
|
|
1663
|
+
### Quickly get all slide numbers of a template
|
|
1664
|
+
|
|
1665
|
+
When calling `pres.getInfo()`, it will gather information about all elements on all slides of all templates. In case you just want to loop through all slides of a certain template, you can use this shortcut:
|
|
1666
|
+
|
|
1667
|
+
```ts
|
|
1668
|
+
const slideNumbers = await pres
|
|
1669
|
+
.getTemplate('myTemplate.pptx')
|
|
1670
|
+
.getAllSlideNumbers();
|
|
1671
|
+
|
|
1672
|
+
for (const slideNumber of slideNumbers) {
|
|
1673
|
+
// do the thing
|
|
1674
|
+
}
|
|
1675
|
+
```
|
|
1676
|
+
|
|
1677
|
+
### Sort output slides
|
|
1678
|
+
|
|
1679
|
+
There are three ways to arrange slides in an output presentation.
|
|
1680
|
+
|
|
1681
|
+
1. By default, all slides will be appended to the existing slides in your root template. The order of `addSlide` calls will define slide sorting in the output presentation.
|
|
1682
|
+
|
|
1683
|
+
2. You can alternatively remove all existing slides by setting the `removeExistingSlides` flag to true. The first slide added with `addSlide` will be first slide in the output presentation. If you want to insert slides from root template, you need to load it a second time.
|
|
1684
|
+
|
|
1685
|
+
```ts
|
|
1686
|
+
import Automizer from 'pptx-templates';
|
|
1687
|
+
|
|
1688
|
+
const automizer = new Automizer({
|
|
1689
|
+
templateDir: `my/pptx/templates`,
|
|
1690
|
+
outputDir: `my/pptx/output`,
|
|
1691
|
+
|
|
1692
|
+
// truncate root presentation and start with zero slides
|
|
1693
|
+
removeExistingSlides: true,
|
|
1694
|
+
});
|
|
1695
|
+
|
|
1696
|
+
let pres = automizer
|
|
1697
|
+
.loadRoot(`RootTemplate.pptx`)
|
|
1698
|
+
// We load this twice to make it available for sorting slides
|
|
1699
|
+
.load(`RootTemplate.pptx`, 'root')
|
|
1700
|
+
.load(`SlideWithShapes.pptx`, 'shapes')
|
|
1701
|
+
.load(`SlideWithGraph.pptx`, 'graph');
|
|
1702
|
+
|
|
1703
|
+
pres
|
|
1704
|
+
.addSlide('root', 1) // First slide will be taken from root
|
|
1705
|
+
.addSlide('graph', 1)
|
|
1706
|
+
.addSlide('shapes', 1)
|
|
1707
|
+
.addSlide('root', 3) // Third slide from root will be appended
|
|
1708
|
+
.addSlide('root', 2); // Second and third slide will switch position
|
|
1709
|
+
|
|
1710
|
+
pres.write(`mySortedPresentation.pptx`).then((summary) => {
|
|
1711
|
+
console.log(summary);
|
|
1712
|
+
});
|
|
1713
|
+
```
|
|
1714
|
+
|
|
1715
|
+
3. Use `sortSlides`-callback
|
|
1716
|
+
You can pass an array of numbers and create a callback and apply it to `presentation.xml`.
|
|
1717
|
+
This will also work without adding slides.
|
|
1718
|
+
|
|
1719
|
+
Slides will be appended to the existing slides by slide number (starting from 1). You may find irritating results in case you skip a slide number.
|
|
1720
|
+
|
|
1721
|
+
```ts
|
|
1722
|
+
import { ModifyPresentationHelper } from 'pptx-templates';
|
|
1723
|
+
|
|
1724
|
+
//
|
|
1725
|
+
// You may truncate root template or you may not
|
|
1726
|
+
// ...
|
|
1727
|
+
|
|
1728
|
+
// It is possible to skip adding slides, try sorting an unmodified presentation
|
|
1729
|
+
pres
|
|
1730
|
+
.addSlide('charts', 1)
|
|
1731
|
+
.addSlide('charts', 2)
|
|
1732
|
+
.addSlide('images', 1)
|
|
1733
|
+
.addSlide('images', 2);
|
|
1734
|
+
|
|
1735
|
+
const order = [3, 2, 4, 1];
|
|
1736
|
+
pres.modify(ModifyPresentationHelper.sortSlides(order));
|
|
1737
|
+
```
|
|
1738
|
+
|
|
1739
|
+
## Helper Utilities
|
|
1740
|
+
|
|
1741
|
+
This section documents handy helpers exposed by `pptx-templates` that provide extra capabilities. You can import helpers directly from the package:
|
|
1742
|
+
|
|
1743
|
+
```ts
|
|
1744
|
+
import {
|
|
1745
|
+
ModifyShapeHelper,
|
|
1746
|
+
ModifyTableHelper,
|
|
1747
|
+
ModifyCleanupHelper,
|
|
1748
|
+
ModifyTextHelper,
|
|
1749
|
+
ModifyImageHelper,
|
|
1750
|
+
} from 'pptx-templates';
|
|
1751
|
+
```
|
|
1752
|
+
|
|
1753
|
+
Some helper families are documented with their feature area:
|
|
1754
|
+
|
|
1755
|
+
- [Table helpers](https://daborsten.github.io/pptx-templates/tables.md#table-helpers) (`ModifyTableHelper`)
|
|
1756
|
+
- [Text helpers (MultiText/HTML)](https://daborsten.github.io/pptx-templates/text.md#text-helpers-multitexthtml) (`ModifyTextHelper`)
|
|
1757
|
+
- [Image helpers](https://daborsten.github.io/pptx-templates/images.md#image-helpers) (`ModifyImageHelper`)
|
|
1758
|
+
|
|
1759
|
+
### Shape helpers
|
|
1760
|
+
|
|
1761
|
+
Use `ModifyShapeHelper` to quickly adjust common properties of shapes and text frames.
|
|
1762
|
+
|
|
1763
|
+
- Solid fill color
|
|
1764
|
+
|
|
1765
|
+
```ts
|
|
1766
|
+
slide.modifyElement('MyShape', [
|
|
1767
|
+
// sets the shape's solid fill to theme color "accent6"
|
|
1768
|
+
ModifyShapeHelper.setSolidFill,
|
|
1769
|
+
]);
|
|
1770
|
+
```
|
|
1771
|
+
|
|
1772
|
+
- Outline (line) width, color and dash style
|
|
1773
|
+
|
|
1774
|
+
```ts
|
|
1775
|
+
import { PtToEmu } from 'pptx-templates';
|
|
1776
|
+
|
|
1777
|
+
slide.modifyElement('MyShape', [
|
|
1778
|
+
ModifyShapeHelper.setOutline({
|
|
1779
|
+
weight: PtToEmu(2), // EMU, 1pt = 12700
|
|
1780
|
+
color: { type: 'srgbClr', value: 'FF0000' },
|
|
1781
|
+
type: 'sysDash', // any a:prstDash value, e.g. solid, dash, dot
|
|
1782
|
+
}),
|
|
1783
|
+
]);
|
|
1784
|
+
```
|
|
1785
|
+
|
|
1786
|
+
Only the given properties are modified. An outline is created if the shape has
|
|
1787
|
+
none, which is the case whenever it was never overridden in PowerPoint and is
|
|
1788
|
+
inherited from the theme or shape style. If the outline was explicitly switched
|
|
1789
|
+
off in PowerPoint, a `weight` alone stays invisible — pass a `color`, too. Use
|
|
1790
|
+
`ModifyCleanupHelper.removeBorder` to remove an outline.
|
|
1791
|
+
|
|
1792
|
+
- Bullet list from strings
|
|
1793
|
+
|
|
1794
|
+
```ts
|
|
1795
|
+
slide.modifyElement('MyTextBox', [
|
|
1796
|
+
ModifyShapeHelper.setBulletList(['Item 1', 'Item 2', 'Item 3']),
|
|
1797
|
+
]);
|
|
1798
|
+
```
|
|
1799
|
+
|
|
1800
|
+
- Replace tagged text (see [Replace tagged text](https://daborsten.github.io/pptx-templates/text.md#replace-tagged-text) for the
|
|
1801
|
+
tag concept; the default delimiters are `{{` and `}}`)
|
|
1802
|
+
|
|
1803
|
+
```ts
|
|
1804
|
+
slide.modifyElement('MyTextBox', [
|
|
1805
|
+
ModifyShapeHelper.replaceText(
|
|
1806
|
+
[
|
|
1807
|
+
{ replace: 'company', by: { text: 'Globex' } },
|
|
1808
|
+
{ replace: 'year', by: { text: '2025', style: { isBold: true } } },
|
|
1809
|
+
],
|
|
1810
|
+
{ openingTag: '{{', closingTag: '}}' },
|
|
1811
|
+
),
|
|
1812
|
+
]);
|
|
1813
|
+
```
|
|
1814
|
+
|
|
1815
|
+
- Position, size, rotation, rounded corners
|
|
1816
|
+
|
|
1817
|
+
```ts
|
|
1818
|
+
import { CmToDxa } from 'pptx-templates';
|
|
1819
|
+
|
|
1820
|
+
slide.modifyElement('MyShape', [
|
|
1821
|
+
// set absolute position/size
|
|
1822
|
+
ModifyShapeHelper.setPosition({ x: CmToDxa(2), y: CmToDxa(3), w: CmToDxa(6), h: CmToDxa(2) }),
|
|
1823
|
+
// update only some props, leave others untouched
|
|
1824
|
+
ModifyShapeHelper.updatePosition({ x: CmToDxa(4) }),
|
|
1825
|
+
// rotate clockwise in degrees
|
|
1826
|
+
ModifyShapeHelper.rotate(15),
|
|
1827
|
+
// rounded rectangle corners (0-100000)
|
|
1828
|
+
ModifyShapeHelper.roundedCorners(25000),
|
|
1829
|
+
]);
|
|
1830
|
+
```
|
|
1831
|
+
|
|
1832
|
+
### Cleanup helpers
|
|
1833
|
+
|
|
1834
|
+
`ModifyCleanupHelper` helps remove formatting noise from shapes when you need a clean base.
|
|
1835
|
+
|
|
1836
|
+
```ts
|
|
1837
|
+
import { XmlElement } from 'pptx-templates';
|
|
1838
|
+
|
|
1839
|
+
slide.modifyElement('MyShape', [
|
|
1840
|
+
ModifyCleanupHelper.removeBackground,
|
|
1841
|
+
ModifyCleanupHelper.removeBorder,
|
|
1842
|
+
ModifyCleanupHelper.removeEffects,
|
|
1843
|
+
// text-level cleanup
|
|
1844
|
+
ModifyCleanupHelper.clearTextUnderline,
|
|
1845
|
+
ModifyCleanupHelper.clearTextBold,
|
|
1846
|
+
ModifyCleanupHelper.clearTextSize,
|
|
1847
|
+
// remove all explicit text colors …
|
|
1848
|
+
(element: XmlElement) => ModifyCleanupHelper.clearTextColor(element),
|
|
1849
|
+
// … or pass a color to set a uniform one instead
|
|
1850
|
+
(element: XmlElement) =>
|
|
1851
|
+
ModifyCleanupHelper.clearTextColor(element, {
|
|
1852
|
+
type: 'srgbClr',
|
|
1853
|
+
value: 'FF0000',
|
|
1854
|
+
}),
|
|
1855
|
+
]);
|
|
1856
|
+
```
|
|
1857
|
+
|
|
1858
|
+
Other useful helpers include: `removeTextEffects`, `removeFillEffects`, `remove3dEffects`, `removeShadowEffects`, and `removeExtLst`.
|
|
1859
|
+
|
|
1860
|
+
### Unit conversion helpers
|
|
1861
|
+
|
|
1862
|
+
PowerPoint stores coordinates and sizes in the `dxa` (EMU) unit. Use the exported converters to work with centimeters instead:
|
|
1863
|
+
|
|
1864
|
+
```ts
|
|
1865
|
+
import { CmToDxa, DxaToCm } from 'pptx-templates';
|
|
1866
|
+
|
|
1867
|
+
// centimeters -> dxa (e.g. when setting position/size)
|
|
1868
|
+
const widthInDxa = CmToDxa(6); // 2160000
|
|
1869
|
+
|
|
1870
|
+
// dxa -> centimeters (e.g. when reading shape coordinates)
|
|
1871
|
+
const widthInCm = DxaToCm(2160000); // 6
|
|
1872
|
+
```
|
|
1873
|
+
|
|
1874
|
+
Line weights are usually given in points, use `PtToEmu`/`EmuToPt` for those:
|
|
1875
|
+
|
|
1876
|
+
```ts
|
|
1877
|
+
import { PtToEmu, EmuToPt } from 'pptx-templates';
|
|
1878
|
+
|
|
1879
|
+
const weight = PtToEmu(1.5); // 19050
|
|
1880
|
+
const inPoints = EmuToPt(19050); // 1.5
|
|
1881
|
+
```
|
|
1882
|
+
|
|
1883
|
+
### Generic / debugging helpers
|
|
1884
|
+
|
|
1885
|
+
`ModifyHelper` (also available through the `modify` namespace) offers low-level callbacks that are handy for debugging or custom XML tweaks:
|
|
1886
|
+
|
|
1887
|
+
```ts
|
|
1888
|
+
import { modify } from 'pptx-templates';
|
|
1889
|
+
|
|
1890
|
+
slide.modifyElement('MyShape', [
|
|
1891
|
+
// print the element's XML to the console
|
|
1892
|
+
modify.dump,
|
|
1893
|
+
// print the related chart XML to the console
|
|
1894
|
+
modify.dumpChart,
|
|
1895
|
+
// set an attribute on the first matching tag (optionally by index)
|
|
1896
|
+
modify.setAttribute('a:off', 'x', 1000000),
|
|
1897
|
+
]);
|
|
1898
|
+
```
|
|
1899
|
+
|
|
1900
|
+
### Advanced XML helpers (power users)
|
|
1901
|
+
|
|
1902
|
+
For advanced scenarios, you can inspect slide XML and relationships. These are considered expert APIs and may change.
|
|
1903
|
+
|
|
1904
|
+
Import them from the package root:
|
|
1905
|
+
|
|
1906
|
+
```ts
|
|
1907
|
+
import { XmlSlideHelper, XmlRelationshipHelper } from 'pptx-templates';
|
|
1908
|
+
```
|
|
1909
|
+
|
|
1910
|
+
Examples:
|
|
1911
|
+
|
|
1912
|
+
- Read all text element IDs on a slide: `new XmlSlideHelper(slideXml).getAllTextElementIds()`
|
|
1913
|
+
- Get named elements: `new XmlSlideHelper(slideXml).getNamedElements(['p:sp'])`
|
|
1914
|
+
- Table introspection: `XmlSlideHelper.readTableInfo(element)`
|
|
1915
|
+
- Relationship targets by type or prefix: `new XmlRelationshipHelper(relsXml).getTargetsByType(type)`
|
|
1916
|
+
|
|
1917
|
+
See tests for practical usage:
|
|
1918
|
+
|
|
1919
|
+
- [Find all text elements on a slide](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/get-all-text-element-ids.test.ts)
|
|
1920
|
+
- [Read shape/group info](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/read-shape-info.test.ts)
|
|
1921
|
+
- [Read group info](https://github.com/DaBorsten/pptx-templates/blob/main/__tests__/read-group-info.test.ts)
|
|
1922
|
+
|
|
1923
|
+
### Writing raw XML callbacks
|
|
1924
|
+
|
|
1925
|
+
Not every OOXML property has a `modify.*` helper (`cap`/`cmpd` line attributes,
|
|
1926
|
+
custom geometry, `a:effectLst`, …). Any callback you pass to
|
|
1927
|
+
`modifyElement`/`addElement` receives the shape's XML element — the `<p:sp>`,
|
|
1928
|
+
`<p:pic>` or `<p:graphicFrame>` node — so you can edit it directly:
|
|
1929
|
+
|
|
1930
|
+
```ts
|
|
1931
|
+
import { XmlElement } from 'pptx-templates';
|
|
1932
|
+
|
|
1933
|
+
slide.modifyElement('MyBox', (element: XmlElement) => {
|
|
1934
|
+
// ...manipulate the DOM node...
|
|
1935
|
+
});
|
|
1936
|
+
```
|
|
1937
|
+
|
|
1938
|
+
Rules for hand-written callbacks:
|
|
1939
|
+
|
|
1940
|
+
1. **Scope your lookups.** `element.getElementsByTagName('a:ln')` searches *all*
|
|
1941
|
+
descendants of the shape, including text run properties (`a:rPr`) — where
|
|
1942
|
+
`a:ln` also occurs. Reach the container first
|
|
1943
|
+
(`element.getElementsByTagName('p:spPr')[0]`), then prefer a **direct child**
|
|
1944
|
+
scan over another `getElementsByTagName`.
|
|
1945
|
+
2. **A missing element is the normal case.** If a property was never overridden
|
|
1946
|
+
in PowerPoint, it is inherited from the theme/shape style and simply absent
|
|
1947
|
+
from the slide XML. Always handle "modify existing" *and* "create new".
|
|
1948
|
+
3. **Child order follows the schema, not your call order.** Appending in the
|
|
1949
|
+
wrong order is what makes PowerPoint show the "repair" prompt on open. For
|
|
1950
|
+
`p:spPr` the sequence is `a:xfrm` → geometry (`a:prstGeom`/`a:custGeom`) →
|
|
1951
|
+
fill → `a:ln` → `a:effectLst` → `a:scene3d` → `a:sp3d` → `a:extLst`; inside
|
|
1952
|
+
`a:ln` it is fill → `a:prstDash` → join (`a:round`/`a:bevel`/`a:miter`) →
|
|
1953
|
+
`a:headEnd` → `a:tailEnd`.
|
|
1954
|
+
4. **Inspect before you guess:** `slide.modifyElement('MyBox', modify.dump)`
|
|
1955
|
+
prints the shape's current XML to the console. Do that first when unsure what
|
|
1956
|
+
the template actually contains.
|
|
1957
|
+
5. **A throwing callback rejects `write()`** with a `CallbackError` naming the
|
|
1958
|
+
slide and element — unless `continueOnError: true` is set, which logs a
|
|
1959
|
+
warning and skips the modification instead (see
|
|
1960
|
+
[deferred execution](https://daborsten.github.io/pptx-templates/concepts.md#deferred-execution)).
|
|
1961
|
+
6. Use `XmlHelper` (exported) for common DOM chores: `XmlHelper.remove(node)`,
|
|
1962
|
+
`insertAfter(new, ref)`, `getClosestParent('p:sp', node)`,
|
|
1963
|
+
`appendClone(node, parent)`, `dump(node)`.
|
|
1964
|
+
|
|
1965
|
+
If a raw callback you wrote turns out to be generally useful, it is a good
|
|
1966
|
+
candidate for a new `modify.*` helper — see
|
|
1967
|
+
[AGENTS.md](https://github.com/DaBorsten/pptx-templates/blob/main/AGENTS.md) in
|
|
1968
|
+
the repository.
|
|
1969
|
+
|
|
1970
|
+
#### Worked example: shape outline (weight + color)
|
|
1971
|
+
|
|
1972
|
+
> Outlines have a dedicated modifier — use `ModifyShapeHelper.setOutline` from
|
|
1973
|
+
> the [shape helpers](#shape-helpers) for real work. The example is kept because
|
|
1974
|
+
> it shows the general technique on a realistic property.
|
|
1975
|
+
|
|
1976
|
+
```ts
|
|
1977
|
+
import Automizer, { XmlElement } from 'pptx-templates';
|
|
1978
|
+
|
|
1979
|
+
// p:spPr children that must stay AFTER a:ln
|
|
1980
|
+
const AFTER_LN = ['a:effectLst', 'a:effectDag', 'a:scene3d', 'a:sp3d', 'a:extLst'];
|
|
1981
|
+
const childByName = (parent: XmlElement, names: string[]) =>
|
|
1982
|
+
Array.from(parent.childNodes as any).find((n: any) =>
|
|
1983
|
+
names.includes(n.nodeName),
|
|
1984
|
+
) as XmlElement | undefined;
|
|
1985
|
+
|
|
1986
|
+
const setOutline =
|
|
1987
|
+
(outline: { weight?: number; color?: string }) =>
|
|
1988
|
+
(element: XmlElement) => {
|
|
1989
|
+
const spPr = element.getElementsByTagName('p:spPr')[0];
|
|
1990
|
+
if (!spPr) return;
|
|
1991
|
+
|
|
1992
|
+
// Direct child only — a:ln also lives inside text run properties.
|
|
1993
|
+
let ln = childByName(spPr, ['a:ln']);
|
|
1994
|
+
if (!ln) {
|
|
1995
|
+
ln = spPr.ownerDocument.createElement('a:ln');
|
|
1996
|
+
const anchor = childByName(spPr, AFTER_LN);
|
|
1997
|
+
anchor ? spPr.insertBefore(ln, anchor) : spPr.appendChild(ln);
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
if (outline.weight !== undefined) {
|
|
2001
|
+
// a:ln/@w is EMU: 1pt = 12700
|
|
2002
|
+
ln.setAttribute('w', String(Math.round(outline.weight * 12700)));
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
if (outline.color) {
|
|
2006
|
+
const solidFill = ln.ownerDocument.createElement('a:solidFill');
|
|
2007
|
+
const srgbClr = ln.ownerDocument.createElement('a:srgbClr');
|
|
2008
|
+
srgbClr.setAttribute('val', outline.color.replace('#', '')); // no '#'!
|
|
2009
|
+
solidFill.appendChild(srgbClr);
|
|
2010
|
+
|
|
2011
|
+
// Fill is the FIRST child of a:ln — replace whatever fill is there.
|
|
2012
|
+
const currentFill = childByName(ln, [
|
|
2013
|
+
'a:noFill', 'a:solidFill', 'a:gradFill', 'a:pattFill',
|
|
2014
|
+
]);
|
|
2015
|
+
currentFill
|
|
2016
|
+
? ln.replaceChild(solidFill, currentFill)
|
|
2017
|
+
: ln.insertBefore(solidFill, ln.firstChild);
|
|
2018
|
+
}
|
|
2019
|
+
};
|
|
2020
|
+
|
|
2021
|
+
slide.modifyElement('MyBox', setOutline({ weight: 2, color: 'FFFFFF' }));
|
|
2022
|
+
```
|
|
2023
|
+
|
|
2024
|
+
Produces `<a:ln w="25400"><a:solidFill><a:srgbClr val="FFFFFF"/></a:solidFill></a:ln>`.
|
|
2025
|
+
Caveat worth knowing: if the shape's existing `a:ln` contains `<a:noFill/>`
|
|
2026
|
+
(outline explicitly turned off in PowerPoint), setting only the weight yields
|
|
2027
|
+
`<a:ln w="…"><a:noFill/></a:ln>` — a thick *invisible* line. Set a color too, or
|
|
2028
|
+
replace the `a:noFill` node.
|
|
2029
|
+
|
|
2030
|
+
#### Units reference
|
|
2031
|
+
|
|
2032
|
+
OOXML uses no single unit. When writing raw XML:
|
|
2033
|
+
|
|
2034
|
+
| What | Unit | Conversion |
|
|
2035
|
+
|---|---|---|
|
|
2036
|
+
| Position/size (`a:off`, `a:ext`), line width `a:ln/@w`, corner radius | EMU | 1 cm = 360000 · 1 inch = 914400 · 1 pt = 12700 |
|
|
2037
|
+
| `modify.setPosition` / `updatePosition` / `setOutline` | same EMU values | helpers `CmToDxa(cm)` / `DxaToCm(v)` (name says Dxa, value is EMU), `PtToEmu(pt)` / `EmuToPt(v)` |
|
|
2038
|
+
| Rotation (`a:xfrm/@rot`) | 1/60000 degree | 45° = 2700000 |
|
|
2039
|
+
| Font size (`a:rPr/@sz`, `TextStyle.size`) | 1/100 pt | 18pt = 1800 |
|
|
2040
|
+
| Percentages (`a:alpha/@val`, `a:lumMod`, …) | 1/1000 % | 50% = 50000 |
|
|
2041
|
+
| Colors (`a:srgbClr/@val`) | 6-digit hex | `'FF0000'`, never `'#FF0000'` |
|
|
2042
|
+
| PptxGenJS `slide.generate(...)` | inches | (different world — see [Generate shapes](https://daborsten.github.io/pptx-templates/generation.md)) |
|
|
2043
|
+
|
|
2044
|
+
## Output
|
|
2045
|
+
|
|
2046
|
+
Calling an output method executes the whole queued modification chain (see [deferred execution](https://daborsten.github.io/pptx-templates/concepts.md#deferred-execution)) and produces the final archive. Always `await` the output call — nothing happens without it, and one output call per Automizer instance.
|
|
2047
|
+
|
|
2048
|
+
### write, stream, getJSZip
|
|
2049
|
+
|
|
2050
|
+
```ts
|
|
2051
|
+
// Write the output file.
|
|
2052
|
+
pres.write('myPresentation.pptx').then((summary) => {
|
|
2053
|
+
console.log(summary);
|
|
2054
|
+
});
|
|
2055
|
+
|
|
2056
|
+
// It is also possible to get a ReadableStream.
|
|
2057
|
+
// stream() accepts JSZip.JSZipGeneratorOptions for 'nodebuffer' type.
|
|
2058
|
+
const stream = await pres.stream({
|
|
2059
|
+
compressionOptions: {
|
|
2060
|
+
level: 9,
|
|
2061
|
+
},
|
|
2062
|
+
});
|
|
2063
|
+
// You can e.g. output the pptx archive to stdout instead of writing a file:
|
|
2064
|
+
stream.pipe(process.stdout);
|
|
2065
|
+
|
|
2066
|
+
// If you need any other output format, you can eventually access
|
|
2067
|
+
// the underlying JSZip instance:
|
|
2068
|
+
const finalJSZip = await pres.getJSZip();
|
|
2069
|
+
// Convert the output to whatever needed:
|
|
2070
|
+
const base64 = await finalJSZip.generateAsync({ type: 'base64' });
|
|
2071
|
+
```
|
|
2072
|
+
|
|
2073
|
+
By default, a throwing modification callback or an unresolvable element selector rejects `write()` with a typed error (`CallbackError`, `ElementNotFoundError`). Set `continueOnError: true` to log a warning and skip the failing modification instead.
|
|
2074
|
+
|
|
2075
|
+
### Track status of automation process
|
|
2076
|
+
|
|
2077
|
+
When creating large presentations, you might want to have some information about the current status. Use a custom status tracker:
|
|
2078
|
+
|
|
2079
|
+
```ts
|
|
2080
|
+
import Automizer, { StatusTracker } from 'pptx-templates';
|
|
2081
|
+
|
|
2082
|
+
// If you want to track the steps of creation process,
|
|
2083
|
+
// you can use a custom callback:
|
|
2084
|
+
const myStatusTracker = (status: StatusTracker) => {
|
|
2085
|
+
console.log(status.info + ' (' + status.share + '%)');
|
|
2086
|
+
};
|
|
2087
|
+
|
|
2088
|
+
const automizer = new Automizer({
|
|
2089
|
+
// ...
|
|
2090
|
+
statusTracker: myStatusTracker,
|
|
2091
|
+
});
|
|
2092
|
+
```
|
|
2093
|
+
|
|
2094
|
+
### Cleanup and compression flags
|
|
2095
|
+
|
|
2096
|
+
The `Automizer` constructor takes a few options that affect the output archive:
|
|
2097
|
+
|
|
2098
|
+
```ts
|
|
2099
|
+
const automizer = new Automizer({
|
|
2100
|
+
templateDir: `my/pptx/templates`,
|
|
2101
|
+
outputDir: `my/pptx/output`,
|
|
2102
|
+
|
|
2103
|
+
// activate `cleanup` to eventually remove unused files from the archive:
|
|
2104
|
+
cleanup: false,
|
|
2105
|
+
|
|
2106
|
+
// Remove all unused placeholders to prevent unwanted overlays:
|
|
2107
|
+
cleanupPlaceholders: false,
|
|
2108
|
+
|
|
2109
|
+
// Set a value from 0-9 to specify the zip-compression level.
|
|
2110
|
+
// The lower the number, the faster your output file will be ready.
|
|
2111
|
+
// Higher compression levels produce smaller files.
|
|
2112
|
+
compression: 0,
|
|
2113
|
+
});
|
|
2114
|
+
```
|
|
2115
|
+
|
|
2116
|
+
If an output file triggers PowerPoint's repair prompt, retry with `cleanup: false` and `cleanupPlaceholders: false` first — see [Troubleshooting](https://daborsten.github.io/pptx-templates/troubleshooting.md).
|
|
2117
|
+
|
|
2118
|
+
## Requirements and Limitations
|
|
2119
|
+
|
|
2120
|
+
This generator can only be used on the server-side and requires a [Node.js](https://nodejs.org/en/download/package-manager/) environment.
|
|
2121
|
+
|
|
2122
|
+
### Shape Types
|
|
2123
|
+
|
|
2124
|
+
At the moment, you might encounter difficulties with special shape types that require additional relations (e.g., hyperlinks, video and audio may not work correctly). However, most shape types, including connection shapes, tables, and charts, are already supported. If you encounter any issues, please feel free to [report any issue](https://github.com/DaBorsten/pptx-templates/issues/new).
|
|
2125
|
+
|
|
2126
|
+
### Chart Types
|
|
2127
|
+
|
|
2128
|
+
Extended chart types, like waterfall or map charts, are basically supported. You might need additional modifiers to handle extended properties, which are not implemented yet. Please help to improve `pptx-templates` and [report](https://github.com/DaBorsten/pptx-templates/issues/new) issues regarding extended charts.
|
|
2129
|
+
|
|
2130
|
+
### Animations
|
|
2131
|
+
|
|
2132
|
+
Animations are currently out of scope of this library. You might get errors on opening an output .pptx when there are added or removed shapes. This is because `pptx-templates` doesn't synchronize `id`-attributes of animations with the existing shapes on a slide.
|
|
2133
|
+
|
|
2134
|
+
### Slide Masters and Layouts
|
|
2135
|
+
|
|
2136
|
+
Slide masters and their layouts can be imported, but individual slide layouts cannot be added, modified or removed directly, and layouts must not carry complex content like charts and images. See [Slide Masters and Layouts](https://daborsten.github.io/pptx-templates/masters-layouts.md) for details and workarounds.
|
|
2137
|
+
|
|
2138
|
+
### Direct manipulation of elements
|
|
2139
|
+
|
|
2140
|
+
It is also important to know that `pptx-templates` is currently limited to _adding_ things to the output presentation. If you require the ability to, for instance, modify a specific element on a slide within an existing presentation and leave the rest untouched, you will need to include all the other slides in the process. Find some workarounds in [Slide Management](https://daborsten.github.io/pptx-templates/slide-management.md#loop-through-the-slides-of-a-presentation).
|
|
2141
|
+
|
|
2142
|
+
### PowerPoint version
|
|
2143
|
+
|
|
2144
|
+
All testing focuses on PowerPoint 2019 .pptx file format.
|
|
2145
|
+
|
|
2146
|
+
## Troubleshooting
|
|
2147
|
+
|
|
2148
|
+
If you encounter problems when opening a `.pptx`-file modified by this library, you might worry about PowerPoint not giving any details about the error. It can be hard to find the cause, but there are some things you can check:
|
|
2149
|
+
|
|
2150
|
+
- **Broken relation**: There are still unsupported shape types and `pptx-templates` will not copy required relations of those. You can inflate `.pptx` output and check `ppt/slides/_rels/slide[#].xml.rels` files to find possible missing files.
|
|
2151
|
+
- **Unsupported media**: You can also take a look at the `ppt/media`-directory of an inflated `.pptx`-file. If you discover any unusual file formats, remove or replace the files by one of the [known types](https://github.com/DaBorsten/pptx-templates/blob/main/src/enums/content-type-map.ts).
|
|
2152
|
+
- **Broken animation**: Pay attention to modified/removed shapes which are part of an animation. In case of doubt, (temporarily) remove all animations from your template. (see [#78](https://github.com/singerla/pptx-automizer/issues/78))
|
|
2153
|
+
- **Proprietary/Binary contents** (e.g. ThinkCell): Walk through all slides, slideMasters and slideLayouts and seek for hidden Objects. Hit `ALT+F10` to toggle the sidebar.
|
|
2154
|
+
- **Chart datasheet won't open** If you encounter an error message on opening a chart's datasheet, please make sure that the data table (blue bordered rectangle in worksheet view) of your template starts at cell A:1. If not, open worksheet in Excel mode and edit the table size in the table draft tab.
|
|
2155
|
+
|
|
2156
|
+
### Bisecting the repair prompt
|
|
2157
|
+
|
|
2158
|
+
If the output opens with a PowerPoint repair prompt and the checks above don't point to a cause:
|
|
2159
|
+
|
|
2160
|
+
1. Retry with `cleanup: false` and `cleanupPlaceholders: false`.
|
|
2161
|
+
2. Remove exotic elements (animations, embedded videos, ThinkCell remnants) from the template.
|
|
2162
|
+
3. Isolate the failing slide by bisecting your `addSlide` calls — comment out half of them, re-run, and narrow down until the offending slide (or modification) is found.
|
|
2163
|
+
|
|
2164
|
+
Another powerful check: run the repo's [OOXML schema validator](https://daborsten.github.io/pptx-templates/testing.md#ooxml-schema-validation-validatepptx) (`yarn validate:pptx`) on your output — it usually names the exact part and element PowerPoint chokes on.
|
|
2165
|
+
|
|
2166
|
+
If none of these could help, please don't hesitate to [talk about it](https://github.com/DaBorsten/pptx-templates/issues/new).
|
|
2167
|
+
|
|
2168
|
+
### Testing
|
|
2169
|
+
|
|
2170
|
+
You can run all unit tests using these commands:
|
|
2171
|
+
|
|
2172
|
+
```
|
|
2173
|
+
yarn test
|
|
2174
|
+
yarn test-coverage
|
|
2175
|
+
```
|
|
2176
|
+
|
|
2177
|
+
See [Testing and Validation Tools](https://daborsten.github.io/pptx-templates/testing.md) for the full test system — archive invariants, the docs-examples compile gate, schema validation and visual regression.
|
|
2178
|
+
|
|
2179
|
+
## Rules for the AI assistant
|
|
2180
|
+
|
|
2181
|
+
1. **Always `await` the output call** (`write`/`stream`/`getJSZip`) — nothing
|
|
2182
|
+
happens without it. One output call per Automizer instance.
|
|
2183
|
+
2. **One Automizer instance per presentation build**, a fresh instance per
|
|
2184
|
+
output file. Running several instances concurrently in the same process is
|
|
2185
|
+
supported.
|
|
2186
|
+
3. Shape callbacks receive raw XML elements for advanced use, but **prefer the
|
|
2187
|
+
`modify.*` helpers** — only fall back to XML manipulation (`XmlHelper`,
|
|
2188
|
+
`modify.dump(element)` for debugging) when no helper exists.
|
|
2189
|
+
4. Slide numbers are **1-based**. Template files are addressed by filename or
|
|
2190
|
+
the alias given to `.load()`.
|
|
2191
|
+
5. `templateDir`/`outputDir` must exist; media loading requires the root
|
|
2192
|
+
template to be loaded first.
|
|
2193
|
+
6. Charts must be **native charts** in the template, tables native tables. A
|
|
2194
|
+
grouped-shape "fake table" cannot be filled with `setTable`.
|
|
2195
|
+
7. If the output opens with a PowerPoint repair prompt: retry with
|
|
2196
|
+
`cleanup: false` and `cleanupPlaceholders: false`, remove exotic elements
|
|
2197
|
+
(animations, embedded videos, think-cell remnants) from the template, and
|
|
2198
|
+
isolate the failing slide by bisecting `addSlide` calls.
|
|
2199
|
+
8. When a modification doesn't apply, don't guess selector variations:
|
|
2200
|
+
`write()` rejects with a typed error (`ElementNotFoundError`,
|
|
2201
|
+
`CallbackError`) naming slide and element — list the real shape names via
|
|
2202
|
+
`pres.getInfo()` and fix the selector. With `continueOnError: true`,
|
|
2203
|
+
failures are only logged and skipped, so check the console output then.
|
|
2204
|
+
9. Keep data preparation (fetching/transforming) separate from the
|
|
2205
|
+
Automizer chain; build plain `ChartData`/`TableData` objects first, then
|
|
2206
|
+
apply them in modifiers.
|
|
2207
|
+
10. Suggested skeleton for any task: *load root → load templates → loop over
|
|
2208
|
+
data → addSlide with modifiers → write → log summary*.
|
|
2209
|
+
|
|
2210
|
+
## Minimal complete example
|
|
2211
|
+
|
|
2212
|
+
```ts
|
|
2213
|
+
import Automizer, { modify } from 'pptx-templates';
|
|
2214
|
+
|
|
2215
|
+
const automizer = new Automizer({
|
|
2216
|
+
templateDir: `${__dirname}/templates`,
|
|
2217
|
+
outputDir: `${__dirname}/output`,
|
|
2218
|
+
removeExistingSlides: true,
|
|
2219
|
+
autoImportSlideMasters: true,
|
|
2220
|
+
});
|
|
2221
|
+
|
|
2222
|
+
const regions = [
|
|
2223
|
+
{ name: 'North', q1: 100, q2: 120 },
|
|
2224
|
+
{ name: 'South', q1: 80, q2: 140 },
|
|
2225
|
+
];
|
|
2226
|
+
|
|
2227
|
+
const pres = automizer
|
|
2228
|
+
.loadRoot('Root.pptx')
|
|
2229
|
+
.load('Report.pptx', 'report');
|
|
2230
|
+
|
|
2231
|
+
for (const region of regions) {
|
|
2232
|
+
pres.addSlide('report', 1, (slide) => {
|
|
2233
|
+
slide.modifyElement('Title', modify.setText(`Sales: ${region.name}`));
|
|
2234
|
+
slide.modifyElement('Chart', modify.setChartData({
|
|
2235
|
+
series: [{ label: 'Revenue' }],
|
|
2236
|
+
categories: [
|
|
2237
|
+
{ label: 'Q1', values: [region.q1] },
|
|
2238
|
+
{ label: 'Q2', values: [region.q2] },
|
|
2239
|
+
],
|
|
2240
|
+
}));
|
|
2241
|
+
});
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
const summary = await pres.write('sales-report.pptx');
|
|
2245
|
+
console.log(`Done: ${summary.slides} slides in ${summary.file}`);
|
|
2246
|
+
```
|
|
2247
|
+
|
|
2248
|
+
More examples: the `__tests__/` directory of the repo covers nearly every
|
|
2249
|
+
feature (94 runnable scenarios). Full documentation:
|
|
2250
|
+
https://daborsten.github.io/pptx-templates/ — every page is also served as
|
|
2251
|
+
Markdown at its URL plus `.md`, indexed in
|
|
2252
|
+
https://daborsten.github.io/pptx-templates/llms.txt.
|
|
2253
|
+
Repo: https://github.com/DaBorsten/pptx-templates
|