Skip to content
Pinter Computing

Knowledge Base for IT Professionals, Teachers, and Astronauts

Pinter Computing

Knowledge Base for IT Professionals, Teachers, and Astronauts

  • Home
  • Programming
  • DevOps
  • Project Management
  • Software and Hardware
  • Miscellaneous
  • Egyebek
  • About
  • Experience
  • Education
  • Contact
  • Home
  • Programming
  • DevOps
  • Project Management
  • Software and Hardware
  • Miscellaneous
  • Egyebek
  • About
  • Experience
  • Education
  • Contact
Close

Search

Home/Knowledge Base/Read and Update Google Sheet data with Python
Knowledge Base

Read and Update Google Sheet data with Python

By Laszlo Pinter
September 28, 2020 6 Min Read
0

In this example we will use python to read data from Google Sheets and update another one using the Google API.

Enable the Google API

  • Navigate to https://console.developers.google.com/
  • Log in with your Google account
  • Click the Create Project link
  • Enter a project name, you can have 12 free project.
  • Click the API panel
  • Click the ENABLE APIS AND SERVICES link
  • In the G Suite section click theVIEW ALL link
  • Click the Google Sheets API panel
  • Click the Enable button
  • Navigate back to the API panel list and click the Google Drive API panel
  • Click the Enable button

Create API credentials

OAuth 2.0 Client credentials

Create the Consent Screen

First, we need to create a Consent Screen before we create our first OAuth 2.0 credential

  • On the left side select Credentials, click the CREATE CREDENTIALS button and select OAuth client ID
  • Click the CONFIGURE CONSENT SCREEN
  • Select External and click the CREATE button
  • Enter the application name, select an email address from the list, enter the developer’s email address and click the SAVE AND CONTINUE button
  • Add a Space
  • Select ../auth/drive.file
  • Click SAVE AND CONTINUE
  • Save again

Create the OAuth 2.0 credential

  • Create the credential
  • In our case, we build a desktop app
  • Enter a Name and click the CREATE button
  • Close the popup

Download the OAuth2 credentials

  • On the Credentials page click the name of the OAuth 2.0 credential name
  • Click the DOWNLOAD JSON button

Read data from Google Sheets with Python

We will use the Google tutorial at https://developers.google.com/sheets/api/quickstart/python

  • Copy the downloaded OAuth 2.0 credentials file to the application directory.
  • Add the file name to the .gitignore file to avoid committing it to GitHub
  • Enter the downloaded credentials file name in the line
            flow = InstalledAppFlow.from_client_secrets_file(
                'MY_OAUTH_CRED_FILE.json', SCOPES)
  • Set the Google sheet ID and data range in
SAMPLE_SPREADSHEET_ID = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
  • The rows of your Google sheet are stored in rows of the values[] array. Adjust the row displayed column numbers in the line
print('%s, %s' % (row[0], row[4]))

Run the application

  • The app will open a page in the default web browser and will ask for a Google login
  • Click Advanced
  • Click the Go to … link
  • Grant the permission
  • Allow the access
  • Confirmation is displayed
  • Your application should display the data from the Google spreadsheet

To use a service account to read Google Sheets

Create a Google Service account

  • Click the CREATE CREDENTIALS button
  • Select the Google Drive API, Other non-UI, Appication data, No, I’m not using them options
  • Click the What credentials do I need? button
  • For the Role select Project, Owner
  • Keep JSON as the private key format, click the Continue button
  • The JSON private key downloads, save it to be able to access the API

Share the Google sheet with the Service Account

  • Open the downloaded JSON key file and copy the client_email value
  • Open the Google Sheet you want to read from
  • Click the Share button
  • Make the service account a Viewer to read the data, but if you later want to update the sheet with the same account make the service account an Editor

Reading Google sheets with a Service Account

Install the gspread, oauth2client, and pandas Python libraries on your workstation

  • Open a terminal window and execute the commands
pip3 install gspread
pip3 install --upgrade google-api-python-client oauth2client
pip3 install pandas
  • Save the code in a .py file
# import the libraries
import gspread
import pandas as pd
from oauth2client.service_account import ServiceAccountCredentials

# define the scope
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']

# get the credentials from the JSON file
creds = ServiceAccountCredentials.from_json_keyfile_name('MY_CREDENTIALS_FILE.json', scope)

# authorize service account in the the clientsheet 
client = gspread.authorize(creds)

Tags:

Google CloudPython
Author

Laszlo Pinter

Follow Me
Other Articles
Previous

Kovászos, Vajas, Tejes Kifli

Next

Failed to complete #create action: [You are not authorized to perform this operation

No Comment! Be the first one.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Last Changes

  • DevOps Engineering part 1. (Mac) - Make your Macintosh easier to use June 25, 2026
  • Japan travel tips June 22, 2026
  • How to stop the rain and snow in Cities: Skylines II June 20, 2026
  • Cities: Skylines II Developer Mode June 20, 2026
  • 'CSII_MANAGEDPATH' has incorrect path(s) when building Cities: Skylines II mod June 20, 2026

Tags

.NET .NETcore 3Dprinting ASP.NET Core AutodeskInventor AWS C# Chef cloud DevOps Docker EntityFramework Games Git Go iOS iPad iPhone iPod Java Kubernetes Linux MacOSX MSSQL MVC Node.js Packer PowerShell Python RDS RightScale Ruby security Splunk TeamCity Terraform TestKitchen Tomcat Ubuntu Vagrant VirtualBox VisualStudio Windows WordPress Xcode

Recent Comments

  • Zengei László on MyHeritage családfa exportálása és küldése emailben
  • Raúl Castillo on DynDns update error
  • MICHAEL on Windows Media Player 12 cannot find the album information
  • Nargis on Configure Epson ET-3850 scanning on Windows 11
  • Venczelné Zemen Erika on Delta S2302 termosztát programozása

–

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
Copyright 2026 — Pinter Computing. All rights reserved.