Reading Sheet2 via Google Sheets API & Python

Question:

I just wanna read the next tab/sheet via the API in my Google Sheets Project with Python (Windows 10) Everything works for sheet1, I just want to read/edit sheet2 (and others that I will create in the project).

Tab example: https://i.imgur.com/260rI7K.jpg

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_penny.json', scope)
client = gspread.authorize(creds)

penny = client.open('pennystocks').sheet1

print(penny.get_all_records())

This works: penny = client.open(‘pennystocks’).sheet1

This doesn’t: penny = client.open(‘pennystocks’).sheet2

Asked By: Biks Wigglesworth

||

Answers:

  • You want to use the sheet except for the sheet of 1st tab using gspread.
    • For example, when there are 2 sheets of “Sheet1” and “Sheet2” in a Spreadsheet, you want to use “Sheet2”.

If my understanding is correct, how about this modification?

From:

penny = client.open('pennystocks').sheet1

To:

penny = client.open('pennystocks').worksheet('Sheet2')

Note:

  • client.open('pennystocks').sheet1 is open the 1st index of sheet. So for example, in your Spreadsheet, when the index of “Sheet1” is modified to “Sheet2”, client.open('pennystocks').sheet1 returns “Sheet2”.

Reference:

If I misunderstood your question and this was not the result you want, I apologize.

Answered By: Tanaike

This will work for you

sheet = client.open(‘google_sheet_name’).worksheet(‘sheet_name,eg. Sheet1,Sheet2’)
Answered By: Mandana
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.