Skip to main content

Batch Export Google Slides as PNGs

Google Slides is a great way to generate images for things, like I did on this site. Each of my shelves and books images are exactly that. What if you want to export your entire slide deck at once, instead of having to manually export each one via the File menu? Google Apps Scripts of course! Here is my script, use it as you see fit.

  1. From your slide deck, go to Tools then Apps Scripts
  2. Enable the Slides API from the AS editor by clicking the Services menu
  3. Use the code below, save it, run it. It will ask for permissions, accept as usual. 
  4. Click the Run button once done
  5. Profit!

The script will run, it is a bit slow so don't be in a hurry - especially if you have a lot of slides in your deck! A new folder in your Google Drive will appear with the name of the slide deck as the name.

So the specific images in my slide deck had a single text box, with two lines. The script below will take the first line of the text box on each slide and use that as the image file's name. If your slides are a wee bit different, make sure you update the script accordingly. 

function exportSlidesAsImagesWithDashTitles() {
  const presentationId = SlidesApp.getActivePresentation().getId();
  const presentation = SlidesApp.openById(presentationId);
  const slides = presentation.getSlides();
  const folder = DriveApp.createFolder(presentation.getName() + "_Slide_Images");

  for (let i = 0; i < slides.length; i++) {
    const slide = slides[i];
    const slideObjectId = slide.getObjectId();
    const title = getFirstTextLine(slide) || `slide-${i + 1}`;
    const sanitizedTitle = title.replace(/[^\w\d]+/g, "-").replace(/-+/g, "-").replace(/^-|-$/g, "").substring(0, 50);

    const thumbnail = Slides.Presentations.Pages.getThumbnail(presentationId, slideObjectId, {
      'thumbnailProperties.mimeType': 'PNG',
      'thumbnailProperties.thumbnailSize': 'LARGE'
    });

    const imageBlob = UrlFetchApp.fetch(thumbnail.contentUrl).getBlob();
    folder.createFile(imageBlob).setName(sanitizedTitle + ".png");
  }

  Logger.log("Export complete! Folder: " + folder.getUrl());
}

function getFirstTextLine(slide) {
  const shapes = slide.getShapes();
  for (let shape of shapes) {
    if (shape.getShapeType() === SlidesApp.ShapeType.TEXT_BOX) {
      const text = shape.getText().asString().trim();
      if (text) {
        return text.split("\n")[0];
      }
    }
  }
  return null;
}