使用Python创建新的Google工作表 背景
大家好……对python来说还是很陌生的。我在Mac(Sierra)上运行Firefox(87.0)中的Jupyter笔记本电脑。我正在尝试使用python脚本构建一个数据框,在特定文件夹中的个人Google驱动器上创建一个新的Google工作表(新工作簿,而不是现有工作簿中的新工作表),然后将我的数据框写入新的Google工作表 我试过的 我已经能够让我的服务帐户打开现有的Google工作表,然后使用如下代码进行读/写操作:使用Python创建新的Google工作表 背景,python,google-sheets,google-drive-api,google-sheets-api,Python,Google Sheets,Google Drive Api,Google Sheets Api,大家好……对python来说还是很陌生的。我在Mac(Sierra)上运行Firefox(87.0)中的Jupyter笔记本电脑。我正在尝试使用python脚本构建一个数据框,在特定文件夹中的个人Google驱动器上创建一个新的Google工作表(新工作簿,而不是现有工作簿中的新工作表),然后将我的数据框写入新的Google工作表 我试过的 我已经能够让我的服务帐户打开现有的Google工作表,然后使用如下代码进行读/写操作: import gspread from oauth2client.s
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)
关于如何从现有的谷歌工作表读/写,有很多文档,但正如我所说的,我想创建一个新的谷歌工作表来写。这一点出人意料地难以找到。我找到的一个参考是,但无法让OP的代码工作(什么是“发现”?)。我觉得我错过了一些明显的东西
问题
我相信你的目标如下
- 您希望使用服务帐户在您自己的Google Drive的特定文件夹中创建新的Google电子表格
- 您希望使用gspread和python来实现这一点
- 当我看到gspread的文件时,似乎可以使用。并且,在这种方法中,文件夹ID可用于放入特定文件夹
- 在这种情况下,需要将Google Drive的特定文件夹与服务帐户的电子邮件共享。通过此操作,可以使用服务帐户将电子表格创建到文件夹中。请注意这一点。
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from gspread_dataframe import set_with_dataframe
json_path = '###' # Please set the file for using service account.
scope = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name(json_path, scope)
client = gspread.authorize(creds)
spreadsheetTitle = 'new spreadsheet title'
folderId = '###' # Please set the folder ID of the folder in your Google Drive.
workbook = client.create(spreadsheetTitle, folder_id=folderId)