Question

(For clarity, this post relates to the difference between the Google Documents List API and Google Drive API on Google App Engine with Python)

With the [now deprecated] Documents list API I was able to edit Google Documents by exporting as HTML, modifying the HTML and then re-uploading, either as a new document or as a modification to the original. This was useful for things like generating PDF documents from a template. I have been trying to replicate this functionality with the new Drive API (V2), however seem unable to.

Have come up with this ...

http = # authenticated http client
drive_service = build('drive', 'v2', http=http)

# get the file ...
file = drive_service.files().get(fileId=FILE_ID).execute()

# download the html ...
url = file['exportLinks']['text/html']
response, content = http.request(url)

# edit html
html = content.replace('foo', 'bar')

# create new file with modified html ...
body = {'title':'Modified Doc', 
        'description':'Some description', 
        'mimeType':'text/html'}
media_body = MediaIoBaseUpload(StringIO.StringIO(html), 'text/html', resumable=False)
drive_service.files().insert(body=body, media_body=media_body)

The above code uploads an html file as a file into Google Drive, rather then rendering the HTML into a Google Document. Fair enough, this makes sense. But how to I get it render as a Google Doc, as I was able to do with the Documents List API?

One other thing - if I set resumable=True it throws the following error on App Engine - '_StreamSlice' has no len(). Couldn't figure out how to get resumable=True to work?

And one last thing - the sample code in the docs uses a MediaInMemoryUpload object, however if you look at the source it is now deprecated, in favour of MediaIoBaseUpload. Should the sample code be updated?!

Was it helpful?

Solution

i suspect the issue is that the default for conversion has changed from true to false. You must explicitly set convert=true on the upload. See https://developers.google.com/drive/v2/reference/files/insert

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top