With this code I can download all the sheets that are in the spreadsheet to csv. but i just want to download a sheet. Is there how to do it?
Code
import csv
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
docid = "123"
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(docid)
for i, worksheet in enumerate(spreadsheet.worksheets()):
filename = docid + '-worksheet' + str(i) + '.csv'
with open(filename, 'wb') as f:
writer = csv.writer(f)
writer.writerows(worksheet.get_all_values())
If anyone knows, please let me know.
Using the method to get the worksheet by name should work for you.
import csv
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
docid = "123"
client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(docid)
worksheetName = "sheet2"
worksheet = spreadsheet.worksheet(worksheetName)
filename = docid + '-worksheet' + worksheetName + '.csv'
with open(filename, 'wb') as f:
writer = csv.writer(f)
writer.writerows(worksheet.get_all_values())