模块 'gspread.worksheet' 没有属性 'update_cell'

module 'gspread.worksheet' has no attribute 'update_cell'

我的代码:

import gspread
from gspread import worksheet
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("keys.json", scope)
client = gspread.authorize(creds)
spreadsheet = client.open("ackerwaldundwiese")
worksheet.update_cell(1, 2, "car")

输出

Traceback (most recent call last):
  File "C:\Users\mm\PycharmProjects\pythonProject1\ddf.py", line 8, in <module>
    worksheet.update_cell(1, 2, "car")
AttributeError: module 'gspread.worksheet' has no attribute 'update_cell'

我正在使用 PyCharm 2022.1.1(社区版)windows 10

您好,在访问作品之前sheet您必须先检索它。

在你的情况下应该是这样的:

spreadsheet = client.open("ackerwaldundwiese")
worksheet = spreadsheet.sheet1
worksheet.update_cell(1, 2, "car")

注意:这将return第一个sheet,如果你需要得到不同的sheet使用方法get_worksheet

您应该从代码中删除这一行:

from gspread import worksheet

您不需要导入 Worksheet 对象,它区分大小写,因此 worksheet 对象不存在,但 Worksheet 确实。