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

LMS*_*rma 4 python google-api pydrive google-sheets-api

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

我想我需要在上传时更改 MimeType。

我尝试过的是:

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

我也试过这个

'mimeType':'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
Run Code Online (Sandbox Code Playgroud)

在 Google文档中

我找到了另一个 MimeType

'mimeType':'application/vnd.google-apps.spreadsheet'
Run Code Online (Sandbox Code Playgroud)

但它给了我错误

ApiRequestError:https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&alt=json 返回“提供的 MIME 类型无效”>

请建议我如何才能达到理想的结果。

DaI*_*mTo 6

我认为主要问题是 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})
Run Code Online (Sandbox Code Playgroud)

通过在文件上传时发送 convert 它将自动转换为 google docs 类型。