Exporting Google Slides to Images
Have you ever needed to convert your Google Slides presentation into a series of images? Maybe you want to share your slides on social media, include them in a document, or use them in a video. Whatever your reason, manually exporting each slide can be time-consuming and tedious. But don't worry! In this tutorial, I'll show you how to automate this process using Google Apps Script. With just a few lines of code, you'll be able to export all your slides as high-quality PNG images and store them neatly in a folder.
This project is perfect for educators, marketers, or anyone who frequently works with presentations. The best part? It'll take you less than 30 minutes to set up, and once done, you can use it for all your future presentations!
Prerequisites
This tutorial assumes the following prerequisites:
Basic knowledge of Google Apps Script. If you're new to Apps Script, check out my Google Apps Script Tutorial Series to get started.
Familiarity with Google Slides.
5 steps to implement the Google Slides to Images exporter
Step 1 — Create a new Apps Script project
First, let's create a new Apps Script project:
Go to script.new to create a new Apps Script project.
Delete any code in the editor.
Rename the project to "Slides to Images Exporter" by clicking on "Untitled project" in the top left corner.
Step 2 — Write a function to export slides to images
Now, let's write our main function that will export the slides to images. Copy and paste the following code into the Code.gs file:
function exportSlidesToImages() {
var presentation = SlidesApp.openById("{{PRESENTATION_ID}}");
var slides = presentation.getSlides();
var presentationFile = DriveApp.getFileById(presentation.getId());
var parentFolder = presentationFile.getParents().next();
// Check if 'images' folder exists, create it if it doesn't
var imagesFolderIterator = parentFolder.getFoldersByName('images');
var imagesFolder;
if (imagesFolderIterator.hasNext()) {
imagesFolder = imagesFolderIterator.next();
} else {
imagesFolder = parentFolder.createFolder('images');
}
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var slideId = slide.getObjectId();
// Export the slide as an image with retry mechanism
var success = false;
var retries = 0;
var maxRetries = 5;
var retryDelay = 2500;
while (!success && retries < maxRetries) {
retries++;
try {
var url = 'https://docs.google.com/presentation/d/' + presentation.getId() + '/export/png?id=' + presentation.getId() + '&pageid=' + slideId;
var options = {
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 200) {
var imageBlob = response.getBlob().setName((i + 1) + '.png');
imagesFolder.createFile(imageBlob);
success = true;
Logger.log('Exported slide ' + (i + 1));
} else if (response.getResponseCode() === 429) {
Logger.log('Rate limit hit for slide ' + (i + 1) + '. Retrying after delay...');
Utilities.sleep(retryDelay);
} else {
throw new Error('Failed to export slide ' + (i + 1) + '. Response Code: ' + response.getResponseCode());
}
} catch (error) {
Logger.log(error.toString());
if (retries >= maxRetries) {
Logger.log('Max retries reached for slide ' + (i + 1) + '. Moving to next slide.');
} else {
Logger.log('Retrying slide ' + (i + 1) + ' after error...');
Utilities.sleep(retryDelay);
}
}
}
// Add a small delay between successful exports to avoid rate limiting
if (success) {
Utilities.sleep(retryDelay);
}
}
}
Important: Replace the Presentation ID
Make sure to replace {{PRESENTATION_ID}}
with the actual ID of your Google Slides presentation. You can find this ID in the URL of your presentation. It's the long string of characters between "/d/" and "/edit" in the URL.
Step 3 — Set up authorization and run the script
Now that we've written our script, let's run it:
Click on the "Run" button (play icon) in the toolbar.
You'll be prompted to review permissions. Click "Review permissions".
Choose your Google account.
You may see a warning that the app isn't verified. Click "Advanced" and then "Go to Slides to Images Exporter (unsafe)".
Review the permissions and click "Allow".
Step 4 — Check the exported images
After the script finishes running:
Open Google Drive and navigate to the folder containing your presentation.
You should see a new folder called "images".
Open this folder to find your exported slide images.
Step 5 — Customize the script
You can customize this script to fit your needs. For example:
Change the export format:Replace export/png
with export/pdf
in the URL to export as PDF.
Replace export/png
with export/svg
in the URL to export as SVG.
Replace export/png
with export/pptx
in the URL to export as PPTX.
Replace export/png
with export/txt
in the URL to export as PPTX.
Replace export/png
with export/jpeg
in the URL to export as JPG.
Replace export/png
with export/odp
in the URL to export as ODP.
Note
If you change the export format, remember to also change the exported file name in this line:
var imageBlob = response.getBlob().setName((i + 1) + '.png');
Modify the file naming
Replace export/png
with export/pdf
in the URL to export as PDF.
Replace export/png
with export/svg
in the URL to export as SVG.
Replace export/png
with export/pptx
in the URL to export as PPTX.
Replace export/png
with export/txt
in the URL to export as PPTX.
Replace export/png
with export/jpeg
in the URL to export as JPG.
Replace export/png
with export/odp
in the URL to export as ODP.
Note If you change the export format, remember to also change the exported file name in this line:
var imageBlob = response.getBlob().setName((i + 1) + '.png');
Change (i + 1) + '.png'
to include the presentation name or other identifiers.
How the code works
Let's break down the script to understand how it works:
1. Opening the presentationvar presentation = SlidesApp.openById("{{PRESENTATION_ID}}");
var slides = presentation.getSlides();
var presentation = SlidesApp.openById("{{PRESENTATION_ID}}");
var slides = presentation.getSlides();
This opens the specified presentation and gets all its slides.
2. Setting up the output foldervar presentationFile = DriveApp.getFileById(presentation.getId());
var parentFolder = presentationFile.getParents().next();
var imagesFolderIterator = parentFolder.getFoldersByName('images');
var imagesFolder;
if (imagesFolderIterator.hasNext()) {
imagesFolder = imagesFolderIterator.next();
} else {
imagesFolder = parentFolder.createFolder('images');
}
var presentationFile = DriveApp.getFileById(presentation.getId());
var parentFolder = presentationFile.getParents().next();
var imagesFolderIterator = parentFolder.getFoldersByName('images');
var imagesFolder;
if (imagesFolderIterator.hasNext()) {
imagesFolder = imagesFolderIterator.next();
} else {
imagesFolder = parentFolder.createFolder('images');
}
This code creates an 'images' folder in the same location as the presentation, or uses an existing one if it's already there.
3. Exporting each slidefor (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var slideId = slide.getObjectId();
// ... (export logic)
}
for (var i = 0; i < slides.length; i++) {
var slide = slides[i];
var slideId = slide.getObjectId();
// ... (export logic)
}
This loop goes through each slide and exports it as an image.
4. Handling rate limits and errorswhile (!success && retries < maxRetries) {
// ... (export attempt)
if (response.getResponseCode() === 429) {
Logger.log('Rate limit hit for slide ' + (i + 1) + '. Retrying after delay...');
Utilities.sleep(retryDelay);
}
// ... (other error handling)
}
while (!success && retries < maxRetries) {
// ... (export attempt)
if (response.getResponseCode() === 429) {
Logger.log('Rate limit hit for slide ' + (i + 1) + '. Retrying after delay...');
Utilities.sleep(retryDelay);
}
// ... (other error handling)
}
This code implements a retry mechanism to handle rate limits and other potential errors.
5. Saving the imagevar imageBlob = response.getBlob().setName((i + 1) + '.png');
imagesFolder.createFile(imageBlob);
var imageBlob = response.getBlob().setName((i + 1) + '.png');
imagesFolder.createFile(imageBlob);
If the export is successful, this saves the image in the 'images' folder.
Conclusion
Congratulations! You've just created a powerful tool to automate the process of exporting Google Slides to images. This script will save you countless hours of manual work, especially if you frequently need to convert presentations to images.
We've covered how to set up the script, run it, and even customize it to fit your specific needs. Remember, automation like this is key to increasing your productivity and freeing up time for more important tasks.
Thank you for following along with this tutorial. I hope you find this script useful in your work with Google Slides!