Coder Social home page Coder Social logo

heavysixer / node-pptx Goto Github PK

View Code? Open in Web Editor NEW
146.0 6.0 43.0 28.06 MB

Generate PPTX files on the server-side with JavaScript.

License: MIT License

JavaScript 100.00%
node-pptx pptx-files pptx powerpoint powerpoint-javascript-library powerpoint-generation

node-pptx's Issues

Animations

What about animations? looking at powerpoint slide XML, it doesn't seem that compilcated.

Bug when trying to load existing présentation

I'm trying to load exisiting powerpoint and add some slide to him.
But when I save it, I get this stacktrace:

(node:10568) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Relationships' of undefined at PptRelsFactory.addPresentationToSlideRelationship (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\ppt\rels.js:24:56) at PptFactory.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\ppt\index.js:64:80) at PowerPointFactory.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\index.js:127:52) at Presentation.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\presentation.js:112:45) at D:\development\sprint-reviewer\index.js:71:18 at Composer.compose (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\composer.js:20:15) at createPptx (D:\development\sprint-reviewer\index.js:65:10) at D:\development\sprint-reviewer\index.js:42:5 at processTicksAndRejections (internal/process/task_queues.js:85:5) (node:10568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10568) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. (node:10568) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Relationships' of undefined at PptRelsFactory.addPresentationToSlideRelationship (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\ppt\rels.js:24:56) at PptFactory.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\ppt\index.js:64:80) at PowerPointFactory.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\index.js:127:52) at Presentation.addSlide (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\presentation.js:112:45) at D:\development\sprint-reviewer\index.js:83:14 at Composer.compose (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\composer.js:20:15) at createPptx (D:\development\sprint-reviewer\index.js:65:10) at D:\development\sprint-reviewer\index.js:42:5 at processTicksAndRejections (internal/process/task_queues.js:85:5) (node:10568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:10568) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Types' of undefined at ContentTypeFactory.addDefaultContentType (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\content-types.js:29:44) at ContentTypeFactory.addDefaultMediaContentTypes (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\content-types.js:21:14) at PowerPointFactory.addDefaultMediaContentTypes (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\factories\index.js:241:33) at Presentation.save (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\presentation.js:163:32) at Composer.save (D:\development\sprint-reviewer\node_modules\nodejs-pptx\lib\composer.js:25:33) at createPptx (D:\development\sprint-reviewer\index.js:91:10) at D:\development\sprint-reviewer\index.js:42:5 at processTicksAndRejections (internal/process/task_queues.js:85:5) (node:10568) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4)

Convert to Typescript

Hello! I would like to see this project support typescript. I would also love to start doing it myself. Let me know if it's ok to start a PR for this

Can't remove slide from presentation

I am trying to remove slide from my presentation using code

`const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();

async function load() {
await pptx.load('/app/go/src/ppt2svgconverter/Node-PPTX/hello-world.pptx'); // load a pre-existing PPTX
}

async function removeSlide() {
await pptx.compose(pres => {
pres.removeSlide(pres.getSlide("slide1")); // remove the first slide from the PPTX
// OR ---> pres.removeSlide(pres.getSlide(1)); <--- example of getting a slide by intege
});
}
load();
removeSlide();`

But getting error like node:10115) UnhandledPromiseRejectionWarning: Error: Invalid slide name in Presentation.getSlide(): slide1

PPT I used is attached here
hello-world.pptx

Load PPTX through Buffer

Hi!
i would like to load a pptx file from http request. I have thought that the fastest way for doing it is to load it througth buffer.
right now i'm ussing the version 1.0.1

