Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python创建新的Google工作表 背景_Python_Google Sheets_Google Drive Api_Google Sheets Api - Fatal编程技术网

使用Python创建新的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

大家好……对python来说还是很陌生的。我在Mac(Sierra)上运行Firefox(87.0)中的Jupyter笔记本电脑。我正在尝试使用python脚本构建一个数据框,在特定文件夹中的个人Google驱动器上创建一个新的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)
关于如何从现有的谷歌工作表读/写,有很多文档,但正如我所说的,我想创建一个新的谷歌工作表来写。这一点出人意料地难以找到。我找到的一个参考是,但无法让OP的代码工作(什么是“发现”?)。我觉得我错过了一些明显的东西

问题
  • 如何使用python让我的服务帐户在特定文件夹中的个人Google驱动器上创建一个新的Google工作表,以便我可以对其进行写入

  • 我相信你的目标如下

    • 您希望使用服务帐户在您自己的Google Drive的特定文件夹中创建新的Google电子表格
    • 您希望使用gspread和python来实现这一点
    修改点:
    • 当我看到gspread的文件时,似乎可以使用。并且,在这种方法中,文件夹ID可用于放入特定文件夹
    当这些内容反映到脚本中时,它将变成如下所示

    修改脚本: 注:
    • 在这种情况下,需要将Google Drive的特定文件夹与服务帐户的电子邮件共享。通过此操作,可以使用服务帐户将电子表格创建到文件夹中。请注意这一点。
    参考:

    @Fist Pump Cat现在,我注意到Class Client的create方法有一个参数,用于在创建新电子表格时将其放入特定文件夹。我为此道歉。使用此选项后,脚本将变得更简单。所以我更新了我的答案。你能确认一下吗?
    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)