简体   繁体   English

Google Drive API v3将Google电子表格下载为Excel

[英]Google Drive API v3 download google spreadsheet as Excel

I try to download a Google Drive spreadsheet as an Excel file. 我尝试将Google云端硬盘电子表格下载为Excel文件。 As far as I can tell, the Google Drive API should make this very simple by just specifying a different mime type to the export_media request. 据我所知,Google Drive API应该通过为export_media请求指定不同的mime类型来实现这一点

Based on the tutorial script I can successfully download the spreadsheet as CVS and Open Office sheet. 根据教程脚本,我可以成功将电子表格下载为CVS和Open Office表。 Great! 大!

However with the mime type set to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - as specified for MS Excel the downloader continues to read from the stream without ever stopping and without increasing the status progress. 但是,如果将mime类型设置为application/vnd.openxmlformats-officedocument.spreadsheetml.sheet - 正如为MS Excel指定的那样,下载程序将继续从流中读取而不会停止并且不会增加状态进度。

The full script is here . 完整的脚本在这里 To run, follow the instructions to create an app in the Google Drive API docs here 要运行,请按照说明在此处的Google Drive API文档中创建应用

The offending code is here: 违规代码在这里:

    file_id = 'my spreadsheet id'

    request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

    fh = FileIO('data.xls', 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))

It continues to print Download 0% and never stops. 它继续打印Download 0% ,永不停止。

It turns out that the MediaBaseIODownload relies on 'content-range or content-length` being present to know when to stop. 事实证明, MediaBaseIODownload依赖于“内容范围or内容长度”来知道何时停止。

  if 'content-range' in resp:
    content_range = resp['content-range']
    length = content_range.rsplit('/', 1)[1]
    self._total_size = int(length)
  elif 'content-length' in resp:
    self._total_size = int(resp['content-length'])

However, in a debugger I can see, they are not present in the response. 但是,在我可以看到的调试器中,它们不在响应中。 Hence, it cannot not know when being done. 因此,它不知道什么时候完成。

  if self._progress == self._total_size:
    self._done = True

The solution was to not run a partial download but to download in full: 解决方案是不进行部分下载,而是完全下载:

request = service.files().export_media(fileId=file_id, mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
with open('data.xlsx', 'wb') as f:
    f.write(request.execute())  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM