How To Extract Images From Google Docs and Google
How To Extract Images From Google Docs and Google
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.
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() {
// Define the folder name where the extracted images will be saved
// Get all the images in the document's body and loop through each image
DocumentApp.getActiveDocument()
.getBody()
.getImages()
// Extract the file extension from the Blob's content type (e.g., 'jpeg', 'png')
// Generate a unique file name for each image based on its position in the document
blob.setName(fileName);
// Create a new file in the specified folder with the image data
folder.createFile(blob);
});
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() {
// Define the folder name where the extracted images will be saved
SlidesApp.getActivePresentation()
.getSlides()
.forEach((slide, slideNumber) => {
// Extract the file extension from the Blob's content type (e.g., 'jpeg', 'png')
blob.setName(fileName);
// Create a new file in the specified folder with the image data
folder.createFile(blob);
Logger.log(`Saved ${fileName}`);
});
});