Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
read and write to a google spreadsheet from heroku using python
import os
import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# based on
# read that file for how to generate the creds and how to use gspread to read and write to the spreadsheet
# use creds to create a client to interact with the Google Drive API
scopes = ['']
json_creds = os.getenv("GOOGLE_SHEETS_CREDS_JSON")
creds_dict = json.loads(json_creds)
creds_dict["private_key"] = creds_dict["private_key"].replace("\\\\n", "\n")
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scopes)
client = gspread.authorize(creds)
# Find a workbook by url
spreadsheet = client.open_by_url("")
sheet = spreadsheet.sheet1
# Extract and print all of the values
rows = sheet.get_all_records()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment