使用 Python 从 Google Drive / Workspace 下载电子表格
您能否制作一个 Python 示例,说明如何在给定密钥和工作表 ID (gid
) 的情况下下载 Google Sheets 电子表格?我不能。
我已经搜索了 API 的版本 1、2 和 3。我运气不好,我无法弄清楚他们复杂的类似 ATOM 的提要 API,gdata.docs.service.DocsService._DownloadFile
私有方法说我未经授权,而且我不知道我不想自己编写整个 Google 登录身份验证系统。由于沮丧,我快要捅自己的脸了。
我有一些电子表格,我想像这样访问它们:
username = '[email protected]'
password = getpass.getpass()
def get_spreadsheet(key, gid=0):
... (help!) ...
for row in get_spreadsheet('5a3c7f7dcee4b4f'):
cell1, cell2, cell3 = row
...
请保重我的面子。
更新 1: 我已尝试以下操作,但 Download()
或 Export()
的组合似乎不起作用。 (DocsService
的文档 这里)
import gdata.docs.service
import getpass
import os
import tempfile
import csv
def get_csv(file_path):
return csv.reader(file(file_path).readlines())
def get_spreadsheet(key, gid=0):
gd_client = gdata.docs.service.DocsService()
gd_client.email = '[email protected]'
gd_client.password = getpass.getpass()
gd_client.ssl = False
gd_client.source = "My Fancy Spreadsheet Downloader"
gd_client.ProgrammaticLogin()
file_path = tempfile.mktemp(suffix='.csv')
uri = 'http://docs.google.com/feeds/documents/private/full/%s' % key
try:
entry = gd_client.GetDocumentListEntry(uri)
# XXXX - The following dies with RequestError "Unauthorized"
gd_client.Download(entry, file_path)
return get_csv(file_path)
finally:
try:
os.remove(file_path)
except OSError:
pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我正在使用这个:
卷曲'https:// docs.google.com/spreadsheets/d/1-lqLuYJyHAKix-T8NR8wV8ZUUbVOJrZTysccid2-ycs/gviz/tq?tqx=out:csv' 在设置为公开可读的工作表上。
因此,如果您可以使用公共工作表,您将需要 python 版本的curl。
如果您的工作表包含一些您不想显示的选项卡,请创建一个新工作表,并将要发布的范围导入到其上的选项卡中。
I'm using this:
curl 'https://docs.google.com/spreadsheets/d/1-lqLuYJyHAKix-T8NR8wV8ZUUbVOJrZTysccid2-ycs/gviz/tq?tqx=out:csv' on a sheet that is set to publicly readable.
So you would need a python version of curl, if you can work with public sheets.
If you have a sheet with some tabs you don't want to reveal, create a new sheet, and import the ranges you want to publish into tabs on it.
使用表格从谷歌文档下载电子表格非常简单。
上的详细文档进行操作
您可以按照https://pypi.org/project/gsheets/
或按照下面给出的步骤。 我建议阅读文档以获得更好的覆盖范围。
pip install gsheets
使用您要访问其电子表格的 Google 帐户登录 Google 开发者控制台。创建(或选择)一个项目并启用 Drive API 和 Sheets API(在 Google Apps API 下)。
转到您的项目的凭据并创建新凭据> OAuth 客户端 ID >其他类型。在 OAuth 2.0 客户端 ID 列表中,单击刚刚创建的客户端 ID 的“下载 JSON”。将文件保存为您的主目录(用户目录)中的 client_secrets.json。
使用以下代码片段。
Downloading a spreadsheet from google doc is pretty simple using sheets.
You can follow the detailed documentation on
https://pypi.org/project/gsheets/
or follow the below-given steps. I recommend reading through the documentation for better coverage.
pip install gsheets
Log in to the Google Developers Console with the Google account whose spreadsheets you want to access. Create (or select) a project and enable the Drive API and Sheets API (under Google Apps APIs).
Go to the Credentials for your project and create New credentials > OAuth client ID > of type Other. In the list of your OAuth 2.0 client IDs click Download JSON for the Client ID you just created. Save the file as client_secrets.json in your home directory (user directory).
Use the following code snippet.
这不是一个完整的答案,但 Andreas Kahler 使用 Google Docs + Google App Engline + 编写了一个有趣的 CMS 解决方案Python。由于没有该领域的任何经验,我无法确切地看出代码的哪一部分可能对您有用,但请检查一下。我知道它与 Google Docs 帐户交互并处理文件,所以我有一种感觉,您会意识到发生了什么。它至少应该为您指明正确的方向。
Google AppEngine + Google 文档 + 一些 Python = 简单 CMS< /a>
This isn't a complete answer, but Andreas Kahler wrote up an interesting CMS solution using Google Docs + Google App Engline + Python. Not having any experience in the area, I cannot see exactly what portion of the code may be of use to you, but check it out. I know it interfaces with a Google Docs account and plays with files, so I have a feeling you'll recognize what's going on. It should at least point you in the right direction.
Google AppEngine + Google Docs + Some Python = Simple CMS
Gspread 确实比 GoogleCL 和 Gdata(这两个我都使用过,谢天谢地已经被 Gspread 淘汰)有了很大的改进。我认为这段代码比之前的答案更快地获取工作表的内容:
Gspread is indeed a big improvement over GoogleCL and Gdata (both of which I've used and thankfully phased out in favor of Gspread). I think that this code is even quicker than the earlier answer to get the contents of the sheet:
(2019 年 3 月,Python 3)我的数据通常不敏感,我通常使用类似于 CSV 的表格格式。
在这种情况下,人们可以简单地将工作表
发布到网络
,然后将其用作服务器上的 CSV 文件。(使用
File
->Publish to the web ...
->Sheet 1
->逗号分隔来发布它值 (.csv) ->
发布
)。(Mar 2019, Python 3) My data is usually not sensitive and I use usually table format similar to CSV.
In such case, one can simply
publish to the web
the sheet and than use it as a CSV file on a server.(One publishes it using
File
->Publish to the web ...
->Sheet 1
->Comma separated values (.csv)
->Publish
).https://github.com/burnash/gspread 库是一种更新、更简单的与 Google 互动的方式电子表格,而不是建议使用 gdata 库的旧答案,它不仅级别太低,而且过于复杂。
您还需要创建并下载(JSON 格式)服务帐户密钥:https:// /console.developers.google.com/apis/credentials/serviceaccountkey
以下是如何使用它的示例:
The https://github.com/burnash/gspread library is a newer, simpler way to interact with Google Spreadsheets, rather than the old answers to this that suggest the
gdata
library which is not only too low-level, but is also overly-complicated.You will also need to create and download (in JSON format) a Service Account key: https://console.developers.google.com/apis/credentials/serviceaccountkey
Here's an example of how to use it:
如果有人遇到此问题并寻求快速解决方案,这里有另一个(当前)有效的解决方案,它不依赖于 gdata 客户端图书馆:
In case anyone comes across this looking for a quick fix, here's another (currently) working solution that doesn't rely on the gdata client library:
您可以尝试使用导出电子表格 文档部分。
为电子表格服务获取单独的登录令牌,并将其替换为导出。将其添加到
get_spreadsheet
代码中对我有用:请注意,我还使用了
Export
,因为Download
似乎只提供 PDF 文件。You might try using the AuthSub method described in the Exporting Spreadsheets section of the documentation.
Get a separate login token for the spreadsheets service and substitue that for the export. Adding this to the
get_spreadsheet
code worked for me:Notice I also used
Export
, asDownload
seems to give only PDF files.(2016 年 7 月) 所有其他答案都已经过时或将会过时,因为它们使用 GData(“Google 数据”)协议,ClientLogin,或 AuthSub,全部已被弃用。对于使用 Google Sheets API v3 或更早版本的所有代码或库也是如此。
现代 Google API 访问使用 API 密钥(用于访问公共数据)、OAuth2 客户端 ID(用于访问用户拥有的数据)或服务帐户(用于访问应用程序/云中拥有的数据)进行,主要使用 适用于 GCP API 和 适用于非 GCP API 的 Google API 客户端库。对于此任务,Python 的后者。
为了实现这一点,您的代码需要获得 Google Drive API 的授权访问权限,也许是为了查询特定的表格下载,然后执行实际导出。由于这可能是常见操作,因此我编写了 博客文章分享了一个可以为您完成此操作的代码片段。如果您想进一步了解这一点,我还有另一对 帖子以及一个视频,概述了如何将文件上传到 Google 云端硬盘以及如何从 Google 云端硬盘下载文件。
请注意,还有一个 Google Sheets API v4,但它主要用于面向电子表格的操作,即插入数据、读取电子表格行、单元格格式、创建图表、添加数据透视表等,而不是基于文件的请求,例如导出,其中 Drive API 是正确的使用。
我写了一篇博文来演示从云端硬盘将 Google 表格导出为 CSV。脚本的核心部分:
要了解有关将 Google Sheets 与 Python 结合使用的更多信息,请参阅我对类似问题的回答。您还可以下载 XLSX 和其他云端硬盘支持的格式的工作表。
如果您对 Google API 完全陌生,那么您需要先退一步并查看这些视频:
如果您已经拥有 Google Workspace(以前称为 G Suite、Google Apps、Google“Docs”)API 的使用经验,并且希望观看有关使用这两个 API 的更多视频:
(Jul 2016) All other answers are pretty much outdated or will be, either because they use GData ("Google Data") Protocol, ClientLogin, or AuthSub, all of which have been deprecated. The same is true for all code or libraries that use the Google Sheets API v3 or older.
Modern Google API access occurs using API keys (for accessing public data), OAuth2 client IDs (for accessing data owned by users), or service accounts (for accessing data owned by applications/in the cloud) primarily with the Google Cloud client libraries for GCP APIs and Google APIs Client Libraries for non-GCP APIs. For this task, it would be the latter for Python.
To make it happen your code needs authorized access to the Google Drive API, perhaps to query for specific Sheets to download, and then to perform the actual export(s). Since this is likely a common operation, I wrote a blogpost sharing a code snippet that does this for you. If you wish to pursue this even more, I've got another pair of posts along with a video that outlines how to upload files to and download files from Google Drive.
Note that there is also a Google Sheets API v4, but it's primarily for spreadsheet-oriented operations, i.e., inserting data, reading spreadsheet rows, cell formatting, creating charts, adding pivot tables, etc., not file-based request like exporting where the Drive API is the correct one to use.
I wrote a blog post that demos exporting a Google Sheet as CSV from Drive. The core part of the script:
To learn more about using Google Sheets with Python, see my answer for a similar question. You can also download a Sheet in XLSX and other formats supported by Drive.
If you're completely new to Google APIs, then you need to take a further step back and review these videos first:
If you already have experience with Google Workspace (formerly G Suite, Google Apps, Google "Docs") APIs and want to see more videos on using both APIs:
从 gdata 2.0.1.4 开始,这不再有效:
相反,您必须执行以下操作:
This no longer works as of gdata 2.0.1.4:
Instead, you have to do:
我编写了 pygsheets 作为 gspread 的替代品,但使用了 google api v4。它有一个
export
方法来导出电子表格。I wrote pygsheets as an alternative to gspread, but using google api v4. It has an
export
method to export spreadsheet.以下代码适用于我的情况(Ubuntu 10.4,python 2.6.5 gdata 2.0.14)
The following code works in my case (Ubuntu 10.4, python 2.6.5 gdata 2.0.14)
我通过删除不必要的面向对象进一步简化了@Cameron 的答案。这使得代码更小并且更容易理解。我还编辑了网址,这可能会更好。
I've simplified @Cameron's answer even further, by removing the unnecessary object orientation. This makes the code smaller and easier to understand. I also edited the url, which might work better.