Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Show all Columns in the SQLite Database using Python ?
Next article icon

Using Google Sheets as Database in Python

Last Updated : 16 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article we’ll be discussing how we can use Google Sheets to run like a database for any Python file. 

Google Spreadsheets:

Google Spreadsheets is free online web based application that resembles Microsoft Excel. You can use it to create and edit tables for various projects such as Contact List, Budget and almost everything you can imagine. 

Google Spreadsheets (Highlighted is Toolbar)

Application Program Interface:

Connectivity is a very important thing, we are all used to it to the instant connectivity that puts the world at our fingertips from desktop or mobile. So how do data travel from Point A to Point B, how different devices and applications connect with each other. An API is the messenger that takes requests and tells a system what you want to do and returns response back to you. 

Google Spreadsheet API:

We can use this Google Spreadsheet as a data store and you can access this data store via an API so what we can do is we can put in our data and from your application you can access the data as a regular JSON API. 

Step-by-step Approach:

  • So our first step is to create a spreadsheet on any of your Google accounts and give it an appropriate name, like we can name Google Sheets API Tutorial as shown below with some random entries:

Google Sheets

  • For the next step visit Google Cloud Platform now a page opens like shown below :

Google Cloud Platform

  • Now click on My First Project following dialog box appears, now click on New Project 
  • Now create your project. Now click on API & Services in the side menu bar then go to Library as shown below and search Google Drive and click on Google Drive API:

Click Enable 

  • Now we are going to download a JSON file now which will store our credentials, so once downloaded we will go back to Library and search for Google Sheets API, Enable it, once enabled we are all set to hooking up some stuff with our code so that’s it for our Google Cloud Platform, just make sure to keep track of where the JSON file is because we are actually going to have open that up now.

creds.json

  • Now we are going to copy the client email and then go to Google Sheets we made earlier, go to share options paste that email in it and click send. This allows access to the Google sheet from our API. No we will go back to Pycharm now, and create a python file sheets.py.
  • Now we will go back to Pycharm now, and create a python file sheets.py. Now we are not writing any codes, yet we are going to install two packages or modules with pip so that we can actually use the API, so to do that in Pycharm we just open the inbuilt terminal, or we can open command prompt and type the below command:
pip install gspread oauth2client 
gspread-copy


  • Once you’ve done, you need to create a python script using some modules. Below is the complete program:
Python
# Import required modules import gspread from oauth2client.service_account import ServiceAccountCredentials from pprint import pprint  scope = ["https://spreadsheets.google.com/feeds", 'https://www.googleapis.com/auth/spreadsheets',          "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]    # Assign credentials ann path of style sheet creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope) client = gspread.authorize(creds) sheet = client.open("Google Sheets API Tutorial").sheet1    # display data data = sheet.get_all_records() row4 = sheet.row_values(4) col2 = sheet.col_values(2) cell = sheet.cell(5, 2).value  print("Column 2 Data : ") pprint(col2) print("\nRow 4 Data : ") pprint(row4) print("\nCell (5,2) Data : ") pprint(cell) print("\nAll Records : ") pprint(data)    # Inserting data insertRow = [6, "Soumodeep Naskar", "Purple"] sheet.insert_row(insertRow, 4) print("\nAll Records after inserting new row : ") pprint(data)    # Deleting data sheet.delete_row(7) print("\nAll Records after deleting row 7 : ") pprint(data)    # Update a cell sheet.update_cell(5, 2, "Nitin Das") print("\nAll Records after updating cell (5,2) : ") pprint(data)    # Display no. of rows, columns  # and no. of rows having content numRows = sheet.row_count numCol = sheet.col_count print("Number of Rows : ", numRows) print("Number of Columns : ", numCol) print("Number of Rows having content : ", len(data)) 

Output:



Next Article
How to Show all Columns in the SQLite Database using Python ?
author
rgndunes
Improve
Article Tags :
  • Python
  • python-utility
Practice Tags :
  • python

Similar Reads

  • Store Google Sheets data into SQLite Database using Python
    In this article, we are going to store google sheets data into a database using python. The first step is to enable the API and to create the credentials, so let's get stared. Enabling the APIs and creating the credentialsGo to Marketplace in Cloud Console.Click on ENABLE APIS AND SERVICESThen Searc
    5 min read
  • How to Show all Columns in the SQLite Database using Python ?
    In this article, we will discuss how we can show all columns of a table in the SQLite database from Python using the sqlite3 module.  Approach:Connect to a database using the connect() method.Create a cursor object and use that cursor object created to execute queries in order to create a table and
    3 min read
  • How to import CSV file in SQLite database using Python ?
    In this article, we'll learn how to import data from a CSV file and store it in a table in the SQLite database using Python. You can download the CSV file from here which contains sample data on the name and age of a few students. Approach: Importing necessary modulesRead data from CSV file DictRead
    2 min read
  • How to Automate Google Sheets with Python?
    In this article, we will discuss how to Automate Google Sheets with Python. Pygsheets is a simple python library that can be used to automate Google Sheets through the Google Sheets API. An example use of this library would be to automate the plotting of graphs based on some data in CSV files that w
    4 min read
  • Save API data into CSV format using Python
    In this article, we are going to see how can we fetch data from API and make a CSV file of it, and then we can perform various stuff on it like applying machine learning model data analysis, etc. Sometimes we want to fetch data from our Database Api and train our machine learning model and it was ve
    6 min read
  • Convert any Dates in Spreadsheets using Python
    In this article, we are going to see how to convert any Dates in Spreadsheets using Python. Used file: This file comprises a single column entitled 'Date' and stores random dates of 2021 in some different forms of format. Approach:We'll begin by importing the pandas library.Let's have a look at the
    3 min read
  • PostgreSQL - Connecting to the Database using Python
    PostgreSQL in Python offers a robust solution for developers looking to interact with databases seamlessly. With the psycopg2 tutorial, we can easily connect Python to PostgreSQL, enabling us to perform various database operations efficiently. In this article, we will walk you through the essential
    4 min read
  • How to Import a CSV file into a SQLite database Table using Python?
    In this article, we are going to discuss how to import a CSV file content into an SQLite database table using Python. Approach:At first, we import csv module (to work with csv file) and sqlite3 module (to populate the database table).Then we connect to our geeks database using the sqlite3.connect()
    3 min read
  • SQL using Python | Set 3 (Handling large data)
    It is recommended to go through SQL using Python | Set 1 and SQL using Python and SQLite | Set 2 In the previous articles the records of the database were limited to small size and single tuple. This article will explain how to write & fetch large data from the database using module SQLite3 cove
    4 min read
  • Connecting to SQL Database using SQLAlchemy in Python
    In this article, we will see how to connect to an SQL database using SQLAlchemy in Python. To connect to a SQL database using SQLAlchemy we will require the sqlalchemy library installed in our python environment. It can be installed using pip - !pip install sqlalchemyThe create_engine() method of sq
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences