2016-12-06 118 views
1

我已经能够成功连接到gspread与适当的凭据,但当我尝试打开一个existig文件(在我的谷歌帐户)失败。我已经尝试open()以及open_by_key()。我假设我的帐户与刚刚创建的服务帐户不同,但我看不到如何手动创建电子表格以添加(并最终由python读取)。蟒蛇gspread新手谷歌电子表格

在此先感谢

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 

在此先感谢您的帮助

+0

我知道我试图访问的文件确实存在于我的个人帐户中,因为我可以在Google工作表中访问它。我从我的个人帐户创建了凭据。假设我的凭证是正确的,我该如何/在哪里编辑一个电子表格,我可以以编程方式访问? – jordanthompson

+0

所以我取得了一些进展(我认为)。似乎您需要在设置凭据时使用json文件中的client_email共享您的个人表单。我分享了它,但仍然得到了SpreadsheetNotFound异常,并带有所有三种不同的打开命令。顺便说一句,我使用的是从pip安装得到的最新API --upgrade google-api-python-client – jordanthompson

+0

我可以看到你已经为这个问题添加了一个答案。你现在能打开电子表格吗? – Burnash

回答

2

您需要做一些事情才能使其发挥作用。

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