Skip to content

Commit 7ec20a0

Browse files
authored
Merge pull request #109
updated .load, .loadRoot, rootTemplate and preTemplates to allow for supplying buffers in addition to file paths.
2 parents a606c56 + 12049dd commit 7ec20a0

File tree

11 files changed

+6730
-578
lines changed

11 files changed

+6730
-578
lines changed

README.md

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ If you require commercial support for complex .pptx automation, you can explore
2626
- [As a Package](#as-a-package)
2727
- [Usage](#usage)
2828
- [Basic Example](#basic-example)
29+
- [Load files from buffer/URL](#load-files-from-bufferurl)
2930
- [How to Select Slides Shapes](#how-to-select-slides-shapes)
3031
- [Select slide by number and shape by name](#select-slide-by-number-and-shape-by-name)
3132
- [Select slides by creationId](#select-slides-by-creationid)
@@ -226,6 +227,31 @@ const finalJSZip = await pres.getJSZip();
226227
const base64 = await finalJSZip.generateAsync({ type: 'base64' });
227228
```
228229

230+
## Load files from buffer/URL
231+
232+
It is possible to load `.pptx` files from buffer:
233+
234+
```ts
235+
const rootTemplate = await fs.readFile(
236+
`${__dirname}/pptx-templates/RootTemplate.pptx`,
237+
);
238+
const slideWithShapes = await fs.readFile(
239+
`${__dirname}/pptx-templates/SlideWithShapes.pptx`,
240+
);
241+
242+
// Additionally, you can fetch a template from url as well:
243+
const url =
244+
'https://raw.githubusercontent.com/singerla/pptx-automizer/main/__tests__/pptx-templates/SlideWithShapes.pptx';
245+
const response = await fetch(url);
246+
const buffer = await response.arrayBuffer();
247+
const bytes = new Uint8Array(buffer);
248+
249+
const pres = automizer
250+
.loadRoot(rootTemplate)
251+
.load(slideWithShapes, 'shapes')
252+
.load(bytes, 'shapesFromUrl');
253+
```
254+
229255
## How to Select Slides Shapes
230256

231257
`pptx-automizer` 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.
@@ -783,7 +809,8 @@ To specify another slideLayout for an added output slide, you need to count slid
783809

784810
To add and modify shapes on a slide master, please take a look at [Add and modify shapes](https://github.com/singerla/pptx-automizer#add-and-modify-shapes).
785811

786-
If you require to modify slide master backgrounds, please refer to
812+
If you require to modify slide master backgrounds, please refer to
813+
787814
- [Modify master background color](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-master-background-color.test.ts).
788815
- [Modify master background image](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-master-background-image.test.ts).
789816

@@ -890,6 +917,7 @@ const setTextContent = function (label: number | string) {
890917
};
891918
};
892919
```
920+
893921
This function will construct an anonymous callback function on setup, while the callback function itself will be executed on runtime, when it's up to the target element on a slide.
894922

895923
You can use the modifier e.g. on adding an element:
@@ -931,10 +959,9 @@ If you encounter problems when opening a `.pptx`-file modified by this library,
931959
- **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.
932960
- **Chart styles not working**: If you try to change e.g. color or size of a chart data label, and it doesn't work as expected, try to remove all data labels and activate them again. If this does not help, try to give the first data label of a series a slightly different style (this creates a single data point).
933961
- **Replace Text not working**: Cut out your e.g. {CustomerName} tag from textbox to clipboard, paste it into a plaintext editor to remove all (visible and invisible) formatting. Copy & paste {CustomerName} back to the textbox. (see [#82](https://github.com/singerla/pptx-automizer/issues/82) and [#73](https://github.com/singerla/pptx-automizer/issues/73))
934-
- **No related chart worksheet**: It might happen to PowerPoint to lose the worksheet relation for a chart. If a chart gets corrupted by this, you will see a normal chart on your slide, but get an error message if you try to open the datasheet. Please replace the corrupted chart by a working one. (see [#104](https://github.com/singerla/pptx-automizer/issues/104))
935-
962+
- **No related chart worksheet**: It might happen to PowerPoint to lose the worksheet relation for a chart. If a chart gets corrupted by this, you will see a normal chart on your slide, but get an error message if you try to open the datasheet. Please replace the corrupted chart by a working one. (see [#104](https://github.com/singerla/pptx-automizer/issues/104))
936963

937-
If none of these could help, please don't hesitate to [talk about it](https://github.com/singerla/pptx-automizer/issues/new).
964+
If none of these could help, please don't hesitate to [talk about it](https://github.com/singerla/pptx-automizer/issues/new).
938965

939966
## Testing
940967

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as fs from 'fs/promises';
2+
import Automizer from '../src/automizer';
3+
import { ChartData, modify } from '../src';
4+
5+
test('create presentation from buffer/from directory and add basic slides', async () => {
6+
const automizer = new Automizer({
7+
outputDir: `${__dirname}/pptx-output`,
8+
templateDir: `${__dirname}/pptx-templates`,
9+
});
10+
const rootTemplate = await fs.readFile(
11+
`${__dirname}/pptx-templates/RootTemplate.pptx`,
12+
);
13+
const slideWithShapes = await fs.readFile(
14+
`${__dirname}/pptx-templates/SlideWithShapes.pptx`,
15+
);
16+
17+
const url =
18+
'https://raw.githubusercontent.com/singerla/pptx-automizer/main/__tests__/pptx-templates/SlideWithShapes.pptx';
19+
20+
const response = await fetch(url);
21+
const buffer = await response.arrayBuffer();
22+
const bytes = new Uint8Array(buffer);
23+
24+
const pres = automizer
25+
.loadRoot(rootTemplate)
26+
.load(bytes, 'shapesFromWeb')
27+
.load('SlideWithCharts.pptx', 'chartsFromDir')
28+
.load(slideWithShapes, 'shapes');
29+
30+
pres.addSlide('chartsFromDir', 2, (slide) => {
31+
slide.modifyElement('ColumnChart', [
32+
modify.setChartData(<ChartData>{
33+
series: [{ label: 'series 1' }, { label: 'series 2' }],
34+
categories: [
35+
{ label: 'cat 2-1', values: [50, 50] },
36+
{ label: 'cat 2-2', values: [14, 50] },
37+
{ label: 'cat 2-3', values: [15, 50] },
38+
{ label: 'cat 2-4', values: [26, 50] },
39+
],
40+
}),
41+
]);
42+
});
43+
44+
for (let i = 0; i <= 10; i++) {
45+
pres.addSlide('shapes', 1);
46+
}
47+
48+
pres.addSlide('shapesFromWeb', 2);
49+
50+
await pres.write(`create-presentation-from-buffer-mixed.test.pptx`);
51+
52+
expect(pres).toBeInstanceOf(Automizer);
53+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as fs from 'fs/promises';
2+
import Automizer from '../src/automizer';
3+
4+
test('create presentation from buffer and add basic slide', async () => {
5+
const automizer = new Automizer({
6+
outputDir: `${__dirname}/pptx-output`,
7+
});
8+
const rootTemplate = await fs.readFile(
9+
`${__dirname}/pptx-templates/RootTemplate.pptx`,
10+
);
11+
const slideWithShapes = await fs.readFile(
12+
`${__dirname}/pptx-templates/SlideWithShapes.pptx`,
13+
);
14+
const pres = automizer.loadRoot(rootTemplate).load(slideWithShapes, 'shapes');
15+
16+
for (let i = 0; i <= 10; i++) {
17+
pres.addSlide('shapes', 1);
18+
}
19+
20+
await pres.write(`create-presentation-from-buffer.test.pptx`);
21+
22+
expect(pres).toBeInstanceOf(Automizer);
23+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Automizer, { LabelPosition, modify } from '../src/index';
2+
3+
test('modify chart series data labels.', async () => {
4+
const automizer = new Automizer({
5+
templateDir: `${__dirname}/pptx-templates`,
6+
outputDir: `${__dirname}/pptx-output`,
7+
});
8+
9+
const pres = automizer
10+
.loadRoot(`RootTemplate.pptx`)
11+
.load(`ChartBarsStackedLabels.pptx`, 'charts');
12+
13+
const result = await pres
14+
.addSlide('charts', 1, (slide) => {
15+
slide.modifyElement('BarsStacked', [
16+
modify.setDataLabelAttributes({
17+
dLblPos: LabelPosition.OutsideEnd,
18+
}),
19+
]);
20+
})
21+
.write(`modify-chart-data-labels.test.pptx`);
22+
23+
expect(result.charts).toBe(2);
24+
});

0 commit comments

Comments
 (0)