3
votes

I am trying to upload a .xlsx file to google drive. I am able to upload it. But when we try to open the same file in Drive, it has to be opened with Google Sheets. Thus, it creates a new file with same name and consumes Drive space.

I suppose I need to change the MimeType while uploading.

What I have tried is :

file = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": FolderID}]
                        ,'title': fileName
                        ,'mimeType':'application/vnd.ms-excel'})
file.SetContentFile('12dec2018.xlsx')
file.Upload()

and I have also tried this one

'mimeType':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

At Google documentation

I have found the other MimeType

'mimeType':'application/vnd.google-apps.spreadsheet'

But it gives me Error

ApiRequestError: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json returned "Invalid mime type provided">

Please suggest how could I achieve the desirable result.

1
Via the Drive REST API (not PyDrive) this is a very simple task: developers.google.com/drive/api/v3/… PyDrive may require you to use a specific flag or similar indicator that you wish to both upload and convert to a G Suite document.tehhowch
Thanks for the edit.LMSharma

1 Answers

5
votes

I think the main question will be does pydrive use Google drive v2 or Google drive v3. My digging in the source seams to point to v2 which means when creating the file you need to send convert=true for the file to be converted at the time of upload files.inesrt

# {'convert': True} triggers conversion to a Google Drive document.
file.Upload({'convert': True})

By sending convert when the file is uploaded it will automatically be converted to a google docs type.