Python で Google SpreadSheets を CSV にエクスポート

OAuth2 を含めた gspread の説明は公式のリファレンス(👉 1、👉 2)が参考になる。

また、CSV へのエクスポートについては Stack Overflow の記事(👉 Saving a google spreadsheet as a csv)を参考にした。


from oauth2client.service_account import ServiceAccountCredentials
import gspread
import csv

JSON_KEYFILE = 'XXX.json'
SPREADSHEET = 'テスト'
WORKSHEET = 'sheet1'
CSV_FILENAME = 'test.csv'

SCOPE = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
serviceAccountCredentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEYFILE, SCOPE)
gspreadClient = gspread.authorize(serviceAccountCredentials)
worksheet = gspreadClient.open(SPREADSHEET).worksheet(WORKSHEET)

with open(CSV_FILENAME, 'w', newline='') as csvFile:
    writer = csv.writer(csvFile)
    writer.writerows(worksheet.get_all_values())

コメント

このブログの人気の投稿

清水俊史『上座部仏教における聖典論の研究』

シークエンスパパともの先見の明

シークエンスパパとも 本物の霊能力