Skip to content

Instantly share code, notes, and snippets.

@mrprompt
Created April 17, 2018 17:59
Show Gist options
  • Save mrprompt/829289e0c84296afa49562a5143a7615 to your computer and use it in GitHub Desktop.
Save mrprompt/829289e0c84296afa49562a5143a7615 to your computer and use it in GitHub Desktop.
Sample code to work with Google Spreadsheet and Python
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('python-15f4d92f03f2.json', scope)
client = gspread.authorize(creds)
sheet = client.open('Horta').sheet1
# get all
plantas = sheet.get_all_records()
pprint.pprint(plantas)
# get single row
result = sheet.row_values(2)
pprint.pprint(result)
# get single col
result = sheet.col_values(2)
pprint.pprint(result)
# get single cell
result = sheet.cell(2, 2)
pprint.pprint(result)
# update cel
sheet.update_cell(6, 2, 'Alface')
result = sheet.cell(6, 2).col_value
pprint.pprint(result)
row = ["I'm", "updating", "a", "spreadsheet", "from", "python"]
index = 3
sheet.insert_row(row, index)
# row count
count = sheet.row_count
pprint.pprint(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment