gpt4 book ai didi

python - 从一个 Google 表格复制并粘贴到另一个

转载 作者:行者123 更新时间:2023-12-05 04:40:56 26 4
gpt4 key购买 nike

我是 python 的新手,正在努力寻找一种使用 gspread 将数据从一个 google 工作表复制并粘贴到另一个工作表的方法。有谁知道如何在不使用 win32 作为桥梁复制到 excel 的情况下做到这一点?请查看下面的代码和错误消息:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import numpy as np
Scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name(r'C:\Users\Documents\Scripts\FX Rates Query\key.json', Scope)

client = gspread.authorize(creds)

sheet = client.open("Capital").sheet1
data=sheet.get_all_records()

df = pd.DataFrame(data)
df.to_excel(r'C:\Users\Documents\Reserves_extract.xlsx')
sheet1 = client.open("Cash Duration ").sheet1
mgnt_fees = sheet1.col_values(5)
fees = pd.DataFrame(mgnt_fees)
fees1 = fees[fees!=0]
print(fees1)
update = sheet1.update('B7',fees1)
##^^ERROR MSG IS COMING FROM HERE

Error msg:

raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type DataFrame is not JSON serializable

最佳答案

根据您对我想将特定列从 google 电子表格 A 复制到 google 电子表格 B 的回复,在这种情况下,进行以下修改怎么样?

修改后的脚本:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
import numpy as np
Scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name(r'C:\Users\Documents\Scripts\FX Rates Query\key.json', Scope)

client = gspread.authorize(creds)

# I modified below script.
spreadsheetA = client.open("Capital")
spreadsheetB = client.open("Cash Duration ")
srcCol = "'" + spreadsheetA.sheet1.title + "'!A1:A" # This is the column "A" of the 1st tab of Spreadsheet A.
dstCol = "'" + spreadsheetB.sheet1.title + "'!B1:B" # This is the column "B" of the 1st tab of Spreadsheet B.
src = spreadsheetA.values_get(srcCol)
del src['range']
spreadsheetB.values_update(dstCol, params={'valueInputOption': 'USER_ENTERED'}, body=src)
  • 在此修改后的脚本中,电子表格 A 第一个选项卡的“A”列被复制到电子表格 B 第一个选项卡的“B”列。请根据您的实际情况进行修改。

引用资料:

关于python - 从一个 Google 表格复制并粘贴到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70174978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com