Converting Large images to Google Document by OCR using Google Apps Script

Gists

This is a sample script for converting the large images to Google Document by OCR using Google Apps Script.

When the image size, the image file size, the resolution of the image, and so on are large, an error like Request Too Large occurs. In this sample script, such the image can be converted to Google Document by reducing them.

Sample script

Please enable Drive API at Advanced Google services.

function myFunction() {
  const fileId = "###"; // Please set the file ID of image file.

  try {
    const file = DriveApp.getFileById(fileId);
    Drive.Files.insert({ title: file.getName() }, file.getBlob(), {
      ocr: true,
    });
  } catch ({ message }) {
    if (message.includes("Request Too Large")) {
      const link = Drive.Files.get(file.getId()).thumbnailLink.replace(
        /=s.+/,
        "=s2000"
      );
      Drive.Files.insert(
        { title: file.getName() },
        UrlFetchApp.fetch(link).getBlob(),
        { ocr: true }
      );
    } else {
      throw new Error(message);
    }
  }
}

When this script is run, by converting the image to Google Document, a new Google Document is created in the root folder as the top image.

Reference

 Share!