How to update Google Sheets from Python

A note on how to edit Google Sheets using OAuth.

Advance preparation

Creating Credentials

What you need here --Email address displayed on the console (eg [email protected]) --P12 key file downloaded above

Python module installation

Install the following with pip

code

import gspread
from oauth2client.client import SignedJwtAssertionCredentials

#Authentication
f = file('/path/to/p12keyfile', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
                  '[email protected]', # Email address
                  key,
                  scope='https://spreadsheets.google.com/feeds https://docs.google.com/feeds',
                  token_uri='https://accounts.google.com/o/oauth2/token'
               )
gs = gspread.authorize(credentials)


doc = gs.open('Worksheet name')

#Add Sheet
sheet = doc.add_worksheet('Sheet name', row=100, col=20)

#Sheet selection
sheet = doc.worksheet("Sheet name")

#Get value

val = sheet.acell('B1').value #When specifying a label, the method name is acell
val = sheet.cell(1,2).value   #When specifying coordinates(line,Column)

#Setting / changing values

sheet.update_acell('B1', 'hoge') #When specifying the label, the method name is update_acell
sheet.update_cell(1, 2, 'hoge')  #When specifying coordinates(line,Column)

#When there is a lot of change
cell_list = sheet.range('A1:C4')
'''
cell_list[0] : A1
cell_list[1] : B1
cell_list[2] : C1
cell_list[3] : A2
cell_list[4] : B2
 :
'''
for cell in cell_list:
    cell.value = 'fuga'
sheet.update_cells(cell_list)

How to not use OAuth

If you don't hesitate to write your password.


import gspread
doc = spread.login('[email protected]', 'password')

sheet = doc.add_worksheet('Sheet name', row=100, col=10)

Recommended Posts

How to update Google Sheets from Python
How to update Python Tkinter to 8.6
How to access wikipedia from python
Update Python on Mac from 2 to 3
How to access RDS from Lambda (python)
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to install Python
Changes from Python 2 to Python 3.0
How to update easy_install
How to install python
How to update Spyder
How to open a web browser from python
Study from Python Hour7: How to use classes
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
How to handle Linux commands well from Python
How to deal with OAuth2 error when using Google APIs from Python
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
Cheating from PHP to Python
[Python] How to use list 1
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
How to use Python argparse
How to update with SQLAlchemy?
From python to running instance on google cloud platform
Update python on Mac to 3.7-> 3.8
Update python-social-auth from 0.1.x to 0.2.x
Python: How to use pydub
[Python] How to use checkio
How to download files from Selenium in Python in Chrome
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
Connect to sqlite from python
Execute Python function from Powershell (how to pass arguments)
How to use Django on Google App Engine / Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
[Python] How to call a c function from python (ctypes)
python3: How to use bottle (3)
How to create a kubernetes pod from python code
python3: How to use bottle
I tried to access Google Spread Sheets using Python
How to use Google Colaboratory
How to use Python bytes
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
How to extract any appointment in Google Calendar with Python
How to launch AWS Batch from a python client app
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
How to hide your Google Maps API key from HTML
How to do Bulk Update with PyMySQL and notes [Python]
How to sample from any probability density function in Python
How to write to update Datastore to async with Google Apps Engine
How to update FC2 blog etc. using XMLRPC with python
Let's use Watson from Python! --How to use Developer Cloud Python SDK
How to call Python or Julia from Ruby (experimental implementation)