使用 Python 通过 Google API 将电子表格复制到另一张电子表格

2023-12-31

我正在尝试从模板创建一个谷歌电子表格,然后编辑其值(单元格)。我只需手动访问原始电子表格并单击复印一份来自File菜单。我还没有找到使用 Python3 和 gspread 来做到这一点的方法。因此,我正在尝试寻找解决方法。

因此,我使用 Python 脚本来创建新的谷歌工作表,如代码片段所示(我仅将其用作此问题的查看器):

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from pprint import pprint
from googleapiclient import discovery

scope = ['https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)

service = discovery.build('sheets', 'v4', credentials=credentials)

spreadsheet_body = {
"properties": {
    "title": "xxGoogleAPIMasterTemplatexx"
  }
}

request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()

#Changing permissions for the user (from the credentials.json) and then the real user (me)
gc.insert_permission(response['spreadsheetId'], '[email protected] /cdn-cgi/l/email-protection', perm_type='user', role='owner')
gc.insert_permission(response['spreadsheetId'], '[email protected] /cdn-cgi/l/email-protection', perm_type='user', role='owner')

新的电子表格称为xxGoogleAPIMasterTemplatexx正如我所料(脚本)。现在,我尝试将一个电子表格(原始模板)中的一张工作表复制到新创建的电子表格中。

我现在的步骤是首先测试是否copy_to_spreadsheet works对于特定 ID。我使用了以下脚本:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from pprint import pprint
from googleapiclient import discovery

scope = ['https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)

# The ID of the spreadsheet containing the sheet to copy. Everybody has access!!!
spreadsheet_id = '1lw6FVG_gSDseDdseS54jOyXph74k_XfTvnyU2Wqd7yo'  
#sheet = gc.open_by_key(response['spreadsheet_id']).Sheet1

# The ID of the sheet to copy. Everybody has access!!!
sheet_id = 0  

copy_sheet_to_another_spreadsheet_request_body = {
    {
  "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}
}

request = service.spreadsheets().sheets().copyTo(spreadsheetId=spreadsheet_id, sheetId=sheet_id, body=copy_sheet_to_another_spreadsheet_request_body)
response = request.execute()

我得到了这个错误:

"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
TypeError: unhashable type: 'dict'

我猜测 ID 必须是可哈希的。添加此行后:

key = frozenset(dict_key.spreadsheet_id)

它仍然不起作用。

Please note that for the test purposes I changed the permissions of the files to global: enter image description here


下面的修改怎么样?

我认为该错误意味着结构"destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"是错的。当它看到右侧时Spreadsheets.sheets.copyTo 的文档 https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.sheets/copyTo,请求体是{"destinationSpreadsheetId": ""}。所以请尝试以下修改。

From :

copy_sheet_to_another_spreadsheet_request_body = {
    {
  "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}
}

To :

copy_sheet_to_another_spreadsheet_request_body = {
  "destinationSpreadsheetId": "1d8CeUxukBIOUpADE6eBlaksS2ptBjI0vIRpVm8ufEbs"
}

如果我误解了你的问题,我很抱歉。

Edit :

对于下一个错误,我确认了以下模式。

  1. 当使用此脚本的用户是您要复制的电子表格的所有者时,当然,该脚本可以正常工作。
  2. When the user who uses this script is NOT the owner of the spreadsheet that you want to copy,
    • 如果电子表格未共享,则会出现以下错误"The caller does not have permission"发生。此错误可能与您的情况相同。
    • 如果电子表格共享为On - Anyone with the link,脚本运行良好。

从以上结果中,您能否确认您要再次复制的电子表格的权限?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python 通过 Google API 将电子表格复制到另一张电子表格 的相关文章

  • 如何让“conda”安装程序查找“PyPi”包

    我试图使用conda http conda pydata org docs using pkgs html managing packages包管理器来安装我的 Python 包 最近 我遇到了 Anaconda org 存储库中不存在我需
  • 从 torch.autograd.gradcheck 导入 zero_gradients

    我想复制代码here https github com LTS4 DeepFool blob master Python deepfool py 并且我在 Google Colab 中运行时收到以下错误 ImportError 无法导入名称
  • 从所有数据帧列中删除子字符串

    我有一个单词列表 大约 1000 个单词 我称之为负面单词 CAST ARTICLES SANITARY JAN CLAUSES SPECIAL ENDORSEMENT 我很快就会用这个单词列表制作一个数据框 我还有一个数据框 看起来像 F
  • Python,Google Places API - 给定一组纬度/经度查找附近的地点

    我有一个由商店 ID 及其纬度 经度组成的数据框 我想迭代该数据框 并使用 google api 为每个商店 ID 查找附近的关键地点 例如输入 Store ID LAT LON 1 1 222 2 222 2 2 334 4 555 3
  • 将整数系列转换为交替(双元)二进制系列

    我不知道如何最好地表达这个问题 因为在这里谷歌搜索和搜索总是让我找到更复杂的东西 我很确定这是基本的东西 但对于我的生活来说 我找不到一个好的方法来做到这一点下列 给定一个整数序列 比如说 for x in range 0 36 我想将这些
  • 使用 Tkinter 打开网页

    因此 我的应用程序需要能够打开其中的单个网页 并且它必须来自互联网并且未保存 特别是我想使用 Tkinter GUI 工具包 因为它是我最熟悉的工具包 最重要的是 我希望能够在窗口中生成事件 例如单击鼠标 但无需实际使用鼠标 有什么好的方法
  • 如何找到多个 pandas 数据框中一对列与任意顺序对的交集?

    我有多个 pandas 数据框 为了简单起见 假设我有三个 gt gt df1 col1 col2 id1 A B id2 C D id3 B A id4 E F gt gt df2 col1 col2 id1 B A id2 D C id
  • 使用 scikit 时 scipy.sparse 矩阵的缩放问题

    在使用 scikit learn 解决机器学习问题时 我需要在使用 SVM 进行训练之前对 scipy sparse 矩阵进行缩放 但在文档 http scikit learn org stable modules preprocessin
  • 为什么需要设置WORKON_HOME环境变量?

    我已经有一段时间没有使用 python 虚拟环境了 但我也安装了虚拟环境包装器 我的问题是 在文档页面中它说要这样做 export WORKON HOME Envs mkdir p WORKON HOME source usr local
  • 获取 Keras model.summary() 作为表

    我在 Keras 中创建了相当大的模型 我正在用 LaTeX 写一篇关于它的文章 为了很好地描述 LaTeX 中的 keras 模型 我想用它创建一个 LaTeX 表 我可以手动实现它 但我想知道是否有任何 更好 的方法来实现这一点 我四处
  • 列表推导式和 for 循环中的 Lambda 表达式[重复]

    这个问题在这里已经有答案了 我想要一个 lambda 列表 作为一些繁重计算的缓存 并注意到这一点 gt gt gt j for j in lambda i for i in range 10 9 9 9 9 9 9 9 9 9 9 Alt
  • 我可以用关闭的文件对象做什么?

    当您打开文件时 它存储在一个打开的文件对象中 该对象使您可以访问该文件的各种方法 例如读取或写入 gt gt gt f open file0 gt gt gt f
  • 如何从 python 脚本执行 7zip 命令

    我试图了解如何使用 os system 模块来执行 7zip 命令 现在我不想用 Popen 或 subprocess 让事情变得复杂 我已经安装了 7zip 并将 7zip exe 复制到我的用户文件夹中 我只想提取我的测试文件 inst
  • 使用会话在 Django 中将文件从一个视图传递到另一个视图

    我当前的工作项目要求我允许用户上传各种格式的文件 目前仅处理 CSV 格式 然后使用包含的数据来绘制图表Pandas http pandas pydata org 图书馆 我决定将图形渲染到模板的最简单方法是为图形创建特定视图 然后将图像从
  • 使用 ElementTree 在 python 中解析 xml

    我对 python 很陌生 我需要解析一些脏的 xml 文件 这些文件需要先清理 我有以下 python 代码 import arff import xml etree ElementTree import re totstring wit
  • Snakemake:将多个输入用于具有多个子组的一个输出的规则

    我有一个工作管道 用于下载 比对和对公共测序数据执行变体调用 问题是它目前只能在每个样本的基础上工作 i e作为每个单独测序实验的样本 如果我想对一组实验 例如样本的生物和 或技术复制 执行变体调用 则它不起作用 我试图解决它 但我无法让它
  • 如何将 URL 添加到 Telegram Bot 的 InlineKeyboardButton

    我想制作一个按钮 可以从 Telegram 聊天中在浏览器中打开 URL 外部超链接 目前 我只开发了可点击的操作按钮 update message reply text Subscribe to us on Facebook and Te
  • 数据损坏 C++ 和 Python 之间的管道

    我正在编写一些代码 从 Python 获取二进制数据 将其通过管道传输到 C 对数据进行一些处理 在本例中计算互信息度量 然后将结果通过管道传输回 Python 在测试时 我发现如果我发送的数据是一组尺寸小于 1500 X 1500 的 2
  • AWS 将 MQTT 消息存储到 DynamoDB

    我构建了一个定期发送 MQTT 消息的 python 脚本 这是发送到后端的 JSON 字符串 Id 1234 Ut 1488395951 Temp 22 86 Rh 48 24 在后端 我想将 MQTT 消息存储到 DynamoDB 表中
  • 如何有效地比较 pandas DataFrame 中的行?

    我有一个 pandas 数据框 其中包含雷击记录以及时间戳和全球位置 格式如下 Index Date Time Lat Lon Good fix 0 1 20160101 00 00 00 9962692 7 1961 60 7604 1

随机推荐