Is it necessary you do this in google app script? If not, you can use google API to get this done. For example, you create an authentication token from Google developer and then use your favourite programming language to make request to google endpoint - this method has no timeout(unlike google app script)
Edit
Per your request. To begin, go here and follow step 1 and step 2 instructions to get the needed credentials and also, to install google client library for python (python 3.8 in my case)
I have broken the source code down into two files (so copy each section of below code and save them in their respected python file - .py
file extension)
credential.py (file name)
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive']
def cred():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
drive = build('drive', 'v2', credentials=creds)
return drive
image_to_doc.py (file name)
"""
Convert image file to doc
"""
from apiclient import errors
from credentials import cred
def retrieve_all_files():
"""Retrieve a list of File resources.
Returns:
List IDs of File resources.
"""
FOLDER_ID = YOUR_FOLDER_ID # Make sure only image files are on this Folder.
result_tmp = []
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = cred().children().list(folderId=FOLDER_ID, **param).execute()
result_tmp.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError as error:
print(f'An error occurred: {error}')
break
result = [r['id'] for r in result_tmp]
return result
def convert(ids):
"""
If you have deleted files(less than 30 days, if you have the feature turned on) on the folder,
they will be included on the files to be converted. To get over this, delete the deleted file(s)
from the trash folder(delete forever)
"""
drive = cred()
try:
for id in ids:
copy_file = drive.files().copy(
fileId=id,
body=None, ocr=True).execute()
# print(copy_file['id'])
except errors.HttpError as error:
print(f'An error occurred: {error}')
def main():
files = retrieve_all_files()
convert(files)
if __name__ == "__main__":
main()
You can then run it with python image_to_doc.py
What this does is, it create a google doc copy (using OCR) of each image in the folder