Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gspread.exceptions.SpreadsheetNotFound

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

like image 234
TestIsFun Avatar asked Jun 02 '16 21:06

TestIsFun


People also ask

What is Gspread in Python?

gspread is a Python API for Google Sheets. Features: Google Sheets API v4. Open a spreadsheet by title, key or url. Read, write, and format cell ranges.

How do I create a new sheet in Gspread?

If you go to Google Sheets and hit the big "+" button you will start a new a new spreadsheet. This spreadsheet will at first contain a single worksheet named "Sheet1". You may add more worksheets to your spreadsheet.


2 Answers

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.

like image 96
ishandutta2007 Avatar answered Sep 26 '22 01:09

ishandutta2007


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.

like image 35
Jacques Gaudin Avatar answered Sep 22 '22 01:09

Jacques Gaudin