【发布时间】: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。