gspread 使用 python 读取 google sheet 文件 3

gspread reading a google sheet file using python 3

我正在使用 Python 3 运行 Pycharm 和模块 gspread 来读取我的 google 驱动器中的 google sheet 文件.好吧,我已经按照 link 中关于如何读取文件的所有步骤进行了操作。不幸的是,我下面的代码还不起作用。

 import gspread
 from oauth2client.service_account import ServiceAccountCredentials


 scope =['https://docs.google.com/spreadsheets/d/1xnaOZMd2v93tY28h_hsuMnZYXC9YqCfFpQX70lwpN94/edit?usp=sharing']
 credentials = ServiceAccountCredentials.from_json_keyfile_name('distech-c1e26e7150b2.json',scope)
 gc = gspread.authorize(credentials)

 wks = gc.open("POC").sheet1

 for temp in wks:
     print(temp)

我如何使用这个模块读取 google sheet 文件?非常感谢

经过深入研究我明白了两件事。

  1. 我的代码中的范围是错误的,因为该范围只是 Google API 提供的范围,用于在 spreadsheet.[= 下授予正确的访问权限14=]

    对我来说正确的范围是:scope =['https://spreadsheets.google.com/feeds']

  2. 开瓶器它只是打开传播sheet,它将return我文件中的作品sheet。

感谢@Pedro Lobito 在他的 post .

中的解决方案

解决方案:

我的帐户上只有几个点差sheet时遇到了同样的问题,问题已解决:

  1. 正在打开 json 密钥文件(项目名称-ca997255dada.json)

  2. 求 client_email 的值,即:client_email": "278348728734832-compute@developer.gserviceaccount.com

  3. 与该电子邮件分享您的 sheet(s)

现在我的代码如下所示:

import gspread
from oauth2client.service_account import ServiceAccountCredentials


scope =['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('xxx.json',scope)
gc = gspread.authorize(credentials)

spreadsheet = gc.open("POC")
wks = spreadsheet.worksheet('test1')
wks2 = spreadsheet.worksheet('test2')
out = list()
out = wks.col_values(1)
for temp in out:
    print(out)