8
votes

I am writing a python(ver 3) script to access google doc using gspread.

  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 is a google sheet which seems to be opened and read fine but if I try to read from a Office excel file it gives me error.here is what they look: enter image description here

The folder which test and mtg are under is shared with the email I got in json file.Also both files were shared with that email.

Tried:

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

and

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

and

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

error:

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

3

3 Answers

5
votes

There is no .xls to be added at the end of the file name, the data is saved in a different format (and can later be exported as .xls).

Try to break your code into:

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

and post the error message if any.

Spreadsheet instances have an attribute sheet1 because it is the default name for the first worksheet. ss.sheet1 actually returns the worksheet with index 0, no matter what its name is.

If you want to access another worksheet, you need to use one of ss.worsheet("<title>") or ss.get_worksheet(<index>). ss.<NameOfFirstSheet> will not work.

16
votes

I was getting this as I had missed step 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.

1
votes

A Very Important Note from here : [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.

This means that you need to share the spreadsheet (by opening the spreadsheet and then clicking on the share button and then just paste the email id) with the "client_email" mail id which is provided in the JSON file that you got from google developer console

eg:

{
  "type": "service_account",
  "project_id": "speeqedy-octane-317516",
  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1yXHiTXdQCkRf2oaLtUMoNARywCNNsJcmTUccm\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": "[email protected]",
  "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"
}

if this is your JSON file then you need to copy the client_email that is [email protected] and then share your sheets to this email id.