首页 > 解决方案 > 使用 gspread 将 CSV 上传到 Google 表格

问题描述

我有一个需要上传到 Google 电子表格的 Json 对象。我已经搜索并阅读了各种资源,但找不到解决方案。有没有办法使用 gspread 将对象或 csv 从本地上传到谷歌电子表格。我宁愿不使用谷歌客户端 api。谢谢

标签: pythoncsvdrivegspread

解决方案


您可以使用方法导入 CSV 数据gspread.Client.import_csv。这是文档中的一个简单示例:

import gspread

# Check how to get `credentials`:
# https://github.com/burnash/gspread

gc = gspread.authorize(credentials)

# Read CSV file contents
content = open('file_to_import.csv', 'r').read()

gc.import_csv('<SPREADSHEET_ID>', content)

这会将您的 CSV 文件上传到带有<SPREADSHEET_ID>.

笔记

此方法删除所有其他工作表,然后完全替换第一个工作表的内容。


推荐阅读