我正在尝试将 .xlsx 文件上传到 Google 云端硬盘。我可以上传它。 但是,当我们尝试在云端硬盘中打开同一文件时,必须使用 Google 表格打开它。因此,它会创建一个同名的新文件并消耗驱动器空间。
我想我需要在上传时更改 MimeType。
我尝试过的是:
file = drive.CreateFile({"parents": [{"kind": "drive#fileLink", "id": FolderID}]
,'title': fileName
,'mimeType':'application/vnd.ms-excel'})
file.SetContentFile('12dec2018.xlsx')
file.Upload()
我也尝试过这个
'mimeType':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
在 Google documentation
我找到了其他MimeType
'mimeType':'application/vnd.google-apps.spreadsheet'
但是它给了我错误
ApiRequestError
: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json returned "Invalid mime type provided">
请建议我如何才能达到理想的结果。
最佳答案
我认为主要问题是 pydrive 使用 Google Drive v2 还是 Google Drive v3。我在源代码中的挖掘指向 v2,这意味着在创建文件时,您需要发送 convert=true
以便在上传时转换文件 files.inesrt
# {'convert': True} triggers conversion to a Google Drive document.
file.Upload({'convert': True})
通过在文件上传时发送转换,它将自动转换为 Google 文档类型。
关于python - 使用 PyDrive 上传 XLSX 并将其转换为 Google Sheets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53739061/