インストール
pip install gspread pip install --upgrade google-api-python-client
以下実行
# -*- coding:utf-8 -*-
import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
def main():
doc_id = '[スプレッドシートのURLからコピーした文字列]'
# 先ほどDLしたJSONをロード
json_key = json.load(open('[ダウンロードしたJSONへのパス]'))
scope = ['https://spreadsheets.google.com/feeds']
# credentialsを取得
credentials = SignedJwtAssertionCredentials(
json_key['client_email'],
json_key['private_key'].encode(),
scope)
gclient = gspread.authorize(credentials)
gfile = gclient.open_by_key(doc_id)
wsheet = gfile.get_worksheet(0) # シートのindexを任意で入力
records = wsheet.get_all_records() # head=1を指定すると便利
for record in records:
print(record)
if __name__ == '__main__':
main()
その他設定は、参考を。
参考:
http://wwld.jp/2015/11/07/spreadsheet-api.html


