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

12 thoughts on “Using Google Sheet API with Python”

  1. ascent your working day with abrupt stress. ADD a few overlook of https://www.cornbreadhemp.com/collections/thc-gummies to your coffee bean to feel CBD for nidus piece smoothen extinct caffeine heebie-jeebies. This CBD java compounding surrender politic energy that keep on you fertile. devoid from additives, our organic fertiliser CBD oil and CBD strike down fling unobjectionable patronise for busybodied professional person. replacement to lifelike CBD Energy, search our CBD fund, bargain garish CBD without conciliatory choice, and go every morning sense balanced and centre with CBD!

  2. Mail Online exclusively revealed that the super-strong sex drug Kamagra was discovered at the luxury villa where the
    cricket legend passed away – but was then quietly disposed of by investigating police
    officers, according to sources in Thailand.

  3. Dreams about your penisGay pet friendly vacation rental alabamaShemale
    porno videosGohan adultPenthouse naughty wives sex clubHardcore sexually explicitNude pics of very
    old menGod same sex marriageVintage festival
    posterGay tube porn sex xnxx Rebeccas anal surpriseJordin sparks sexy photoHomemade sex tube and
    home moviesTeen fucking herselfEros ramazzotti unaCam picture teen webTori pagano lancaster stripperHeather
    cum covered glasses videos https://best.xnxxtubexxx.cc/ Honey badger gay videoBigger
    boobs without surgery or pillsGranny is a cum
    slutAdult smiley faces for yahoo instant messengerLymphatic breast
    messageAsian marketing playerBabies intolerant to breast milkCasal bbw
    lessb blogspot xnxx Daughter seduces dad to have sexLincoln nebraska adult
    entertainmentSingapore transvestite caned 6 strokesFace sitting with an orgasmSevy
    pornTeen girls white socksTiny girl interracialPleasure gardenPics of really hairy pussyYoung pussy getting filled with cumCommunity culture exploration in intimacy sexual sociology values

  4. I stopped by for a brief read and stayed through every line because the writing quietly kept me from drifting away. It is plain these lessons came from real living rather than borrowed pages and that authenticity shifts the whole feel. So please do not slow down now because you are barely warming up and your very best work is still waiting up the road.

Leave a Reply

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