1

Can a Google Apps script be written to convert 1000s of PDFs to Google Docs?

Currently you have to open individual PDFs one-by-one "Open with Google Docs" to perform this transformation.

This is my first time working with Google Apps script (Google Doc > Tools > Script editor)

I have attempted to use https://github.com/odeke-em/drive but not having success yet.

Syncdocs https://www.syncdocs.com/forums/topic/pdf-conversion-issues#post-10464 is paid 3rd party tool that might work, but looking to do this for free if possible.

http://patt0.blogspot.com/2014/08/continuous-batch-library-update-for.html has some ideas but I have not been able to understand how to implement them.

keep getting an error

Missing ; before statement. (line 1, file "Code")Dismiss"

when entering any code

2
  • 1
    Welcome. Please add more details about your attempt your of using Google Apps ScrIpt,If you got an error message , add the textual error message
    – Rubén
    Dec 8, 2019 at 21:03
  • 1
    Hi sirgeo, Google Apps Script is a JavaScript implementation, so you can't write Bash scripts here. Dec 12, 2019 at 9:55

3 Answers 3

1

Google Apps Script can't run code like

drive init ~/gdrive
cd ~/gdrive

Google Apps Script only can run JavaScript code with some limitations, like it doesn't support by default let statements, promises among other.

Note: Converting files from PDF to Google Documents isn't an straightforward tasks because not all the PDF features could be converted

From Convert PDF to Doc with Google Script Editor

function pdfToDoc() {  
  var fileBlob = DriveApp.getFileById('0B3m2D6239t6aWHo5TVpyYzhxV1U').getBlob();  
  var resource = {
    title: fileBlob.getName(),
    mimeType: fileBlob.getContentType()
  };
  var options = {
    ocr: true
  };
  var docFile = Drive.Files.insert(resource, fileBlob, options);  
  Logger.log(docFile.alternateLink);  
}

Add Drive API to google apps script project https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services

You can see you pdf file id on google drive when you download it http://i.imgur.com/3pYvwjx.png

NOTE: I didn't tested the above code yet.

References

Related

3
  • getting an error "We're sorry, a server error occurred. Please wait a bit and try again. (line 2, file "Code")Dismiss" i.imgur.com/YhVxiy4.png
    – sirgeo
    Dec 9, 2019 at 3:54
  • @sirgeo I suggest you to post a new question including more details that describe how you adapted the to code including how do you get the file ID, if you are the owner of if it was shared with you, etc.
    – Rubén
    Dec 9, 2019 at 4:16
  • @ Rubén i copy / pasted that censored portion from the url of my google drive guessing that was the unique key required in that part of the script... was that not correct?
    – sirgeo
    Dec 9, 2019 at 4:28
0

I have solution with odeke-em's drive tool you mentioned.

Keep in mind that this tool is a CLI (Command Line Interface) tool and has no connection with Google Apps Script. This tool also will probably not work on Windows.

Get OAuth2 Credentials

drive init ~/gdrive
cd ~/gdrive

drive init will give you a link with the OAuth2 authorization code. Enter the authorization code in the prompt.

Create a folder inside the gdrive folder and put your pdf files in this folder.

Execute drive push -convert YourFolderName

This will recursively go through the folder and convert every file to its native Google Docs format.

source: https://github.com/odeke-em/drive

6
  • keep getting an error "Missing ; before statement. (line 1, file "Code")Dismiss"
    – sirgeo
    Dec 9, 2019 at 0:58
  • 1
    drive is a cli (Command Line Interface) tool. drive should be executed inside a terminal. drive has no connection with google apps script. Like @Rubén said, you can only use Javascript with Google Apps Script.
    – 5eb
    Dec 9, 2019 at 8:06
  • in Windows 7 "Windows Command Processor" neither drive init ~/gdrive cd ~/gdrive iare recognized... is odeke-em's tool not Windows-friendly or am i missing something else?
    – sirgeo
    Dec 9, 2019 at 12:51
  • 1
    You need to install the tool first. The installation instruction are described on the github page you linked, but the github page says that it probably will not work on Windows.
    – 5eb
    Dec 9, 2019 at 12:57
  • It does appear to be built for Linux users.... It seems Google purposefully designed G Drive to not allow batch processing their "Open in Google Docs" OCR tool without an enormous amount of work-around, like odeke-em's tool..... the question is why?
    – sirgeo
    Dec 9, 2019 at 13:56
0

I got something to work here:

https://stackoverflow.com/questions/53687444/batch-convert-jpg-to-google-docs

Batch convert PDF to Google Docs

SOURCE AND DESTIN: https://drive.google.com/drive/u/0/folders/123456789

Function convertPDFtoGoogleDocs() {
var srcfolderId = "123456789"; // <--- 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.PDF); // Modified
while (files.hasNext()) {
var file = files.next();
Drive.Files.insert({title: file.getName(), parents: [{id: dstfolderId}]},     (),     {ocr: true}); // Modified
}
}

i needed to also use the following link to solve another error that initially pops up using this script:

https://stackoverflow.com/questions/43838960/referenceerror-drive-is-not-defined-line-16-file-code-in-google-app-scr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.