title: Editing Google docs with drive API
body: <p>(For clarity, this post relates to the difference between the Google <a href="https://developers.google.com/google-apps/documents-list/" rel="noreferrer">Documents List API</a> and <a href="https://developers.google.com/drive/" rel="noreferrer">Google Drive API</a> on <a href="https://developers.google.com/appengine/" rel="noreferrer">Google App Engine</a> with Python)</p>
  
  <p>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. </p>
  
  <p>Have come up with this ...</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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?</p>
  
  <p>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?</p>
  
  <p>And one last thing - the <a href="https://developers.google.com/drive/examples/python#updating_files" rel="noreferrer">sample code in the docs</a> uses a MediaInMemoryUpload object, however <a href="http://code.google.com/p/google-api-python-client/source/browse/apiclient/http.py#441" rel="noreferrer">if you look at the source</a> it is now deprecated, in favour of MediaIoBaseUpload. Should the sample code be updated?!</p>