【问题标题】:python gspread newbie google spreadsheetpython gspread 新手谷歌电子表格
【发布时间】:2016-12-06 05:19:27
【问题描述】:

我已经能够使用适当的凭据成功连接到 gspread,但是当我尝试打开一个存在的文件(在我的 google 帐户中)时,它失败了。我试过 open() 和 open_by_key()。我假设我的帐户与我刚刚创建的服务帐户不同,但我看不到如何手动创建电子表格以添加(最终由 python 读取)。

提前致谢

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name("MyCreds.json", scope)
gc = gspread.authorize(credentials)
# Does not work (Spreadsheet not found):
wks = gc.open("My Existing Sheet").sheet1
Does not work (Spreadsheet not found):
wks = gc.open_by_url("https://docs.google.com/spreadsheets/d/16H7OZohbLmRoe1zZzb5ek5KZnLQKMGA-ZqyfnBms7OI/edit#gid=0").sheet1
# Does not work (Spreadsheet not found):
wks = gc.open_by_key("16H7OZohbLmRoe1zZzb5ek5KZnLQKMGA-ZqyfnBms7OI").sheet1

提前感谢您的帮助

【问题讨论】:

  • 我知道我要访问的文件确实存在于我的个人帐户中,因为我可以在 Google 表格中访问它。我从我的个人帐户创建了凭据。假设我的凭据是正确的,我该如何/去哪里编辑我可以以编程方式访问的电子表格?
  • 所以我取得了一点进展(我认为)。似乎您需要与设置凭据时获得的 json 文件中的 client_email 共享您的个人工作表。我分享了它,但仍然使用所有三个不同的打开命令得到 SpreadsheetNotFound 异常。顺便说一句,我正在使用从 pip install --upgrade google-api-python-client 获得的最新 API
  • 我可以看到你已经添加了这个问题的答案。您现在可以打开电子表格了吗?
  • 另外,你不需要pip install --upgrade google-api-python-client来升级gspread。该库与 gspread 无关。要获取最新的 gspread,请执行 pip install --upgrade gspread

标签: python gspread


【解决方案1】:

你需要做几件事才能让它发挥作用。

  1. 从 Google 获取 json 对象,记录在 here
  2. 将工作表从您的帐户共享到在您的 json 对象中创建的电子邮件。
  3. wks = gc.open("My Existing Sheet").sheet1 调用中使用电子表格的标题。

【讨论】:

    【解决方案2】:

    如果库没有找到文件,则 id 不存在或凭据错误。 此外,您应该考虑使用官方客户端库及其电子表格 API v4(这个比 v3 好得多):

    https://developers.google.com/api-client-library/python/start/installation

    您还应该检查驱动器 API 是否已启用,但我猜是。

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多