Biks Wigglesworth
Biks Wigglesworth

Reputation: 33

Reading Sheet2 via Google Sheets API & Python

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.sstatic.net/d4Ee9.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

Upvotes: 2

Views: 3114

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • 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.

Upvotes: 5

Related Questions