【问题标题】:Upload and convert XLSX to Google Sheets with PyDrive使用 PyDrive 上传 XLSX 并将其转换为 Google 表格
【发布时间】:2018-12-12 08:40:55
【问题描述】:

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

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

在谷歌documentation

我找到了另一个MimeType

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

但它给了我错误

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

请建议我怎样才能达到理想的结果。

【问题讨论】:

  • 通过 Drive REST API(不是 PyDrive)这是一个非常简单的任务:developers.google.com/drive/api/v3/… PyDrive 可能需要您使用特定的标志或类似的指示符,您希望上传并转换为 G套件文件。
  • 感谢编辑。

标签: python google-api google-sheets-api pydrive


【解决方案1】:

我认为主要问题是 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})

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

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多