共有設定
次のようにして得られる Eメールアドレスを加えます。
$ jq . service_account.json | grep client_email
"client_email": "sss-aaa@project-nov15-2024.iam.gserviceaccount.com",
Google Drive API を使えるようにする
プログラム
list_spread.py
#! /usr/bin/python
#
# list_spread.py
#
# Nov/29/2024
# ------------------------------------------------------------------
import sys
import os
# pipx でインストールされたパッケージのパスを取得
package_path = os.path.expanduser('~/.local/share/pipx/venvs/gspread/lib/python3.12/site-packages')
sys.path.append(package_path)
package_path = os.path.expanduser('~/.local/share/pipx/venvs/gspread-dataframe/lib/python3.12/site-packages')
sys.path.append(package_path)
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
from gspread_dataframe import get_as_dataframe, set_with_dataframe
# ------------------------------------------------------------------
credentials = ServiceAccountCredentials.from_json_keyfile_name('./service_account.json')
#
client = gspread.authorize(credentials)
spreadsheets = client.openall()
for spreadsheet in spreadsheets:
print(f"タイトル: {spreadsheet.title}, ID: {spreadsheet.id}")
#
# ------------------------------------------------------------------
実行結果
アクセス権のあるものが、すべて表示されます。
$ ./list_spread.py
タイトル: 豊田小_2024-10-08, ID: 1TKKGXSYzpWj3ekaQMfYtMiQtPDc1ebQ
タイトル: 羽川小_20241106, ID: 1eK0hZ_kI6FO2J2RZ94hizP7VZ1lkYObX5N
タイトル: 都市の人口, ID: 1D1hk5LH6O3BlFehaBgnzfGtbcra35BX0r9niclA
タイトル: nov26_aa, ID: 14MdjJ3Hvna9ynheAphHozcW_k1JNQjjtkOtvu
タイトル: 絹義務小_2024-06-05, ID: 1hygAKaT2Jpmi7fa1yiRa0mYClOxRr99
タイトル: 絹義務小_2024-11-12, ID: 1rW5RphB5UGAXxN9SJMy3HqgT5T9hO
タイトル: 絹義務小_2024-09-25, ID: 1cGr-uoEa-kjHGLi5u9OfKcdYWrM
タイトル: 豊田小_2024-06-18, ID: 1DBwZHtrPQ28wjCu5Mp5DtlVbcvSo9pkN
タイトル: 間々田小_2024-10-23, ID: 1JWd7zaxidv5uGEKuMndgwGjVk3U3U07
タイトル: 間々田小_2024-06-19, ID: 1kGVQR1db7UnFc-fWygdKleB5tKwIPi
タイトル: 乙女中_2024-10-02, ID: 1ut0lZdayQ9R3Dc28-JLLNvAQvZTf8
タイトル: 乙女中_2024-07-02, ID: 1Ak2rJG-_nnusmr_br2gGOw-w36pcx
タイトル: 大谷中_2024-10-31, ID: 1Rd3F5mcC7AEs6dPDXn3GlyG1R0n