Python:阅读谷歌电子表格

时间:2015-12-21 16:01:01

标签: python

我对Python不是很有经验所以请原谅我可能的愚蠢。 我想通过python阅读Google电子表格。我不是电子表格的所有者,但是所有者与我共享(只读。不写)。

这就是我现在所拥有的:

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials

json_key = json.load(open('MyProject-ab2f3b9e6d60.json'))
scope = ['https://spreadsheets.google.com/feeds']
credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'].encode(), scope)
client = gspread.authorize(credentials)
client.login()
client.open_by_key('11Z_6NMLa0e3QDNozj6kExij8TG6bWRsxPT_ZppV13w')

不幸的是我总是得到:“gspread.exceptions.SpreadsheetNotFound”

所以登录/授权似乎工作正常,但某种程度上gspread无法找到电子表格。它不适用于open_by_url或打开..

那么,甚至可以阅读我不拥有的Google电子表格吗?

我正在使用python 3.5

2 个答案:

答案 0 :(得分:0)

如果您的登录按预期工作,则问题可能是电子表格密钥:

为此,您可以向以下链接发送GET请求:

https://spreadsheets.google.com/feeds/spreadsheets/private/full

这将返回JSOn或XML(您选择的任何一个)中的所有电子表格的列表,然后查找包含所需电子表格的完整网址的ID字段,最后验证您的密钥是否为'重新使用与请求返回的相同。

在JSON中:

entry: {
    id: https://spreadsheets.google.com/feeds/spreadsheets/(this-is-where-the-key-will-be)
    ...
}

在XML中:

<entry gd:etag=... >
    <id> https://spreadsheets.google.com/feeds/spreadsheets/(this-is-where-the-key-will-be) </id>
    ...
</entry>

这是指向Sheets API

的链接

答案 1 :(得分:0)

请注意,您必须使用json_key['client_email']帐户(这是一个非常长且奇怪的电子邮件地址)与您自己的帐户共享您的文档。这可能是个问题。