读取/写入Google Spreadsheet中的数据

时间:2019-05-27 07:10:09

标签: selenium data-driven-tests

我不在本地计算机上使用MS Office。因此,我正在使用Google文档。

现在,我需要创建一个脚本来从Selenium中的Google电子表格中获取数据。

我想使用Selenium Web驱动程序从Google电子表格中获取数据[读/写]。

有人知道怎么做吗?

技术: Selenium Web驱动程序 爪哇 测试NG Eclipse IDE

1 个答案:

答案 0 :(得分:0)

我现在无权访问Google表格,但我猜它看起来像这样。

pip install gspread oauth2client

然后...

import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Copy of Legislators 2017").sheet1

# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

或者,获取列表列表:

sheet.get_all_values()

最后,您可以从单个行,列或单元格中提取数据:

sheet.row_values(1)

sheet.col_values(1)

sheet.cell(1, 1).value

https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html

https://towardsdatascience.com/accessing-google-spreadsheet-data-using-python-90a5bc214fd2