【发布时间】:2021-08-05 15:07:08
【问题描述】:
背景
大家好...对 python 来说还是很新的。我在 Firefox (87.0) 中运行 Jupyter Notebook 的 Mac (Sierra) 上。我正在尝试使用我的 python 脚本来构建一个数据框,在我的个人 Google Drive 上的特定文件夹中创建一个新的 Google 工作表(一个新工作簿,而不是现有工作簿中的新工作表),然后将我的数据框架写入那个新的 Google 表格。
我的尝试
我已经能够让我的服务帐户打开现有的 Google 表格,然后使用如下代码进行读/写:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import set_with_dataframe
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_path, scope)
client = gspread.authorize(creds)
workbook = client.open_by_key(my_existing_google_sheet_key)
有大量关于如何从现有 Google 表格读取/写入的文档,但正如我所说,我想创建一个新的 Google 表格来写入。这出乎意料地难以找到。我确实找到的一个参考是this related post,但无法让 OP 的代码工作(什么是“发现”?)。我觉得我错过了一些明显的东西。
问题
- 如何使用 python 让我的服务帐户在我的个人 Google Drive 上的特定文件夹中创建一个新的 Google Sheet,以便我可以写入它?
【问题讨论】:
标签: python google-sheets google-drive-api google-sheets-api