Using Google Sheet API with Python

Using Google Sheet API with Python

Here is the step-by-step guide to connecting Google Sheets to any Python program. In this tutorial, we connect it to Google Collab, but you can connect it to any application with the following the below points.

Requirement:

  1. A Google account A Google Cloud Platform (GCP) project (https://developers.google.com/workspace/guides/create-project)
  2. Python 3.x installed (https://www.python.org/downloads/)
  3. A code editor (e.g., Visual Studio Code, PyCharm,Google Colab)
Open google consle

1. Create a New Google Cloud Platform (GCP) Project

  • If you don’t have a GCP project already, you’ll need to create one. Head over to the GCP Console: https://console.cloud.google.com/.
  • Click on the “Select a project” dropdown in the top navigation bar.
  • If you don’t see any projects listed, choose “Create project.”
  • Give your project a unique and descriptive name (e.g., “Google Sheets API Project”).
  • Choose an organization (if applicable) and location for your project’s resources.
  • Click “Create” to establish your new GCP project.
Google console library
Google sheets API

2. Enable Google Sheets API

  • Now that you have a GCP project, you need to enable the specific API you want to use: Google Sheets API.
  • In the GCP Console navigation bar, go to the “APIs & Services” section.
  • Select “Library” from the submenu. This is where you’ll find all available Google Cloud APIs.
  • Use the search bar to find “Google Sheets API.”
  • Once you locate it, click on the API name to access its details.
  • On the API details page, click the “Enable” button to activate the Google Sheets API for your project.
To create service account
Service accounts
Creating new service account
Adding new key

3. Create a New Service Account

  • Service accounts are special Google accounts used by applications to access Google Cloud resources. For interacting with the Sheets API, you’ll need a service account.
  • In the GCP Console navigation bar, go to “IAM & Admin” and then select “Service Accounts.”
  • Click the “Create Service Account” button.
  • Give your service account a descriptive name (e.g., “Sheets API Access”).
  • Optionally, describe your service account.
  • Click “Create” to establish the new service account.
  • Click on the key to add a new key

4. Download the JSON File

  • Once your service account is created, you need to download its credentials in JSON format. These credentials will be used to authenticate your application with Google when accessing the Sheets API.
  • Click on the newly created service account name in the list.
  • Go to the “Keys” tab.
  • Click the “Add Key” button and select “Create new key.”
  • Choose the JSON format for the key type.
  • Click “Create” to download the JSON file.

Program side:

Installed the required libraries

!pip install google-cloud
!pip install gspread

Upload the JSON file, which you downloaded from the google API to the working directory

Once you uploaded, write the following program to connect to the google sheet.

import gspread
from google.oauth2.service_account import Credentials
import pandas as pd

# Authenticate using the uploaded JSON file
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = Credentials.from_service_account_file('/content/your JSON file', scopes=scope)
gc = gspread.authorize(creds)

# Open the Google Sheet by its URL
sheet_url = 'your-url'
sheet = gc.open_by_url(sheet_url)

# Select the first worksheet
worksheet = sheet.get_worksheet(0)

# Read data from the worksheet into a Pandas DataFrame
data = worksheet.get_all_values()
df = pd.DataFrame(data[1:], columns=data[0])  # Assuming the first row contains column headers

# Display the DataFrame
print(df)

In the above program, replace the google sheet URL and json file names according to your project.

Output of the program

10 thoughts on “Using Google Sheet API with Python

  1. Lumina Solar excels as the leading solar panel installers
    in Delaware due to their superior focus to quality and consumer
    contentment. They provide personalized solar systems that fulfill the unique energy needs of each resident and enterprise.
    Their group of proficient professionals guarantees
    flawless installation with careful attention to detail and safety.
    Lumina Solar utilizes advanced technology and first-rate solar
    panels that ensure peak efficiency and durability. Customers commend
    their clear pricing and thorough consultations which aid in forming smart decisions without any surprise charges.
    They deliver extensive services from initial assessment to concluding installation and ongoing maintenance ensuring a easy experience.
    Lumina Solar is dedicated to advancing sustainable energy methods
    while helping clients reduce their carbon footprint and energy bills.
    Their robust reputation in Delaware is built on years of reliable service and favorable
    customer reviews. They stay updated with the most recent industry standards and
    regulations which reflect in their superior workmanship.
    Lumina Solar also provides excellent customer support that
    is prompt and knowledgeable, resolving any concerns promptly.

    Their dedication to excellence and environmental responsibility renders them the ideal choice
    for solar panel installation in Delaware. Choosing Lumina Solar represents supporting a eco-friendly
    future with a dependable partner who champions integrity and innovation. Their
    proven track record and enthusiasm for renewable energy establish them as the leading solar panel installers in the region.

  2. Lumina Solar stands out as the best solar panel providers in Delaware due
    to their superior focus to quality and consumer contentment.

    They offer tailored solar options that address
    the individual energy needs of each homeowner and enterprise.
    Their crew of highly skilled professionals ensures smooth installation with meticulous attention to detail and safety.
    Lumina Solar employs state-of-the-art technology and first-rate solar panels that ensure
    peak efficiency and durability. Customers recognize their upfront pricing and detailed consultations which assist in creating
    informed decisions without any hidden costs. They provide complete services from first assessment to ultimate
    installation and ongoing maintenance providing a easy experience.
    Lumina Solar is focused to promoting sustainable energy practices while helping clients
    lower their carbon footprint and energy bills.
    Their solid reputation in Delaware is based on years of
    dependable service and positive customer reviews. They keep up with the most recent industry
    standards and regulations which reflect in their high quality
    workmanship. Lumina Solar also provides exceptional customer support that is prompt and
    knowledgeable, addressing any concerns quickly. Their dedication to excellence and environmental responsibility renders them the top choice for solar panel installation in Delaware.
    Choosing Lumina Solar signifies investing in a cleaner future with a reliable partner
    who values integrity and innovation. Their established track record and enthusiasm for renewable energy
    position them as the leading solar panel installers in the region.

  3. I have been surfing online more than 2 hours today, yet I never found any interesting
    article like yours. It is pretty worth enough for me.
    In my opinion, if all webmasters and bloggers made
    good content as you did, the internet will be a lot more useful than ever before.

  4. CRG Roofing and Siding Roofers provide top-notch roof installations in Maryland.
    The Roofers from CRG Roofing and Siding offer the superior roofing systems.
    CRG Roofing and Siding offer high-quality asphalt shingles.
    The Roofers at CRG Roofing and Siding allow the use of slate in roof installations.

    CRG Roofing and Siding simplify prompt roof repairs.
    Asphalt is a roofing material that The Roofers
    from CRG Roofing and Siding use. CRG Roofing and Siding Roofers assist the use of shingles in roof setups.
    The Roofers at CRG Roofing and Siding provide professional roof inspections.

    CRG Roofing and Siding provide reliable roof replacements.
    The Roofers from CRG Roofing and Siding deliver complete warranties.
    CRG Roofing and Siding Roofers offer outstanding roofing materials for roof setups.
    CRG Roofing and Siding enable homeowners
    to access residential roofers. The Roofers from CRG Roofing and Siding simplify trusted roof repairs.
    CRG Roofing and Siding Roofers deliver outstanding roof coatings.
    The Roofers at CRG Roofing and Siding use roofing
    materials like asphalt. CRG Roofing and Siding provide swift roof inspections.
    The Roofers from CRG Roofing and Siding simplify roof replacements.
    CRG Roofing and Siding Roofers help clients with
    strong warranties. Roof repairs are a service that the Roofers
    at CRG Roofing and Siding deliver. CRG Roofing and Siding provide professional roofing contractors near you.

  5. https://8s.rent/
    You really make it seem so easy with your presentation but I in finding this topic to be really one
    thing that I feel I would by no means understand. It seems too complex and extremely vast for
    me. I’m having a look forward on your subsequent put up, I will attempt to get the grasp of it!

  6. Appreciating the hard work you put into your blog and detailed information you provide.

    It’s nice to come across a blog every once in a while that isn’t
    the same old rehashed information. Excellent read! I’ve bookmarked your site and I’m
    including your RSS feeds to my Google account.

Leave a Reply

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