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表格

相关推荐
金銀銅鐵12 小时前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup1117 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi0019 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵21 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent
星云穿梭2 天前
用Python写一个带图形界面的学生管理系统——完整教程
python