4

I have a folder of jpgs in Google Drive that I would like to convert to Google Docs. Now I can select each one manually and in the context menu "Open in Google Docs" This creates a new document with the image at the top of the page and OCR text below. I just want to do this with all my images.

There is a script here which converts gdoc to docx which I ought to be able to adapt for my case but I don't seem to be able to get it to work.

Here is my adapted script:

function convertJPGtoGoogleDocs() {
  var srcfolderId = "~~~~~~~~~Sv4qZuPdJgvEq1A~~~~~~~~~"; // <--- Please input folder ID.
  var dstfolderId = srcfolderId; // <--- If you want to change the destination folder, please modify this.
  var files = DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.JPG);
  while (files.hasNext()) {
    var file = files.next();
    DriveApp.getFolderById(dstfolderId).createFile(
      UrlFetchApp.fetch(
        "https://docs.google.com/document/d/" + file.getId() + "/export?format=gdoc",
        {
          "headers" : {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()},
          "muteHttpExceptions" : true
        }
      ).getBlob().setName(file.getName() + ".docx")
    );
  }
}

Can anyone help?

Thanks.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Packwood
  • 239
  • 4
  • 13
  • please don't delete this!! I don't have enough reputation to add a comment so I have to post it here. thank you for the great response. but what about 6 min limit. can you do something for that? or at least make it some way that in the second execution it wouldn't repeat the converted photos again and just continue where it left. – Amir Fotouhi Jan 23 '21 at 22:26

1 Answers1

5
  • You want to convert Jpeg files in a folder as Google Document.
  • When the Jpeg file is converted to Google Document, you want to use OCR.

If my understanding is correct, how about this modification?

Modification points:

  • In the script you modified, MimeType.JPG returns undefined. So the script in while is not run.
    • Please use MimeType.JPEG.
  • The script of this answer is used for exporting Google Document as Microsoft Word. Unfortunately, that script cannot be directly used for converting Jpeg file to Google Document.

If you want to modify the script of this answer, how about modifying as follows?

When you use this script, please enable Drive API at Advanced Google Services. By this, the API is automatically enabled at API console. The specification of Google Apps Script Project was Changed at April 8, 2019.

Modified script:

function convertJPGtoGoogleDocs() {
  var srcfolderId = "~~~~~~~~~Sv4qZuPdJgvEq1A~~~~~~~~~"; // <--- Please input folder ID.
  var dstfolderId = srcfolderId; // <--- If you want to change the destination folder, please modify this.
  var files = DriveApp.getFolderById(srcfolderId).getFilesByType(MimeType.JPEG); // Modified
  while (files.hasNext()) {
    var file = files.next();
    Drive.Files.insert({title: file.getName(), parents: [{id: dstfolderId}]}, file.getBlob(), {ocr: true}); // Modified
  }
}

Note:

References:

If I misunderstand your question, please tell me. I would like to modify it.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Packwood Welcome. I'm glad your issue was resolved. Thank you, too. – Tanaike Dec 10 '18 at 22:10
  • Notice - you need to enable the Drive API through the Apps Script, not thorough the GCP. Use the "Resources/Advanced Google Services" menu entry of the script editor (at script.google.coom) – Avi Sep 14 '19 at 16:03
  • @Avi Thank you for your comment. Yes. You are correct. [The specification of Google Apps Script Project was Changed at April 8, 2019](https://developers.google.com/apps-script/guides/cloud-platform-projects). I forgot to modify my answer. I deeply apologize for this. I modified it just now. – Tanaike Sep 14 '19 at 22:46
  • 1
    No need to apologize. I must thank you. Your answers have helped me SO much! – Avi Sep 15 '19 at 11:07
  • @exsonic01 Thank you for your comment. I deeply apologize for my poor English skill. From your error message, I think that you might have run the script without enabling Drive API at Advanced Google services. So how about enabling Drive API at Advanced Google services? [Ref](https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services) If this was not the direct solution, I apologize. – Tanaike Feb 18 '20 at 02:41
  • @Tanaike Thanks, I figured your I need to change 'JPEG' to 'JPG' or other figure type to execute. So I deleted comments. However, your script is still not working, creating nothing on my google drive. No error, but no google doc created from uploaded jpg files. What am I doing wrong? – exsonic01 Feb 18 '20 at 02:58
  • @exsonic01 Thank you for replying. I would like to support you. But, unfortunately, I cannot understand about your situation from your replying. This is due to my poor English skill. I apologize for this. So in order to correctly understand about your situation, can you post your issue as a question by including the detail information? By this, I would like to confirm your situation. If you can cooperate to resolve your issue, I'm glad. – Tanaike Feb 18 '20 at 03:02
  • @Tanaike I uploaded 5~6 jpg files in my google drive folder. Then I went to my script.google account, create a new project in "my project", name as test, and copy and pasted your script, and then I run script. I put proper google drive folder ID (xxxxx from https://drive.google.com/drive/folders/xxxxx) and then I changed JPEG to JPG. It runs without error, but creates nothing in my google drive. – exsonic01 Feb 18 '20 at 03:06
  • @Tanaike Oh, and I faced "This app isn't verified. This app hasn't been verified by Google yet. Only proceed if you know and trust the developer." message window. What do I need to do with this? – exsonic01 Feb 18 '20 at 03:09
  • @exsonic01 Thank you for replying. When I could correctly understand about your situation and could find the solution. I would like to tell you. – Tanaike Feb 18 '20 at 03:13