投稿

ラベル(GoogleAPI)が付いた投稿を表示しています

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())