I’m working with Google Drive API v2 and trying to update Google Documents programmatically. Previously I used the old Documents List API where I could export a doc as HTML, make changes to the content, and upload it back as a Google Document.
Now with the Drive API I’m having trouble getting the same result. Here’s what I’m trying:
auth_http = # my authenticated http client
api_service = build('drive', 'v2', http=auth_http)
# fetch the document
document = api_service.files().get(fileId=DOC_ID).execute()
# get html export
export_url = document['exportLinks']['text/html']
resp, html_content = auth_http.request(export_url)
# modify content
modified_html = html_content.replace('old_text', 'new_text')
# upload modified version
file_metadata = {
'title': 'Updated Document',
'description': 'Modified version',
'mimeType': 'text/html'
}
upload_media = MediaIoBaseUpload(StringIO.StringIO(modified_html), 'text/html', resumable=False)
api_service.files().insert(body=file_metadata, media_body=upload_media)
The problem is this creates a regular HTML file in Drive instead of converting it to a proper Google Doc format. How can I make the API treat the uploaded HTML as a Google Document like the old API did?
Also having issues with resumable uploads on App Engine - getting ‘_StreamSlice’ has no len() error when resumable=True.