Upload and convert XLSX to Google Sheets with PyDrive

≯℡__Kan透↙ 提交于 2020-07-21 07:41:32

问题


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:


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.



来源:https://stackoverflow.com/questions/53739061/upload-and-convert-xlsx-to-google-sheets-with-pydrive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!