Modifying Google Documents using the Drive API

Help with Google Drive API for Doc Editing

I’m trying to figure out how to edit Google Docs using the Drive API v2. I used to do this with the old Documents List API by exporting as HTML, changing it, and reuploading. But now I can’t seem to do the same thing with the new API.

Here’s what I’ve tried:

drive_service = build('drive', 'v2', http=authenticated_http)

# Get file and download HTML
file = drive_service.files().get(fileId=MY_FILE_ID).execute()
html_url = file['exportLinks']['text/html']
_, content = authenticated_http.request(html_url)

# Edit HTML
edited_html = content.replace('old_text', 'new_text')

# Try to upload modified HTML
new_file = {
    'title': 'Changed Document',
    'mimeType': 'text/html'
}
media = MediaIoBaseUpload(StringIO.StringIO(edited_html), 'text/html')
drive_service.files().insert(body=new_file, media_body=media)

This uploads an HTML file to Drive, but doesn’t create a Google Doc. How can I make it into a proper Google Doc like before?

Also, I’m having trouble with resumable uploads on App Engine. Any tips?

I’ve wrestled with the Drive API for doc editing too, and it can be tricky. From my experience, the key is to use the ‘application/vnd.google-apps.document’ MIME type when uploading. Here’s a snippet that worked for me:

new_file = {
    'title': 'Changed Document',
    'mimeType': 'application/vnd.google-apps.document'
}
media = MediaIoBaseUpload(StringIO.StringIO(edited_html), 'text/html', resumable=True)
file = drive_service.files().insert(body=new_file, media_body=media, convert=True).execute()

The ‘convert=True’ parameter is crucial - it tells Drive to convert the uploaded HTML to a Google Doc.

For resumable uploads on App Engine, I found using the googleapiclient.http.MediaIoBaseUpload with chunked transfer encoding helps. Set a reasonable chunk size (like 262144 bytes) and make sure you’re handling potential timeouts and retries.

Hope this helps! Let me know if you need any clarification.

hey, i’ve had some luck with this. try using the docs api instead of drive api for editing. it’s way more precise. you can do stuff like:

docs_service = build(‘docs’, ‘v1’, credentials=creds)
requests = [
{‘insertText’: {‘location’: {‘index’: 1}, ‘text’: ‘new text’}}
]
docs_service.documents().batchUpdate(documentId=DOC_ID, body={‘requests’: requests}).execute()

this lets u make specific changes. hope it helps!

I’ve encountered similar issues when working with the Drive API for document editing. One approach that’s worked well for me is using the Google Docs API in conjunction with the Drive API. The Docs API provides more granular control over document content and formatting.

Here’s a basic workflow I’ve used:

  1. Use the Drive API to create a new Google Doc or get an existing one.
  2. Use the Docs API to make specific changes to the document content.
  3. Optionally, use the Drive API again for file management tasks.

This method allows for more precise editing and avoids the HTML conversion issues. It does require setting up the Docs API separately, but it’s worth it for the improved functionality.

Regarding resumable uploads on App Engine, I’ve found that using smaller chunk sizes and implementing exponential backoff for retries helps mitigate timeout issues.