Until you insert data into a spreadsheet in Python

I wanted to operate google spreadsheet from python, so I summarized the procedure to authenticate google and add it to the cell.

Create a service account

Create a google apis service account by using the URL below. Authenticate with service account and get data from Google Spreadsheet

Install module

Install the modules ʻoauth2clientandgspread` for google api authentication and spreadsheet manipulation.

$ pip3 install oauth2client
$ pip3 install gspread

Creating a class for authentication

Create an intersection class so that it can be used by multiple classes.

google_service_account.py


import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']

credentials = ServiceAccountCredentials.from_json_keyfile_name('path/to/google-service-account.json'), scope)
client = gspread.authorize(credentials)

Create a spreadsheet insert function

google_spreadsheet_service.py


from datetime import date
from google_service_account import client

class GoogleSpreadsheetService:
    @classmethod
    def edit_cells(self):
        spreadsheet = client.open('sample') #Specify the name of the spreadsheet you want to work with
        worksheet = spreadsheet.worksheet('Annual bonuses') #Specify the sheet
        data = ['{0:%Y-%m-%d}'.format(date.today()), Hello, World] #Data you want to insert
        worksheet.append_row(data) #Insert data in last line

All you have to do is call GoogleSpreadsheetService.edit_cells () when you want to insert it.

bonus

I formatted it according to the Date format so that it would be treated as a date, but the inserted date data was treated as just a character string.

before


data = ['{0:%Y-%m-%d}'.format(date.today()), Hello, World]
worksheet.append_row(data) #It will be treated as just a character string

This was avoided by specifying value_input_option ='USER_ENTERED'.

after


data = ['{0:%Y-%m-%d}'.format(date.today()), Hello, World]
worksheet.append_row(data, value_input_option='USER_ENTERED')

Recommended Posts

Until you insert data into a spreadsheet in Python
Until you put Python in Docker
Python --How do you split a list into evenly sized chunks in Python?
Until you run the changefinder sample in python
Data analysis in Python: A note about line_profiler
Until you create a new app in Django
A well-prepared record of data analysis in Python
Insert an object inside a string in Python
Until drawing a 3D graph in Python on windows10
BigQuery-If you get a Reason: responseTooLarge error in Python
If you encounter a "Unicode Decode Error" in Python
A Python program that converts ical data into text
Take a screenshot in Python
Handle Ambient data in Python
Until you get daily data for multiple years of Japanese stocks and save it in a single CSV (Python)
Create a function in Python
Create a dictionary in Python
Display UTM-30LX data in Python
[Python] Insert ":" in MAC address
Make a bookmarklet in Python
Draw a heart in Python
Why do you add a main ()-if statement in Python?
A function that divides iterable into N pieces in Python
Published a library that hides character data in Python images
Make a table of multiplication of each element in a spreadsheet (Python)
How to import a file anywhere you like in Python
Get Leap Motion data in Python.
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
A simple data analysis of Bitcoin provided by CoinMetrics in Python
Read Protocol Buffers data in Python3
Get data from Quandl in Python
How to make a string into an array or an array into a string in Python
Create a DI Container in Python
Make multiple numerical elevation data into a single picture with Python
Do you need a Python re.compile?
ABC166 in Python A ~ C problem
Handle NetCDF format data in Python
Until you start crawling in Scrapy
Write A * (A-star) algorithm in Python
Create a binary file in Python
Split iterator into chunks in python
Hashing data in R and Python
Solve ABC036 A ~ C in Python
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Check if you can connect to a TCP port in Python
Until you run python with apache
Solve ABC037 A ~ C in Python
Until dealing with python in Atom
Until you put pyaudio in Mavericks
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
[Introduction] Insert line breaks in Python 3
Schedule a Zoom meeting in Python