使用pandas数据框和gspread更新现有的google工作表

时间:2017-03-13 23:44:41

标签: python pandas gspread

我想使用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行,行数根据生成数据帧的时间而变化。

任何建议表示赞赏。

3 个答案:

答案 0 :(得分:2)

我最终将pandas数据框转换为列表并使用pygsheets包更新google工作表。

import pygsheets
gc = pygsheets.authorize(service_file='file.json')

df = df.tolist()

#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.update_cells(crange='B2',values = df)

答案 1 :(得分:2)

pygsheets有pandas支持inbuild。

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')

答案 2 :(得分:1)

您还可以使用df2gspread库:

NotificationCenter.default.addObserver(self, selector: #selector(clipboardChanged),
                                       name: NSNotification.Name.UIPasteboardChanged , object: nil)