1

I have this python file that will write to my Google sheet using the Google Sheets API just fine using the code from this example from Python Google API writing to Google Sheet google-sheet:

values = [
    ["Item", "Cost", "Stocked", "Ship Date",],
    ["Wheel", "20.50", "4", "3/1/2016",],
    ["Door", "15", "2", "3/15/2016",],
    ["Engine", "100", "1", "30/20/2016",],
    ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)",],
]
Body = {  
'values' : values,
'majorDimension' : 'ROWS',
}
}
result = service.spreadsheets().values().update(
spreadsheetId=spreadsheetID, range=rangeName, 
valueInputOption='USER_ENTERED', body=Body).execute()  
print("Writing OK!!")

However I want to write a dataframe that looks like this:

    Date    ORCL    TSLA    IBM YELP    MSFT
0   10/24/2016  37.665958   202.759995  145.080612  34.48   59.564964
1   10/25/2016  37.754536   202.339996  145.379303  33.950001   59.555199
2   10/26/2016  37.705326   202.240005  146.275406  33.490002   59.203667
3   10/27/2016  37.616749   204.009995  147.759277  32.740002   58.686134
4   10/28/2016  37.567539   199.970001  147.046234  32.290001   58.461548
5   10/31/2016  37.813587   197.729996  148.086884  32.66   58.510365
246 10/16/2017  48.860001   350.600006  146.830002  43.52   77.650002
247 10/17/2017  49.189999   355.75  146.539993  43.200001   77.589996
248 10/18/2017  49.580002   359.649994  159.529999  44.580002   77.610001
249 10/19/2017  49.349998   351.809998  160.899994  44.439999   77.910004
250 10/20/2017  49.25   345.100006  162.070007  44.52   78.809998
251 10/23/2017  49.310001   337.019989  159.550003  43.599998   78.830002

I have tried from here: Appending pandas Data Frame to Google spreadsheet to-google-spreadsheet

import gspread
import gc
import pandas as pd
gc = gspread.authorize(credentials)
sh.share('otto@gmail.com', perm_type='user', role='writer')
sh = gc.open_by_key('1C09vB5F8zcyOrY4w_rctVUedXYJCZqtyoTc-bB0bgBY')

sheetName = 'sheet1'
rangeName = "Sheet1!A1:F252"

I declared my dataframe:

df = pd.DataFrame()

After some processing a non-empty data frame has been created...and populated it:

    Date    ORCL    TSLA    IBM YELP    MSFT
0   10/24/2016  37.665958   202.759995  145.080612  34.48   59.564964
1   10/25/2016  37.754536   202.339996  145.379303  33.950001   59.555199
2   10/26/2016  37.705326   202.240005  146.275406  33.490002   59.203667
3   10/27/2016  37.616749   204.009995  147.759277  32.740002   58.686134
4   10/28/2016  37.567539   199.970001  147.046234  32.290001   58.461548
5   10/31/2016  37.813587   197.729996  148.086884  32.66   58.510365
246 10/16/2017  48.860001   350.600006  146.830002  43.52   77.650002
247 10/17/2017  49.189999   355.75  146.539993  43.200001   77.589996
248 10/18/2017  49.580002   359.649994  159.529999  44.580002   77.610001
249 10/19/2017  49.349998   351.809998  160.899994  44.439999   77.910004
250 10/20/2017  49.25   345.100006  162.070007  44.52   78.809998
251 10/23/2017  49.310001   337.019989  159.550003  43.599998   78.830002

# Output_conn = gc.open("SheetName").worksheet("xyz")
# Here 'SheetName' is google spreadsheet and 'xyz' is sheet in the workbook

Output_conn = gc.open(sheetName).worksheet(spreadsheetid)

for i,row in df.iterrows():
     Output_conn.append_row(Row)

from: Appending pandas Data Frame to Google spreadsheet frame-to-google-spreadsheet

I have this error msg at this line:

Output_conn = gc.open(sheetName).worksheet(spreadsheetid)

AttributeError: 'module' object has no attribute 'open'

Updated code:

import gspread
import gc
import pandas as pd
gc = gspread.authorize(credentials)
sh.share('otto@gmail.com', perm_type='user', role='writer')
sh = gc.open_by_key('1C09vB5F8zcyOrY4w_rctVUedXYJCZqtyoTc-bB0bgBY')

Error msg: UnboundedLocalError: local variable 'sh' referenced before assignment

Any help? Thanks!

Chas
  • 107
  • 4
  • 18
  • Where is gc defined? Maybe you are importing the garbage collection module? – NickD Oct 29 '17 at 22:47
  • Edit your question to show us what `gc` is. – John Gordon Oct 29 '17 at 22:56
  • Did you read this link? https://github.com/burnash/gspread `gc` is a connection object, you must instantiate it first. – cs95 Oct 29 '17 at 23:28
  • Thanks COLDSPEED, I read the link. gspread writes to a sheet in four steps: import gspread gc = gspread.authorize(credentials) # Open a worksheet from spreadsheet with one shot wks = gc.open("Where is the money Lebowski?").sheet1 wks.update_acell('B2', "it's down there somewhere, let me take another look.") – Chas Oct 30 '17 at 15:05

0 Answers0