Gspread:我想将电子表格复制到另一个文件夹

问题描述 投票:0回答:1

我尝试将电子表格复制到自定义文件夹,但收到以下错误消息:

gspread.exceptions.APIError: {'code': 404, 'message': 'File not found: <folder_id>', 'errors': [{'message': 'File not found: <folder_id>', 'domain': 'global', 'reason': 'notFound', 'location': 'fileId', 'locationType': 'parameter'}]}

这是我的代码,身份验证似乎工作正常:

def copy_spreadsheet(credentials):
    # Authenticate with Google Sheets using the JSON key file
    scope = ['https://www.googleapis.com/auth/spreadsheets',
             "https://www.googleapis.com/auth/drive"]


    creds = Credentials.from_service_account_file(
        credentials, scopes=scope)

    client = gspread.authorize(creds)
  
    client.copy(
        "<sheet_id>",
        title='new',
        copy_permissions=True,
        folder_id='<folder_id>')

此外,当我在没有“folder_id”的情况下复制时:

print(client.list_spreadsheet_files())

我得到了电子表格列表,但它们不在我的 Google 云端硬盘上,

有人可以帮助我吗?

提前致谢!

python google-sheets gspread
1个回答
0
投票

修改要点:

根据您的以下脚本,

creds = Credentials.from_service_account_file(
        credentials, scopes=scope)

我认为在你的情况下,似乎使用了服务帐户。

并且,从您的以下情况来看,

此外,当我在没有“folder_id”的情况下进行复制时: print(client.list_spreadsheet_files()),

我得到了电子表格列表,但它们不在我的 Google 云端硬盘上,

我担心在您目前的情况下,

folder_id
的文件夹可能会被放入您Google帐户的Google Drive中。如果我的理解是正确的,服务帐户的 Google 云端硬盘与您的 Google 帐户的 Google 云端硬盘不同。我认为这可能是您当前问题的原因。

为了在您的 Google 云端硬盘中显示复制的电子表格,以下流程怎么样?

流量:

  1. 使用服务帐户的电子邮件共享 Google 云端硬盘中

    folder_id
    的文件夹。

    • 在这种情况下,请使用 Google 云端硬盘中的浏览器手动操作。
    • 您可以在
      credentials
      的文件中确认服务帐户的电子邮件。
  2. 运行您的显示脚本。

通过此流程,您可以在 Google Drive 上的

folder_id
文件夹中看到复制的电子表格。

© www.soinside.com 2019 - 2024. All rights reserved.