如何使用pygsheets上传工作表

时间:2017-02-20 18:49:57

标签: python json google-sheets-api pygsheets

我正在使用http://pygsheets.readthedocs.io/en/latest/index.html使用google sheet api v4的包装器。

我有一个小脚本,我试图选择一个json工作表上传到谷歌工作表工作表。文件名由:

组成
spreadsheetname_year_month_xxx.json

代码:

import tkinter as tk
import tkFileDialog
import pygsheets


root = tk.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
print file_path
file_name = file_path.split('/')[-1]
print file_name
file_name_segments = file_name.split('_')
spreadsheet = file_name_segments[0]
worksheet = file_name_segments[1]+'_'+file_name_segments[2]

gc = pygsheets.authorize(outh_file='client_secret_xxxxxx.apps.googleusercontent.com.json')

ssheet = gc.open(spreadsheet)
ws = ssheet.add_worksheet(worksheet(ssheet,str(raw_input(file_path))))

文件路径导致生成的json文件如下所示:

{
  "count": 12, 
  "results": [
    {
      "case": "abc1", 
      "case_name": "invalid", 
      "case_type": "invalid", 

    }, 
    {
      "case": "abc2", 
      "case_name": "invalid", 
      "case_type": "invalid", 
      },
............

我得到了:

File "upload_to_google_sheets.py", line 27, in <module>
ws = ssheet.add_worksheet(worksheet(ssheet,str(raw_input(file_path))))
TypeError: 'unicode' object is not callable

正如您所看到的,我尝试使用json数据实例化工作表。我做错了什么?

1 个答案:

答案 0 :(得分:1)

JsonSheet参数不适用于工作表的值,而是用于指定工作表的属性,因此应为this格式。

由于直接将json转换为电子表格非常模糊,因此您需要先将其转换为numpy数组(矩阵)或pandas DataFrame。然后,您可以使用set_as_df update_values来更新电子表格。