Chris
Chris

Reputation: 76

How to update multiple sheets within one Google Spreadsheet via Python API

Currently I am sending DataFrames (Python/Pandas) to a Google Spreadsheet via gspread-dataframe Currently I'm pushing one DataFrame to One Google Spreadsheet.

my code is standard via the documentation, and looks like this:

from gspread_dataframe import get_as_dataframe, set_with_dataframe
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
google_client = gspread.authorize(creds)
sheet = google_client.open('User IDs').sheet1

My question: If I wanted to push multiple dataframes to multiple sheets on the same Google Spreadsheet, could I?

Note: I know I can make Separate Google Spreadsheets to hold Multiple DataFrames, but I would like to be able to have one Spreadsheet with Multiple sheets contained within it.

Thank you

Upvotes: 1

Views: 2448

Answers (1)

Chris
Chris

Reputation: 76

Google refers to "tabs" within Google Spreadsheets as "workbooks".

All you need to do is:

sheet1 = google_client.open('User IDs').workbook('sheet1')
sheet2 = google_client.open('User IDs').workbook('sheet2')
...
sheet_n = google_client.open('User IDs').workbook('sheet_n')

where 'sheet1'...'sheet_n' are the names of the workbooks within your spreadsheet 'User IDs'

THEN... you can push the data to the spreadsheet via

set_with_dataframe(WORKBOOK_NAME, DATAFRAME_NAME)

*assuming that you are using the gspread_dataframe module

Upvotes: 3

Related Questions