gspread.exceptions.SpreadsheetNotFound

gspread.exceptions.SpreadsheetNotFound

我正在编写一个 python(ver 3) 脚本来使用 gspread 访问 google 文档。

  1)  import gspread
  2)  from oauth2client.service_account import ServiceAccountCredentials
  3)  scope = ['https://spreadsheets.google.com/feeds']
  4)  credentials = ServiceAccountCredentials.from_json_keyfile_name(r'/path/to/jason/file/xxxxxx.json',scope)
  5)  gc = gspread.authorize(credentials)
  6)  wks = gc.open("test").sheet1

test 是一个 google sheet ,它似乎打开并阅读正常,但如果我尝试从 Office excel 文件读取它给我 error.here 他们看起来是什么:

test 和 mtg 所在的文件夹与我在 json 中收到的电子邮件共享 file.Also 两个文件都与该电子邮件共享。

尝试过:

wks = gc.open("mtg.xls").sheet1

wks = gc.open("mtg.xls").<NameOfFirstSheet>

wks = gc.open("mtg").<NameOfFirstSheet> 

错误:

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/gspread/client.py", line 152, in open raise SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound

没有在文件名末尾添加.xls,数据以不同的格式保存(以后可以导出为.xls)。

尝试将您的代码分解为:

ss = open("MTG_Collection_5_14_16") 
ws = ss.worksheet("<NameOfFirstSheet>")

和 post 错误消息(如果有)。

Spreadsheet 个实例有一个属性 sheet1,因为它是第一个工作表的默认名称。 ss.sheet1 实际上 returns 索引为 0 的工作表,无论其名称是什么。

如果您想访问另一个工作表,您需要使用 ss.worsheet("<title>")ss.get_worksheet(<index>) 之一。 ss.<NameOfFirstSheet> 将不起作用。

我得到这个是因为我错过了第 4 步 here

Go to your spreadsheet and share it with a client_email from the step above. Otherwise you’ll get a SpreadsheetNotFound exception when trying to access this spreadsheet with gspread.

这里有一个非常重要的说明:[https://docs.gspread.org/en/latest/oauth2.html#enable-api-access]

Very important! Go to your spreadsheet and share it with a client_email from the step above. Just like you do with any other Google account.
If you don’t do this, you’ll get a gspread.exceptions.SpreadsheetNotFound exception when trying to access this spreadsheet from your application or a script.

这意味着您需要共享电子表格(通过打开电子表格然后单击共享按钮,然后只需粘贴电子邮件 ID)"client_email" 邮件 ID,它在您从 google 开发人员控制台

获得的 JSON 文件中提供

例如:

{
  "type": "service_account",
  "project_id": "speeqedy-octane-317516",
  "private_key_id": "8ct0ad00e3e59d49a576012cb515ea89e4e49cc4",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCOlC07MwSYGr54\nMx1+XAXC2k8HrBA/W6I3PZxnG5CUTPL8rJy8Ne2kuXW2k3qN/A0CKf2yUKc0DbIP\nn+LsCDYt6ikTOpb5VeToRMFPlg3dwcghsVFlO2RKVZc1NiWYDiUu5jqjy/WOpqNH\nMZbPDJchhApiyEb+brSpgag0Smpn8oWNBFNUqyN7n+dQMDxlwe9CniM6p19rhwyX\nPPwYcONb/FQffIRrjUlSbxAV+uTwoN/BeTN/jDvjDSdbF2jfBzis/sBejZAt3wsA\nzM2KcNu/bGFck+Slg0rXbHf8qPW4C+kuGe4x5KI4y/SG77TuuflNfeIIadwVblAH\nruhlzGRvAgMBAAECggEAQ7+rIncilrgWi30WZaWKY0Viyi17Zu++y+rtt5zEfYN2\n7pOCwngJnP03LvMzB+ads8qKL0HdwYFEe+IobKmWPQkITYfIl1+2LpwFEzElE5tx\n4nikSasIA75Z5EH/aRpHltp1QMG4w/jwmvK4S1gbNCTO3pN2EI5AME9YwsgdeZhx\nFPbTlZ7CstqrvJwrejInuX7b7+YLCRUiLz09n5xDVLCpgzqcUxRUIlX6qnQ5O5fQ\nUmVI4qeeIMb3TE2X6JN2Hon5RCqA1j6kNj3auIgDgrqCarcjX4MU9hDlBfj+MRKM\nZz19lNCYBL7D2p9o+QJMzBakmUawdEdm0PNnzouxGQKBgQDIHE6H3u84YzqWsyAm\nJoON9jWq3Z0Dp9Woxd1c12sJGaqpCjvejAHsUklRDLSnj/9TxySxCBtHzOkpWZ27\nC1+ZQVkHgVkJaBRJ2dxwsNODDyvn12gN2OE00e0IATx6hIuMrZBiisrOldlzkfbe\nbrzdpcverz/j8O+DU+xlpkd+bQKBgQC2ZmkO+P2fofc4AbnLNKqr8e9NVqMAHovi\nj6dqE1/YQwarXyORHlpqZy0cqG1yXHiTXdQCkRf2oaLtUMoNARywCNNsJcmTUccm\nbG1/g3KdcsTu5r7/IgJjJ6Oytd5WE3nOOG0pN0PxTVjhC5GIMdDqT7bO7PxYxt8J\ny5stQwE0ywKBgEjUZJB2xsHS+4PVdaHs6nxEW4vwVDD8aHqgaMxdaotJWB9lphx9\nAEYuPJjO8wD5G4g2NlhoBuW+4i02fH2SyhxQ+EIPBYSy6I9oRx6MfK56WxRqBWjk\nwjIjJQ2Axa4hh1iP4ri6os1FJHs3aI3T59ETgnttdOdUzRcS+DqWpfaRAoGADnjx\nvQ5kkVX4l6kLZOQ45ou7wRcut8O0tbVQQ0pmQHKRBOf3mnweEHM/URbMGFbd+lNn\njwyUKi2JD+tpHL8J163bmKkm5VmHw7S2u3dikLuahJoqbXP1XIi4pGVpzsqcz5AK\nmUqZSXwpoTGZyoSpXu4UQzbqjuC4sb5GAcFfRyECgYEAqPEqRTfjU48q8X5ZNx/D\nGze5ZU8PpkOr9viffcptt7dF9f/SMyqY6a8oDzm70tN8IHsctoadu/9ZqUAC56XO\nZtc96uppNBHM/ZZr2eXR6iU8fXTkHgA009xYeOIciS/exvlntTn7qeBiLb5DyA2s\nHB/iyGpo6KQBl/rRtcNxQXw=\n-----END PRIVATE KEY-----\n",
  "client_email": "cybor@speedy-octane-313216.iam.gserviceaccount.com",
  "client_id": "102481923285078876543",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/cybor%40speedy-octane-317516.iam.gserviceaccount.com"
}

如果这是您的 JSON 文件,那么您需要复制 client_emailcybor@speedy-octane-313216.iam.gserviceaccount.com 然后将您的工作表分享到此电子邮件 ID。