Skip to main content
 首页 » 编程设计

python之使用 pandas 数据框和 gspread 更新现有的谷歌表格

2025年05月04日18qlqwjy

我想使用 Pandas 数据框更新 Google 表格文档中的特定选项卡。我查看了 GitHub 和 readthedocs.io 上的文档,并能够使用以下代码成功更新特定范围的单元格:

cell_list = sheet4.range('A1:A7') 
cell_values = [1,2,3,4,5,6,7] 
 
for i, val in enumerate(cell_values):   
    cell_list[i].value = val     
 
sheet4.update_cells(cell_list) 

作为初学者,我无法将各个点联系起来并弄清楚如何使用 Pandas 数据框更新整个工作表。数据框将有 9 列和最多 9,000 行,行数根据数据框的生成时间而有所不同。

欢迎任何建议。

请您参考如下方法:

pygsheets 内置了 pandas 支持。

import pygsheets 
gc = pygsheets.authorize(service_file='file.json') 
 
#open the google spreadsheet  
sh = gc.open_by_url('url') 
 
#select the first sheet  
wks = sh[0] 
 
#update the first sheet with df, starting at cell B2.  
wks.set_dataframe(df, 'B2')