简体   繁体   中英

Python: Reading a google Spreadsheet

I'm not very experienced in Python so please forgive me my possible stupidity. I want to read a Google spreadsheet via python. I'm not the owner of the spreadsheet but the owner shared it with me (read only. no write).

That's what I have right now:

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')

Unfortunately I always get: "gspread.exceptions.SpreadsheetNotFound"

So the login/authorization seems to be working fine but somehow gspread is unable to find the spreadsheet. It doesn't work with open_by_url or open either..

So, is it even possible to read a Google spreadsheet that i don't own?

I'm using python 3.5

If your login is working as expected, then the issue can be the spreadsheet key:

For that, you can send a GET request to the following link:

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

This will return a list of all spreadsheets in either JSOn or XML (whichever one you choose) and then look for the id field that contains the entire url for the spreadsheet you want, and lastly verify that the key you're using is the same as the one returned by the request.

In JSON:

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

In XML:

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

Here's a link to the Sheets API

Note that you have to share your document not with your own account, but with json_key['client_email'] account (which is very long and strange e-mail address). It can be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM