Write to Google Sheet Python

This blog article shows you one of the possible methods to write data into Google Sheet.

1. You go to Google API Console to enable Google Sheet API.

2. Create a KEY then download the Json file for the KEY, you realize I have this like in my code later.

creds = ServiceAccountCredentials.from_json_keyfile_name(‘myprojectiscc-e22bd9284a10.json’, scope)

image

3. Share the Google Sheet with the Service Account

  • Open your Google Sheet.
  • Click the Share button.
  • Add the service account email (found in the JSON key file under client_email) as an Editor or Viewer.
  • Example service account email: your-service-account@your-project.iam.gserviceaccount.com.

Also: Where to get the ClientId and ClientSecret for Google API

image

You can then use the code snippet below to test writing to Google Sheet.

import pandas as pd

import gspread

from oauth2client.service_account import ServiceAccountCredentials

# Define the scope

scope = [“https://spreadsheets.google.com/feeds”, “https://www.googleapis.com/auth/drive”, ‘https://www.googleapis.com/auth/spreadsheets’%5D

# Add credentials to the account

creds = ServiceAccountCredentials.from_json_keyfile_name(‘myprojectiscc-e22bd9284a10.json’, scope)

# Authorize the clientsheet

client = gspread.authorize(creds)

# Get the instance of the Spreadsheet

sheet = client.open_by_url(“https://docs.google.com/spreadsheets/d/130Npclfy9nPsXRihiczaUmW6-hza21bwfGfVa064sDo/edit”)

# Get the first sheet of the Spreadsheet

worksheet = sheet.get_worksheet(0)

# Read CSV file into DataFrame

df = pd.read_csv(‘data.csv’)

# Write DataFrame to Google Sheet

worksheet.update([df.columns.values.tolist()] + df.values.tolist())

Source code download: https://github.com/chanmmn/python/tree/main/2025/WriteGoogleSheet?WT.mc_id=DP-MVP-36769

Reference: https://docs.gspread.org/en/v6.1.3/?WT.mc_id=DP-MVP-36769

Unknown's avatar

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net, Cloud, Community, Computers and Internet, programming and tagged . Bookmark the permalink.

2 Responses to Write to Google Sheet Python

  1. Pingback: Python in Ubuntu | Chanmingman's Blog

  2. Pingback: Read Top 100 rows from CSV File Python | Chanmingman's Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.