简体   繁体   English

通过Python gspread从SQL数据库到Google工作表

[英]From SQL Database to Google sheet via Python gspread

I am trying to populate an already created google sheet from my sql table using python and gspread. 我正在尝试使用python和gspread从我的sql表填充一个已经创建的google工作表。

I can update the sheet one row at a time using a for loop, but i have a lot of data to add to the sheet and want to do a column at a time or more if possible. 我可以使用for循环一次更新工作表的一行,但是我有很多数据要添加到工作表中,并且希望一次或多于一列。

Any suggestions here's what i've been using and i get an error: Object of type 'Row' is not JSON serializable 这是我一直在使用的任何建议,但出现错误:'Row'类型的对象不是JSON可序列化的

#!/usr/bin/python3
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import dbconnect

#credentials for google 
gc = gspread.authorize(credentials)


worksheet = gc.open('NAMEOFWS').sheet1
cell_list = worksheet.range('A2:A86')

#connect to database using dbconnect and grab cursor
query = "select loc from table"
cursor.execute(query)

results = cursor.fetchall()
cell_values = (results)

for i, val in enumerate(cell_values):  
    cell_list[i].value = val  
worksheet.update_cells(cell_list)

I am not sure how to do this with gspread, but you can modify you code very easily and use pygsheets and it allows to update a column all at once. 我不确定如何使用gspread做到这一点,但是您可以非常轻松地修改代码并使用pygsheets,它可以一次更新一列。 Also, I am not sure what your data looks like so the below may need to be altered or you may need to alter your data set a little. 另外,我不确定您的数据是什么样子,因此可能需要更改以下内容,或者可能需要稍微更改数据集。 Hope this helps. 希望这可以帮助。

import pygsheets

gc = pygsheets.authorize(service_file = 'client_secret2.json')

# Open spreadsheet and select worksheet
sh = gc.open('Api_Test')
wks = sh.sheet1

#update Column
notes = [1,2,3,4] #this is the dataset to getupdated in the column
wks.update_col(4, notes, 1)# 4 is the number of column, notes is the dataset to update, 1 skips the first row (I used a header)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM