python如何使用gspread读取google在线excel数据?

一、背景

公司使用google在线excel管理测试用例,为了方便把手工测试用到的测试数据用来做自动化用例测试数据,所以就想使用python读取在线excel数据,通过数据驱动方式,完成自动化回归测试,提升手动复制,粘贴的效率。

二、实现过程

gspread官网

第一步:为项目启用 API 访问

参考链接:enable-api-access

第二步:将Goog​​le表格分享给client_email

(在凭证json file文件中) ,这应该类似于: id.gserviceaccount.com

第三步:读取表格内容

python3 复制代码
import gspread

def google_online_excel_utils():
    credentials = {
        "type": "xxx",
        "project_id": "xxx",
        "private_key_id": "xxx",
        "private_key": "xxx",
        "client_email": "xxx",
        "client_id": "xxx",
        "auth_uri": "xxx",
        "token_uri": "xxx",
        "auth_provider_x509_cert_url": "xxx",
        "client_x509_cert_url": "",
        "universe_domain": "xxx"
    }

    gc = gspread.service_account_from_dict(credentials)
    
    sh = gc.open_by_url("target_url")  # 打开在线excel地址

    worksheet = sh.worksheet("测试用例") #选择需要打开的sheet页
    case_data_list = worksheet.get_all_values()  #获取所有信息
    print(case_data_list)
    return case_data_list

if __name__ == '__main__':
    google_online_excel_utils()

第四步:实现效果

三、参考文档

如何使用Python读取和写入Google表格

相关推荐
devilnumber41 分钟前
Java 递归算法 详解 + 核心要点 + 实战运用 + 避坑指南
java·开发语言·算法
大貔貅喝啤酒1 小时前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf2 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf2 小时前
Prompt 组织管理
后端·python·agent
asdfg12589632 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy11013 小时前
JavaScript变量
开发语言·javascript·ecmascript
shimly1234563 小时前
python3 uvicorn 是啥?
python
z落落3 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保4 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway24 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash