昔の記事
Python3でGoogle SpreadsheetをDBのように利用する
環境等
- Python3
- Ubuntu 14.04
利用ライブラリ
手順概要
やることはシンプルです。
- Google API ConsoleからGoole Sheets APIを有効化し、JSONを取得する
- 読み書きをしたいドキュメントの共有設定を行う
- コードからデータを取得する
詳細はPython3でGoogle SpreadsheetをDBのように利用するとあまり変わらないので割愛します。
コード
コードは以前と一部変わりました。
# -*- coding:utf-8 -*-
import gspread
from oauth2client.service_account import ServiceAccountCredentials
def get_spreadsheet_data():
doc_id = '[google spreadsheetのdocument id]'
json_key_path = './google-creds.json'
scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_path, scope)
gclient = gspread.authorize(credentials)
gfile = gclient.open_by_key(doc_id)
wsheet = gfile.get_worksheet(0)
records = wsheet.get_all_records(head=1)
return records
print(get_spreadsheet_data())
以上です。