【やってみた】PythonでGoogleカレンダーを自動登録する方法|API連携で予定管理

✅この記事でわかること

  • GoogleカレンダーAPIの有効化手順
  • Pythonで予定を自動登録するコード例
  • 毎日のタスク・スケジュール管理を自動化するメリット

「タスク管理を効率化したい」「業務日報を自動でカレンダーに反映させたい」そんな方にピッタリの内容です!


🗓️ GoogleカレンダーAPIとは?

Googleが提供するクラウドベースのスケジューラAPIです。
予定の「取得・追加・更新・削除」がPythonなどのプログラムから簡単に実行できます。

操作概要
insert新しい予定を追加
list予定を一覧取得
delete予定を削除
update予定を変更

🔧 ステップ1:APIキーと認証情報を取得

① Google Cloud Consoleにアクセス

👉 https://console.cloud.google.com/

② 新しいプロジェクトを作成

  1. 上部メニューから「プロジェクトを作成」
  2. 任意の名前でプロジェクト作成

③ Google Calendar API を有効化

  1. 左メニュー「APIとサービス」→「ライブラリ」
  2. 「Google Calendar API」を検索し、有効化

④ OAuth認証情報を作成

  1. 左メニュー「認証情報」→「認証情報を作成」
  2. 「OAuthクライアントID」を選択
  3. アプリの種類:「デスクトップアプリ」
  4. credentials.json をダウンロード

📦 ステップ2:Pythonの環境準備

以下のパッケージをインストール

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

💻 ステップ3:Pythonで予定を登録するコード

✅ コード全体

from __future__ import print_function
import datetime
import os.path
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

# 必要な権限
SCOPES = ['https://www.googleapis.com/auth/calendar']

# 認証とサービス作成
def get_calendar_service():
    creds = None
    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
    if not creds or not creds.valid:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service = build('calendar', 'v3', credentials=creds)
    return service

# 予定を登録
def add_event():
    service = get_calendar_service()

    event = {
        'summary': 'Python自動登録テスト',
        'location': 'オンライン',
        'description': 'Google Calendar API経由で登録',
        'start': {
            'dateTime': '2025-07-15T10:00:00+09:00',
            'timeZone': 'Asia/Tokyo',
        },
        'end': {
            'dateTime': '2025-07-15T11:00:00+09:00',
            'timeZone': 'Asia/Tokyo',
        },
    }

    event = service.events().insert(calendarId='primary', body=event).execute()
    print(f"予定を登録しました: {event.get('htmlLink')}")

add_event()

📌 補足:複数予定を自動化する応用例

ExcelやCSVにタスクを書き出して、自動で複数の予定を登録することも可能です。

import pandas as pd

df = pd.read_csv("schedule.csv")  # タイトル,開始日時,終了日時の列

for _, row in df.iterrows():
    # 各予定をループで登録する処理(上記関数を活用)

✅ まとめ:スケジュール管理を自動化して時短!

GoogleカレンダーAPIを使えば、Pythonスクリプトから予定を一括登録・更新できるようになります。

  • ✔ 毎日のルーチンを自動登録
  • ✔ チーム全体の予定を可視化
  • ✔ Webアプリや通知機能への応用も可能

🔗 関連リンク・おすすめ記事

タイトルとURLをコピーしました