사용자 인증 정보 → 서비스 계정 → 이메일 복사 → 관리할 스프레드 시트의 편집자로 초대

Untitled

서비스계정의 이메일을 클릭해 들어와 키 설정

Untitled

새 키 만들기 → JSON으로 다운로드

pip3 install gspread
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# set credentials and authorize
scope = ['<https://spreadsheets.google.com/feeds>', '<https://www.googleapis.com/auth/drive>']
creds = ServiceAccountCredentials.from_json_keyfile_name('<credentials_file_path>', scope)
client = gspread.authorize(creds)

# open the Google Sheet
sheet = client.open('<sheet_name>').sheet1

# define the data you want to write
data = [
  ['Name', 'Age', 'Gender'],
  ['John', '30', 'Male'],
  ['Jane', '25', 'Female']
]

# write the data to the sheet
sheet.insert_rows(data, row=1)

<credentials_file_path>와 <sheet_name>를 채운 후 위 코드를 실행하면 설정한대로 데이터가 입력되는 것을 확인할 수 있다.