使用 PyDrive 上传 XLSX 并将其转换为 Google 表格

Upload and convert XLSX to Google Sheets with PyDrive

我正在尝试将 .xlsx 文件上传到 google 驱动器。我可以上传它。 但是当我们试图在 Drive 中打开同一个文件时,它必须用 Google Sheets 打开。因此,它会创建一个同名的新文件并使用 Drive space.

我想我需要在上传时更改 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'

在Googledocumentation

我找到了另一个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 驱动器 v2 还是 Google 驱动器 v3。我在源接缝中挖掘指向 v2,这意味着在创建文件时,您需要发送 convert=true 以便在上传时转换文件 files.inesrt

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

通过在上传文件时发送转换,它会自动转换为 google 文档类型。