BM Balmiki Mandal 02 May 2023 0 views 0 comments Python

Read Data From Google Sheets Using Python with source code

Read Data From Google Sheets Using Python

Google Sheets is a great way to store and manage tabular data. With the help of Python, you can easily read data from Google Sheets. This tutorial will show you how to read data from Google Sheets with Python.

Prerequisites

  • Python
  • Google account with access to Google Sheets
  • Google Sheets API library for Python
  • Google Sheets ID

Step 1: Install the Google Sheets API Library

The first step is to install the Google Sheets API library. The library can be installed using pip:

pip install --upgrade google-api-python-client

Step 2: Enable the Google Sheets API

The next step is to enable the Google Sheets API. This can be done by going to the Google APIs Console and clicking on the “Enable APIs and Services” button.

You will then need to search for the “Google Sheets API” service and click on the “Enable” button.

Step 3: Create a Service Account

Next, you need to create a Service Account. A Service Account is an account that belongs to your application instead of an individual user. It is used to authenticate your application so that it can access the Google Sheets API. To create the Service Account, go to the Google APIs Console and click on the “Create credentials” button to create a new Service Account.

Step 4: Get Your Google Sheets ID

The next step is to get your Google Sheets ID. The ID can be found in the URL of the spreadsheet. For example, if the URL is “https://docs.google.com/spreadsheets/d/1V4OMRBXz46J_RjFS5m0SVfZ3qL5Y0Pw8aJgA_ABoIzM/edit#gid=0”, then the ID is “1V4OMRBXz46J_RjFS5m0SVfZ3qL5Y0Pw8aJgA_ABoIzM”.

Step 5: Connect to the Google Sheets with Python

Once you have your Service Account and your Google Sheets ID, you can use Python to connect to the spreadsheet. To do this, first import the Google Sheets API library and create a service instance:

from googleapiclient.discovery import build

service = build('sheets', 'v4')

The build() method creates a Sheets service object which is used to interact with the Google Sheets API.

Then, you can use the Sheets.spreadsheets().values().get() method to get the data from the sheet:

spreadsheet_id = '1V4OMRBXz46J_RjFS5m0SVfZ3qL5Y0Pw8aJgA_ABoIzM'
range_name = 'Sheet1!A1:E5'
result = service.spreadsheets().values().get(
    spreadsheetId=spreadsheet_id, range=range_name).execute()
values = result.get('values', [])

if not values:
    print('No data found.')
else:
    print('Name, Major:')
    for row in values:
        # Print columns A and E, which correspond to indices 0 and 4.
        print('%s, %s' % (row[0], row[4]))

The code above reads data from the Google Sheet and prints it to the console.

Conclusion

In this tutorial, you learned how to read data from Google Sheets with Python. The process involves installing the Google Sheets API library, enabling the API, creating a Service Account and getting the Google Sheets ID. You can then use Python to connect to the sheet and read the data.

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

What is an Application Programming Interface (API)?

What is an API? An API (Application Program Interface) is a set of protocols, tools, and subroutines used for...

BM Balmiki Mandal 06 Jan 2024
Understand the Difference Between Interpreted and Compiled Stage

What is the Difference Between Interpreted and Compiled Programming Languages? Compiled and interpreted langu...

BM Balmiki Mandal 28 Nov 2023
Executing Python Code: A Comprehensive Guide to Source Code Execution

Exploring the Internal Mechanisms of Python Programming Python is an interpreted language, which means it doe...

BM Balmiki Mandal 28 Nov 2023