Methods to Extract Pictures from Google Docs and Google Slides

Shubham
5 Min Read

Think about you’re working with a prolonged Google Doc, or a Google Slides presentation, and it is advisable to extract all of the embedded pictures from the textual content and save them as particular person recordsdata.

A easy resolution to deal with this problem is as follows: convert your Google Doc or Google Slide into an internet web page. Right here’s how you are able to do it:

Go to the “File” menu. Choose the “Share” submenu after which select “Publish to Internet.” It’s going to generate a public internet web page that accommodates all the photographs out of your doc or slide. You possibly can merely right-click a picture on the web page and choose the “Save Picture” possibility obtain it to your native disk.

What now we have simply mentioned is a guide course of however we are able to simply automate this with the assistance of Google Apps Script.

Open your Google Doc containing the photographs, go to the Extensions menu and select Apps Script. Copy-paste the code beneath and run the saveGoogleDocsImages perform to obtain all pictures to a selected folder in your Google Drive.

The pictures are sequentially numbered and the file extension is similar as that of the embedded inline picture.

perform saveGoogleDocsImages() {
  // Outline the folder identify the place the extracted pictures will likely be saved
  const folderName = 'Doc Pictures';

  // Examine if a folder with the desired identify already exists
  const folders = DriveApp.getFoldersByName(folderName);

  // If the folder exists, use it; in any other case, create a brand new folder
  const folder = folders.hasNext() ? folders.subsequent() : DriveApp.createFolder(folderName);

  // Get all the photographs within the doc's physique and loop by way of every picture
  DocumentApp.getActiveDocument()
    .getBody()
    .getImages()
    .forEach((picture, index) => {
      // Get the picture knowledge as a Blob
      const blob = picture.getBlob();

      // Extract the file extension from the Blob's content material kind (e.g., 'jpeg', 'png')
      const [, fileExtension] = blob.getContentType().break up("https://www.labnol.org/");

      // Generate a novel file identify for every picture based mostly on its place within the doc
      const fileName = `Picture #${index + 1}.${fileExtension}`;

      // Set the Blob's identify to the generated file identify
      blob.setName(fileName);

      // Create a brand new file within the specified folder with the picture knowledge
      folder.createFile(blob);

      // Log a message indicating that the picture has been saved
      Logger.log(`Saved ${fileName}`);
    });
}

The Apps Script code to obtain pictures from a Google Slides presentation is analogous. The perform iterates over the slides within the presentation after which for every slide, the perform iterates over the photographs in that slide.

perform extractImagesFromSlides() {
  // Outline the folder identify the place the extracted pictures will likely be saved
  const folderName = 'Presentation Pictures';

  // Examine if a folder with the desired identify already exists
  const folders = DriveApp.getFoldersByName(folderName);

  // If the folder exists, use it; in any other case, create a brand new folder
  const folder = folders.hasNext() ? folders.subsequent() : DriveApp.createFolder(folderName);

  // Iterate by way of every slide within the lively presentation
  SlidesApp.getActivePresentation()
    .getSlides()
    .forEach((slide, slideNumber) => {
      // Retrieve all pictures on the present slide
      slide.getImages().forEach((picture, index) => {
        // Get the picture knowledge as a Blob
        const blob = picture.getBlob();

        // Extract the file extension from the Blob's content material kind (e.g., 'jpeg', 'png')
        const fileExtension = blob.getContentType().break up("https://www.labnol.org/")[1];

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

        // Set the Blob's identify to the generated file identify
        blob.setName(fileName);

        // Create a brand new file within the specified folder with the picture knowledge
        folder.createFile(blob);

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

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *