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:
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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With