I'm using Python 3.9 and the following version of Google Sheets ...
gsheets==0.5.1
gspread==3.6.0
I'm trying to export my Google sheet as a CSV file. In older versions of Python, I was using the Pandas module like so
import gspread
...
client = gspread.authorize(creds)
sheet = client.open('My_Sheet_name')
# get the third sheet of the Spreadsheet. This
# contains the data we want
sheet_instance = sheet.get_worksheet(3)
records_data = sheet_instance.get_all_records()
records_df = pd.DataFrame.from_dict(records_data)
# view the top records
records_df.to_csv(sys.stdout)
How would I export the CSV without using Pandas? I ask because it would seem newer versions of Python (e.g. 3.9) do not support the pandas module yet.
I believe your goal as situation as follows.
In this case, in order to achieve your goal, I would like to propose to use the endpoint for exporting the sheet as CSV data. The access token is retrieved from client
of client = gspread.authorize(creds)
. When this proposal is reflected to your script, it becomes as follows.
client = gspread.authorize(creds)
sheet = client.open('My_Sheet_name')
# get the third sheet of the Spreadsheet. This
# contains the data we want
sheet_instance = sheet.get_worksheet(2) # Modified
# I added below script.
url = 'https://docs.google.com/spreadsheets/d/' + sheet.id + '/gviz/tq?tqx=out:csv&gid=' + str(sheet_instance.id)
headers = {'Authorization': 'Bearer ' + client.auth.token}
res = requests.get(url, headers=headers)
print(res.text)
import requests
.About sheet_instance = sheet.get_worksheet(3)
, your comment says get the third sheet of the Spreadsheet.
. But the 1st number of get_worksheet
is 0
. So in this case, 4th sheet in the Spreadsheet is retrieved. Please be careful this.
In this case, I think that you can also use the endpoint as follows.
url = 'https://docs.google.com/spreadsheets/d/' + sheet.id + '/export?format=csv&gid=' + str(sheet_instance.id)