简体   繁体   中英

How do I fix "SpreadsheetNotFound" when interfacing Python with Google Spreadsheets?

I'm trying to access and modify data on Google Spreadsheet using Python. I'm having trouble to open the Google Spreadsheet from Python. I closely followed various tutorials and prepared the following before writing any code.

  • Enabled Google Sheets API and Google Drive API on GCP Console
  • Generated and downloaded credentials (JSON file) from GCP Console
  • Spreadsheet: Shared (edit-access) with the client email found in the JSON file
  • Installed gspread and oauth2client --> pip install gspread oauth2client

The following is the Python code to interface with Google Sheets. The goal in Lines 12 and 13, is to output to the console all of the data found in the linked Google Spreadsheet.

 1    import gspread
 2    from oauth2client.service_account import ServiceAccountCredentials
 3
 4    scope = ["https://spreadsheets.google.com/feeds","https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
 5    creds = ServiceAccountCredentials.from_json_keyfile_name('Py-Sheets.json', scope)
 6    client = gspread.authorize(creds)
 7    
 8    print("Hello World")
 9
10    sheet = client.open("Test-Sheets").sheet1
11
12    sample = sheet.get_all_records()
13    print(sample)

Everything seems to run fine up to line 10 (above), where I get an error saying SpreadsheetNotFound . Here's the error in full (below).

Traceback (most recent call last):
  File "/home/username/anaconda3/lib/python3.7/site-packages/gspread/client.py", line 119, in open
    self.list_spreadsheet_files(title),
  File "/home/username/anaconda3/lib/python3.7/site-packages/gspread/utils.py", line 97, in finditem
    return next((item for item in seq if func(item)))
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pysheets.py", line 10, in <module>
    sheet = client.open("Test-Sheets").sheet1
  File "/home/username/anaconda3/lib/python3.7/site-packages/gspread/client.py", line 127, in open
    raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound

I also received the following error via email.

DNS Error: 15698833 DNS type 'mx' lookup of python-spreadsheets-123456.iam.gserviceaccount.com responded with code NXDOMAIN Domain name not found: python-spreadsheets-123456.iam.gserviceaccount.com

How do I fix the error created after executing Line 10? The code is almost the exact same as what I found in the tutorials. The spreadsheet is named exactly what I put in client.open() . Does the spreadsheet have to be in a specific GDrive directory for it to be located?

An alternative would be opening the spreadsheet by URL on Google Colab:

# Access Google Sheets as a data source.
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
# At this point, you will have a link printed in your output which will direct you to a sign-in page. Pick the relevant google account and copy the provided link. Paste it in the provided line at the output section.
 

# Load your dataframe

import pandas as pd
        
wb = gc.open_by_url('https://docs.google.com/spreadsheets/.....') # A URL of your workbook.
sheet1 = wb.worksheet('Sheet1') # Enter your sheet name.
original_df = sheet1.get_all_values()
df = pd.DataFrame(original_df)
df.head()

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