Automate PowerPoint Slide Creation with Node.js and ChatGPT
Written on
Chapter 1: Streamlining PowerPoint Creation
In my quest to automate the generation of PowerPoint presentations, I am focusing on strategies to simplify this task. My goal is to boost efficiency in my daily workflow, making it easier to produce a significant number of slides when necessary.
To achieve this, I considered leveraging ChatGPT to assist in writing a program that automates PowerPoint slide creation. I began this journey by taking the following steps.
I started with a basic prompt: "Can you create code in Node.js and npm for generating PowerPoint slides?" This initial inquiry was designed to seek a straightforward solution. After numerous iterations and adjustments, I arrived at the functional code provided below. Although it took several attempts to refine, this code represents an excellent starting point and a framework for further development.
const fs = require("fs");
const pptxgen = require("pptxgenjs");
// Create a new presentation
const ppt = new pptxgen();
// Read the text elements from a file
const filePath = "text_file.txt";
const texts = fs.readFileSync(filePath, "utf8").split("n").map((text) => text.trim());
// Set the initial coordinates and height for the first text element
let x = 1;
let y = 1;
const height = 0.2;
const maxTextsPerSlide = 4;
// Add the text elements to slides
let slide = ppt.addSlide();
let textCounter = 0;
texts.forEach((text) => {
const textOpts = {
x, // X coordinate of the text box
y, // Y coordinate of the text box
w: 8, // Width of the text box
h: height, // Height of the text box
color: "000000", // Text color
align: "left", // Text alignment
};
slide.addText(text, textOpts);
textCounter++;
if (textCounter % maxTextsPerSlide === 0) {
// Create a new slide after every four text elements
slide = ppt.addSlide();
x = 1;
y = 1;
} else {
// Increment the y value for the next text element within the set
y += height + 0.5; // Adjust the increment as needed
}
});
// Save the presentation to a file
ppt.writeFile({
fileName: "example.pptx",
}, (error) => {
if (error) {
console.log("Error saving PowerPoint file:", error);
return;
}
console.log("PowerPoint slides created successfully!");
});
Then navigate to the directory:
cd autoPowerPoint
Execute the script with:
node newcreate.js
Make sure to update the text_file.txt with your desired content for the PowerPoint slides. The current setup of the program allocates four lines per slide, but you can easily modify this to fit your specific needs. Running the program will generate a file named "example.pptx" as the final output.
For a demonstration, check out this video:
Chapter 2: Customizing Your Presentation
Feel free to tweak the code to adjust the number of lines per slide according to your requirements. This flexibility allows you to create presentations that meet your specific needs and preferences.