issue

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/nodejs-pptx/lib/factories/index.js b/node_modules/nodejs-pptx/lib/factories/index.js
index 9d96aa5..3932925 100644
--- a/node_modules/nodejs-pptx/lib/factories/index.js
+++ b/node_modules/nodejs-pptx/lib/factories/index.js
@@ -39,7 +39,7 @@ class PowerPointFactory {
         await zip.loadAsync(data);
 
         for (let key in zip.files) {
-            if (zip.files.hasOwnProperty(key)) {
+            if (Object.prototype.hasOwnProperty.call(zip.files, key)) {
                 let ext = key.substr(key.lastIndexOf('.'));
 
                 if (ext === '.xml' || ext === '.rels') {

This issue body was partially generated by patch-package.

Support for tables?

Guys,

Does this library support adding tables? If not, is there a way to use
https://github.com/gitbrent/PptxGenJS/
to create a slide and add it using node-pptx?

I tried something like

const PPTX = require('nodejs-pptx');
let pptx = new PPTX.Composer();
const fs = require('fs');
const pptxgenjs  =  require("pptxgenjs");
let pres = new pptxgenjs();

let slide = pres.addSlide();
slide.bkgd = '#39006A';
slide.addText('rajat');
async function init() {
    await pptx.load(`./existing.pptx`);
    await pptx.compose(async pres => {
        pres.addSlide(slide);
        fs.writeFile('helloworlds.xml',await pres.getSlide('slide1'), function (err) {
            if (err) return console.log(err);
        });
    });
    await pptx.save(`./existing1.pptx`);
}
init();

but have been unsuccessful so far.
I get UnhandledPromiseRejectionWarning: TypeError: config is not a function

npm package not updated?

looking at the NPM packages I can see the following :

image

but github shows a latest version of 2021.

image

Can I suggest pushing out the updates to NPM, or updating the installation instructions to load the release from Github?

Size not decreasing when removing the slides from a presentation

When I remove the slides from a presentation of size 10MB with 5 slides, the resulting presentation is also 10MB. Even after removing 4 of the 5 slides, its still 10MB.

const PPTX = require('node-pptx');
let pptx = new PPTX.Composer();

await pptx.load('./sample.pptx')
await pptx.compose(async pres => {
  pres.removeSlide(pres.getSlide('slide1')); 
  

});
await pptx.save('./test.pptx');

It would be nice if the size decreased with reducing slides. Please correct me if I'm making a mistake.
And is there a way to ADD slides from existing presentation to another presentation.
Something like this...

slide1=pres1.getSlide('slide1')
pres2.addSlide(slide1)

Is it possible to edit an existing Text Box?

I have a template pptx that I need to generate copies of by swapping data in and out of it. Is it possible to reference a specific text box? Even a plain getElements() would be great! Any insight on this?

Thank you, the package is awesome!

Text over image

Hey,
I've been using the library for a week now, so far I haven't had any problems and a really good library!
I encountered the first problem when trying to add an image that will serve as a background to the text and I can't do it.
I manage to add text over text and add image over image.
But I can't add text over a picture.
I tried to create the image first and then the text and vice versa, but always the image is before the text. Am I missing something? Or is there really a problem with that?
Maybe there is another way to do it?
Many thanks in advance,

Example to my code:

slide.addText(text => {
        text
            .value("text")
            .textColor("ffffff")
            .backgroundColor("000000")
            .x(0)
            .cx(SlideSettings.headerSettings.width)
            .y(0)
            .cy(SlideSettings.headerSettings.height)
            .textVerticalAlign("center")
            .textAlign("center");
    });

  // Add background image
   slide.addImage(image => {
        image
            .data(base64_encode(`D:/projects/PowerPoint generator/Public/Images/Gradiant3.png`))
            .x(0)
            .cx(SlideSettings.headerSettings.width)
            .y(-2)
            .cy(SlideSettings.headerSettings.height + 5);
    });

    // Add tag image
    slide.addImage(image => {
        image
            .data(base64_encode('D:/projects/PowerPoint generator/Public/Images/a_tag.png'))
            .x(0)
            .cx(SlideSettings.tagSettings.width)
            .y(-2)
            .cy(SlideSettings.tagSettings.height);
    });

    // Add tag image
    slide.addImage(image => {
        image
            .data(base64_encode('D:/projects/PowerPoint generator/Public/Images/b_tag.png'))
            .x(SlideSettings.headerSettings.width - SlideSettings.tagSettings.width - 10)
            .cx(SlideSettings.tagSettings.width)
            .y(5)
            .cy(SlideSettings.tagSettings.height);
    });

Ron

How to use Bullet points?

Hi - I am new to Node JS and wanted to create a PPT with bullet points using node-pptx.
Could you please add examples for bullet points example or let me know how to do it?

modify slide

how can I edit text, images and other elements of a slide?

await is only valid in async function

when i folow examples , i have some issues that ' i cant use await becuz it can be used only in async function' but i saw that node-pptx is sync funtion,,,
so how can i use await like you used in examples??

Copying slides

Is there a way to copy an existing slide into a new slide?

Something like:

pres.copySlide(pres.getSlide(3))

Allow passing an image name when adding to Slide

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/nodejs-pptx/lib/factories/index.js b/node_modules/nodejs-pptx/lib/factories/index.js
index 7809e88..15b7b82 100644
--- a/node_modules/nodejs-pptx/lib/factories/index.js
+++ b/node_modules/nodejs-pptx/lib/factories/index.js
@@ -199,11 +199,11 @@ class PowerPointFactory {
         }
     }
 
-    addImage(slide, image) {
-        image.setContent(this.pptFactory.addImage(slide, image));
+    addImage(slide, image, name) {
+        image.setContent(this.pptFactory.addImage(slide, image, name));
     }
 
-    async addImageFromRemoteUrl(slide, image) {
+    async addImageFromRemoteUrl(slide, image, name) {
         image.source = await new Promise(function(resolve, reject) {
             request.get(image.downloadUrl, { timeout: 30000 }, function(err, res, buffer) {
                 if (err) reject(err);
@@ -211,7 +211,7 @@ class PowerPointFactory {
             });
         });
 
-        return this.addImage(slide, image);
+        return this.addImage(slide, image, name);
     }
 
     addText(slide, textBox) {
diff --git a/node_modules/nodejs-pptx/lib/factories/ppt/index.js b/node_modules/nodejs-pptx/lib/factories/ppt/index.js
index 9652f45..4796637 100644
--- a/node_modules/nodejs-pptx/lib/factories/ppt/index.js
+++ b/node_modules/nodejs-pptx/lib/factories/ppt/index.js
@@ -74,21 +74,21 @@ class PptFactory {
         this.slideFactory.moveSlide(sourceSlideNum, destinationSlideNum);
     }
 
-    addImage(slide, image) {
+    addImage(slide, image, name = uuidv4()) {
         let mediaName = '';
         let source = image.source;
 
         if (image.sourceType === 'file') {
-            mediaName = `image-${uuidv4()}${path.extname(source)}`;
+            mediaName = `${name}${path.extname(source)}`;
         } else if (image.sourceType === 'base64') {
             let imageExt = 'png'; // assume png unless otherwise specified
 
             if (source && /image\/(\w+);/.exec(source) && /image\/(\w+);/.exec(source).length > 0) imageExt = /image\/(\w+);/.exec(source)[1];
             if (source.indexOf(';') > -1) source = source.split(';').pop();
 
-            mediaName = `image-${uuidv4()}.${imageExt}`;
+            mediaName = `${name}.${imageExt}`;
         } else if (image.sourceType === 'url') {
-            mediaName = `image-${uuidv4()}${path.extname(image.downloadUrl)}`;
+            mediaName = `${name}${path.extname(image.downloadUrl)}`;
         } else {
             throw new Error('Invalid "sourceType" specified in PptFactory.addImage(). Possible values: "base64," "file," or "binary."');
         }
diff --git a/node_modules/nodejs-pptx/lib/slide.js b/node_modules/nodejs-pptx/lib/slide.js
index b5c93f0..98870e5 100644
--- a/node_modules/nodejs-pptx/lib/slide.js
+++ b/node_modules/nodejs-pptx/lib/slide.js
@@ -56,7 +56,7 @@ class Slide {
         }
     }
 
-    async addImage(config) {
+    async addImage(config, name) {
         let image = new Image();
 
         try {
@@ -67,9 +67,9 @@ class Slide {
 
         try {
             if (image.sourceType === 'file' || image.sourceType === 'base64') {
-                this.powerPointFactory.addImage(this, image);
+                this.powerPointFactory.addImage(this, image, name);
             } else if (image.sourceType === 'url') {
-                await this.powerPointFactory.addImageFromRemoteUrl(this, image);
+                await this.powerPointFactory.addImageFromRemoteUrl(this, image, name);
             }
 
             this.elements.push(image);

This issue body was partially generated by patch-package.

Find/Replace text, Get list of slides containing some text

I have a presentation with a lot of variable placeholders that look like this: {some_variable}

I'd like to find those and replace them with the proper values (from a JSON file). I'm doing this now with docxtemplater, but I'd rather use node-pptx.

In addition, I'd like to search the presentation for the remaining instances of a variable that were not filled in - this is an indication that there's no data for that slide and it should be removed. I'd like to search for, say, "{remove_slide}" and remove that slide. I could do it with a list of slides containing that variable, then I'd sort it in descending order, then use node-pptx to delete the slides. Sorting the list descending seems like the proper path so you don't screw up the slide ordering while you're deleting slides. Maybe there's a simpler way.

deep require is no longer supported for uuid library

First of all, thank you for your very helpful library !

I ran into the following problem:
In the file node-pptx/lib/factories/ppt/index.js line 5 a deep requires is used for the uuid library :
const uuidv4 = require('uuid/v4');
According to their page this is deprecated and no longer supported: https://github.com/uuidjs/uuid#deep-requires-no-longer-supported

Therefore the following error is thrown in my project: Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports" in .../node_modules/uuid/package.json

Update data of existing chart

I've got an existing set of slides which have a couple of charts and a couple of tables on them. I'd like to be able to update the Excel data which sits behind the charts, is that possible? I couldn't see a getChart function.

I'd like to update the data in the tables too. The tables aren't Excel as far as I know, they're just PowerPoint's 'Insert Table' tables. I don't mind tidying the slide up manually if there gets to be too many rows, but I'd like to be able to start with what I've created within PowerPoint, rather than having to recreate it all from scratch.

How to position text?

I'm trying to figure out how to position the text using the x and y values

await pres.getSlide('slide3')
            .addText(text => {
                text.value('val')
                    .x(10)
                    .y(50)
            })

Is there a way by which I can know exactly what values to pass for x and y? For example, by opening an existing ppt and getting the position of a similar element and then passing those values in the function.
Or do I have to do this by trial and error?

Textbox height/width options?

I notice textboxes all come in the same size, can I set the height and width as needed? I see the constructor for Textbox has cx and cy but no props to override it?

Apply Master

Hey,

is there a possibility to apply a slide master?
In my case, I have an existing .pptx-file and I want to change the slide master of an added slide (considering there is more than one slide master).

Trying to integrate with this from my electron app with vue

Hi all, thanks all for creating and maintaining this package.

I am trying to create an electron app that creates a pptx file via nodejs. I have added a dependency to this package and trying to run it, but whenever I try to import an existing pptx file, it throws below error [1], which seems like coming from this line [2]. I have listed below how to reproduce this error. Please let me know if there's anything I am missing or you need anything else to debug. Thank you so much!

[1]

(node:34950) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/../fragments/[Content_Types].xml'
    at Object.openSync (node:fs:601:3)
    at Object.func [as openSync] (node:electron/js2c/asar_bundle:2:1869)
    at Object.readFileSync (node:fs:469:35)
    at Object.readFileSync (node:electron/js2c/asar_bundle:2:9212)
    at ContentTypeFactory.build (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:197426:31)
    at PowerPointFactory.build (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:204005:29)
    at new PowerPointFactory (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:203972:10)
    at new Presentation (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:204163:30)
    at new Composer (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:204292:25)
    at generatePPTX (/Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:205722:15)
    at /Users/dongkyukim/workplace/hymn-lyrics-ppt-app/packages/electron-app/dist-electron/main.js:205781:3
    at WebContents.<anonymous> (node:electron/js2c/browser_init:2:89468)
    at WebContents.emit (node:events:513:28)

[2]

xml2js.parseString(fs.readFileSync(`${__dirname}/../fragments/[Content_Types].xml`), (err, js) => {

How to reproduce

  1. git clone https://github.com/dongkyu9315/hymn-lyrics-ppt-app.git
  2. cd hymn-lyrics-ppt-app
  3. yarn run dev:all (this will start an electron app)
  4. Click the Trigger convert button and you will see an error

can we edit existing slide element?

Screen Shot 2021-04-30 at 4 48 58 PM

i want to replace all *cross with actual value coming from REST API, can we do this?
from documentation i could see to add new element
await pres.getSlide('slide1').addImage(image => {
image
.file(./images/pizza.jpg)
.x(500)
.y(100)
.cx(166)
.cy(100);
});

TypeError: zip.files.hasOwnProperty is not a function

TypeError: zip.files.hasOwnProperty is not a function
at PowerPointFactory.loadFromRawFileData (node_modules/nodejs-pptx/lib/factories/index.js:42:27)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Presentation.loadExistingPPTX (node_modules/nodejs-pptx/lib/presentation.js:99:13)
at async Composer.load (node_modules/nodejs-pptx/lib/composer.js:15:9)

Installation Errors

I got this error while installing it.

Error: Can't find Python executable "python", you can set the PYTHON env variable.

.. I tried few things like i tried to set python env variables but no use.

Please suggest a fix.
error

multi lines was saved as 1 line

test code

  let pptx = new PPTX.Composer()
  await pptx.load(__dirname + '/../../templates/test.pptx')
  const tmpPPTX = '/tmp/worship' + Math.random() + '.pptx'  
  await pptx.save(tmpPPTX)

In the test.pptx, I have a slide which has two lines as its tile.

first line
second line

but the saved ppt always rendered to 1 line

first line second line

I looked into the codes, we are using
xml2js.parseString to parse xml string to js obj.
and use xml2js.Builder to save js obj back to xml string

with below code, we can easily reproduce the issue:

let xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a:p><a:pPr marL="0" lvl="0" indent="0" algn="ctr" rtl="0"><a:spcBef><a:spcPts val="0"/></a:spcBef><a:spcAft><a:spcPts val="0"/></a:spcAft><a:buClr><a:schemeClr val="dk1"/></a:buClr><a:buSzPts val="1100"/><a:buFont typeface="Arial"/><a:buNone/></a:pPr><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>first line</a:t></a:r><a:br><a:rPr lang="en-CA" altLang="ja-JP" dirty="0"/></a:br><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>second line</a:t></a:r><a:endParaRPr lang="ja-JP" altLang="en-US" dirty="0"/></a:p>'

  xml2js.parseString(xml, function(err, js) {
    let builder = new xml2js.Builder({ renderOpts: { pretty: false } });
    let savexml = builder.buildObject(js);
    console.log(xml)
    console.log(savexml)
    console.log(xml === savexml)
  });

The output is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a:p><a:pPr marL="0" lvl="0" indent="0" algn="ctr" rtl="0"><a:spcBef><a:spcPts val="0"/></a:spcBef><a:spcAft><a:spcPts val="0"/></a:spcAft><a:buClr><a:schemeClr val="dk1"/></a:buClr><a:buSzPts val="1100"/><a:buFont typeface="Arial"/><a:buNone/></a:pPr><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>first line</a:t></a:r><a:br><a:rPr lang="en-CA" altLang="ja-JP" dirty="0"/></a:br><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>second line</a:t></a:r><a:endParaRPr lang="ja-JP" altLang="en-US" dirty="0"/></a:p>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a:p><a:pPr marL="0" lvl="0" indent="0" algn="ctr" rtl="0"><a:spcBef><a:spcPts val="0"/></a:spcBef><a:spcAft><a:spcPts val="0"/></a:spcAft><a:buClr><a:schemeClr val="dk1"/></a:buClr><a:buSzPts val="1100"/><a:buFont typeface="Arial"/><a:buNone/></a:pPr><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>first line</a:t></a:r><a:r><a:rPr lang="ja-JP" altLang="en-US"/><a:t>second line</a:t></a:r><a:br><a:rPr lang="en-CA" altLang="ja-JP" dirty="0"/></a:br><a:endParaRPr lang="ja-JP" altLang="en-US" dirty="0"/></a:p>
false

As you can see in the output, the break line node <a:br> was saved after the two lines <a:r> rather than between them.
That is the reason why multi line was saved into 1 line.

npm package not installing correctly

npm i nodejs-pptx doesn't seem to install all the correct files. Right now I see index.js, the license file, package.json, and readme.md.
Can you please check/fix this? Thank you!

Refactor this name

Image is already a defined class within JavaScript (at least in the browser environment.) To prevent confusion I think we should refactor this name like we did the Text class.

Additionally, the addImage and addSlide methods are confusing in terms of their responsibilities. I'd assume that if you created an Image you would not call addImage on an image...

class Image extends ElementProperties {

Would you accept a PR to allow loading pptx from buffer OR file path?

Issue with loading PowerPoint file from buffer

The save() method allows you to specify either a callback (passing buffer) or file path, but when loading a PowerPoint file, you must pass a file path. This can be pretty inconvenient in cases where you already have the PowerPoint file contents in a buffer.

Proposed solution

To address this issue, can we please add support for loading from a buffer as well as a file path.

I've quickly scanned your code, and at first glance it looks like it's as simple as changing loadExistingPPTX() :

async loadExistingPPTX(done) {
if (this.templateFilePath !== null) {
await this.powerPointFactory.loadFromRawFileData(fs.readFileSync(this.templateFilePath));
}
}

To something like:

async loadExistingPPTX(done) {
    // Throw an error if templateFilePath is null or undefined
    if (!this.templateFilePath) {
        throw new Error('Input is null or undefined. Input must be a file path or buffer.');
    }

    try {
        // Check if templateFilePath is a string (file path)
        if (typeof this.templateFilePath === 'string') {
            // Load PowerPoint file from the specified file path using fs.readFileSync()
            await this.powerPointFactory.loadFromRawFileData(fs.readFileSync(this.templateFilePath));
        }
        // Check if templateFilePath is a buffer
        else if (Buffer.isBuffer(this.templateFilePath)) {
            // Load PowerPoint file from the buffer directly using powerPointFactory.loadFromRawFileData()
            await this.powerPointFactory.loadFromRawFileData(this.templateFilePath);
        }
        // If templateFilePath is neither a string nor a buffer, throw an error
        else {
            throw new Error('Invalid input type. Input must be a file path or buffer.');
        }
    } catch (error) {
        // Catch any errors that occur during loading and rethrow with a more descriptive message
        throw new Error(`Error loading PPTX file: ${error.message}`);
    }
}

Obviously we'd also need to change the variable name to templateFile or similar and any other housekeeping as needed.

Would you accept a PR from me along these lines to add this feature?

Extract presenter notes?

How can one extract just the presenter notes?
The solution which I'm using is this:

// const fs = require("fs");
const PPTX2Json = require("pptx2json");
const pptx2json = new PPTX2Json();

pptx2json.toJson("./test.pptx").then((json) => {
  json = Object.keys(json)
    .filter((key) => key.includes("ppt/notesSlides/notesSlide"))
    .sort((a, b) => a - b)
    .map((key) =>
      json[key]["p:notes"]["p:cSld"][0]["p:spTree"][0]["p:sp"]
        .filter((item) => item["p:txBody"])
        .map((item) => item["p:txBody"][0]["a:p"])
        .map((item) => item.map((item) => item["a:r"]))[0]
        .filter((x) => x)
        .map((item) => [
          ...item.reduce((prev, curr) => [...prev, curr["a:t"]], []),
        ])
        .join(" ")
    );
  console.log(json);
  // fs.writeFileSync("./data.txt", JSON.stringify(json));
});

Problems calling the library front next.js

Hello - I'm running into this error when the node function is being called from a next.js runtime. My file/function works fine when calling directly from node, but it appears to fail to load based on a file path:

  • error unhandledRejection: Error: ENOENT: no such file or directory, open '/Users/username/dev/app/.next/server/app/api/endpoint/../fragments/[Content_Types].xml'

It seems like the next.js runtime is a different path than the node_modules or the Content_Types.xml. I have tried putting the fragments directory in public and fetching the file, i have tried absolute paths, I'm not sure why it's not working when being called by the API. I found the reference in this file which could be causing the issue:

node-pptx/fragments/[Content_Types].xml

    build() {
        xml2js.parseString(fs.readFileSync(`${__dirname}/../fragments/[Content_Types].xml`), (err, js) => {
            this.content['[Content_Types].xml'] = js;
        });

        this.addDefaultMediaContentTypes();
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.