Search code examples
pythonfile-uploadgoogle-drive-apigoogle-docs-api

Google Drive API write a new drive file thats a not a Drive 'viewer' type


I'm getting going with GooG Drive API. I've successfully stepped through the quickstart.py tutorial/example. All good.

The file that appears in my Drive opens as a Drive 'viewer' file, rather than an editable file, which is what i need. i'm guessing its the MIMEtype, but the document IS a .txt file, so i'm not sure what else it should be set to. so how do i write it as a Drive editable file type ???

thanks

Dav-o

media_body = MediaFileUpload(FILENAME, mimetype='text/plain', resumable=True)
body = {
  'title': 'My document',
  'description': 'A test document',
  'mimeType': 'text/plain'
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()
pprint.pprint(file)

Solution

  • You are uploading the file as a txt, which is a format not editable with any of the Google editors.

    You should convert the file into the corresponding native Google format to make it editable in the Drive default viewer:

    https://developers.google.com/drive/v2/reference/files/insert

    To enable conversion during upload, you have to set convert=True in the insert request:

    file = drive_service.files().insert(body=body, media_body=media_body, convert=True).execute()