Sunday, April 14, 2024

How to Extract Images from Google Docs and Google Slides

Share

Learn how extract all the embedded images from a Google Document or Google Slides presentation and save them as individual files in a specified folder in your Google Drive.

Imagine you’re working with a lengthy Google Document, or a Google Slides presentation, and you need to extract all the embedded images from the text and save them as individual files.


Extract Images in Google Docs

A simple solution to address this issue is as follows: convert your Google Document or Google Slide into a web page. Here’s how you can do it:

Go to the “File” menu. Select the “Share” submenu and then choose “Publish to Web.” It will generate a public web page that contains all the images from your document or slide. You can simply right-click an image on the page and select the “Save Image” option download it to your local disk.

What we have just discussed is a manual process but we can easily automate this with the help of Google Apps Script.

Open your Google Document containing the images, go to the Extensions menu and choose Apps Script. Copy-paste the code below and run the saveGoogleDocsImages function to download all images to a specific folder in your Google Drive.

The images are sequentially numbered and the file extension is the same as that of the embedded inline image.

function saveGoogleDocsImages() {
  
  const folderName = 'Document Images';

  
  const folders = DriveApp.getFoldersByName(folderName);

  
  const folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folderName);

  
  DocumentApp.getActiveDocument()
    .getBody()
    .getImages()
    .forEach((image, index) => {
      
      const blob = image.getBlob();

      
      const [, fileExtension] = blob.getContentType().split('/');

      
      const fileName = `Image #${index + 1}.${fileExtension}`;

      
      blob.setName(fileName);

      
      folder.createFile(blob);

      
      Logger.log(`Saved ${fileName}`);
    });
}

The Apps Script code to download images from a Google Slides presentation is similar. The function iterates over the slides in the presentation and then for each slide, the function iterates over the images in that slide.

function extractImagesFromSlides() {
  
  const folderName = 'Presentation Images';

  
  const folders = DriveApp.getFoldersByName(folderName);

  
  const folder = folders.hasNext() ? folders.next() : DriveApp.createFolder(folderName);

  
  SlidesApp.getActivePresentation()
    .getSlides()
    .forEach((slide, slideNumber) => {
      
      slide.getImages().forEach((image, index) => {
        
        const blob = image.getBlob();

        
        const fileExtension = blob.getContentType().split('/')[1];

        const fileName = `Slide${slideNumber + 1}_Image${index + 1}.${fileExtension}`;

        
        blob.setName(fileName);

        
        folder.createFile(blob);

        Logger.log(`Saved ${fileName}`);
      });
    });
}

Table of contents

Read more

Local News