import gspread
from oauth2client.service_account import ServiceAccountCredentials
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"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
client = gspread.authorize(creds)
sheet = client.open("doc_name").sheet1 # Open the spreadhseet
当我运行这段代码时,我得到了错误:
Traceback(最近一次调用):File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gspread/client.py"属性= finditem()File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gspread/utils.py",第88行,在finditem . js文件中返回next((item for item in seq if func(item)))
抛出StopIteration
在处理上述异常时,出现了另一个异常:
Traceback(最近一次调用):文件"/Users/samar/PycharmProjects/Automation/sheets.py",第9行sheet = client.open("Covid Data 2020-21")。#打开电子表格File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gspread/client.py",第130行,在open
raise SpreadsheetNotFound
gspread.exceptions.SpreadsheetNotFound
您收到的错误似乎表明带有"doc_name"您提供的名称不存在。
要从特定的电子表格中检索特定的工作表,我建议您提供正确的名称或对代码进行以下更改:
spread_sheet = client.open_by_key('SPREADSHEET_ID')
work_sheet = spread_sheet.worksheet('SHEET_NAME')
参考- Gspread使用。