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 SheetsFor the next step visit Google Cloud Platform now a page opens like shown below :Google Cloud PlatformNow 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.jsonNow 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 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: Comment More infoAdvertise with us Next Article Using Google Sheets as Database in Python rgndunes Follow Improve Article Tags : Python Web Technologies 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 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. Contents of the CSV file Approach: Importing necessary modulesRead da 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 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 How to Automate an Excel Sheet in Python? Before you read this article and learn automation in Python, let's watch a video of Christian Genco (a talented programmer and an entrepreneur) explaining the importance of coding by taking the example of automation.You might have laughed loudly after watching this video, and you surely might have u 8 min read Export Data From Mysql to Excel Sheet Using Python We are given a MySQL database and our task is to export the data into an excel sheet using Python. In this article, we will see how to export data from MySQL to Excel Sheets using Python. Export Data From Mysql to Excel Sheet Using PythonBelow are some of the ways by which we can export data from My 3 min read Inserting variables to database table using Python In this article, we will see how one can insert the user data using variables. Here, we are using the sqlite module to work on a database but before that, we need to import that package.  import sqlite3 To see the operation on a database level just download the SQLite browser database.Note: For the 3 min read Making a Flask app using a PostgreSQL database The Postgres database can be accessed via one of two methods in Python. Installing PgAdmin4 is the first step because it offers a user interface for interacting with databases and another for using the psycopg2 connector. In this post, we'll concentrate on a different approach that lets us alter the 4 min read Fetch JSON URL Data and Store in Excel using Python In this article, we will learn how to fetch the JSON data from a URL using Python, parse it, and store it in an Excel file. We will use the Requests library to fetch the JSON data and Pandas to handle the data manipulation and export it to Excel.Fetch JSON data from URL and store it in an Excel file 3 min read How to get sheet names using openpyxl - Python The openpyxl library is widely used to handle excel files using Python. Often when dealing with excel files one might have noticed multiple sheets. The sheets are normally mentioned in the bottom left corner of the screen. One can install this library by running the following command. pip install op 2 min read Like