0
0

Pythonを使ってOutlookのカレンダーに予定を追加する(9時間補正)

Posted at

Pythonを使ってOutlookのカレンダーに予定を追加するとき、9時間ずれるの直せなかったので、

9時間の時間補正をしたメモ

Outlook デスクトップアプリです。


Pythonを使ってOutlookのカレンダーに予定を追加する

終日の予定を追加する例

まず、「野球」の予定として追加する例から始めましょう。ここでのポイントは、予定の開始時間に日本標準時(JST)から世界協定時(UTC)への9時間の補正を加えることです。

import win32com.client
import datetime

# Outlookのインスタンスを作成
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")

# 予定表フォルダーを取得
calendar_folder = namespace.GetDefaultFolder(9) # 9はカレンダーフォルダー

# 追加したい日付のリスト
dates = [
    "2024-04-29", "2024-05-03", "2024-05-06", "2024-07-15", "2024-08-12",
    "2024-08-13", "2024-08-14", "2024-08-15", "2024-09-16", "2024-09-23",
    "2024-10-14", "2024-11-04", "2024-12-30", "2024-12-31", "2025-01-01",
    "2025-01-02", "2025-01-03", "2025-01-13", "2025-02-11", "2025-02-24", "2025-03-20"
]

# 各日付に対して終日の予定を追加、9時間の補正を加える
for date_str in dates:
    date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
    start_date = date + datetime.timedelta(hours=9)
    end_date = start_date + datetime.timedelta(days=1)

    appointment = calendar_folder.Items.Add("IPM.Appointment")
    appointment.Subject = "野球"
    appointment.AllDayEvent = True
    appointment.Start = start_date
    appointment.End = end_date
    appointment.Save()

    print(f"野球の予定が追加されました:{date_str}")

特定時間の予定を追加する例

次に、「なんかの予定」として特定の時間帯(8:30から17:00)に設定される予定を追加する例を見てみましょう。こちらも同様に、9時間の補正を加えています。

import win32com.client
import datetime

# Outlookのインスタンスを作成
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")

# 予定表フォルダーを取得
calendar_folder = namespace.GetDefaultFolder(9)

# 追加したい予定の日付リスト
dates = [
    (4, 13), (4, 27),
    (5, 11), (5, 25),
    (6, 8), (6, 22),
    (7, 6), (7, 20),
    (8, 3), (8, 17)
]

# 各日付に対して特定時間の

予定を追加9時間の補正を加える
for month, day in dates:
    start_time = datetime.datetime(2024, month, day, 8, 30) + datetime.timedelta(hours=9)
    end_time = datetime.datetime(2024, month, day, 17, 0) + datetime.timedelta(hours=9)

    appointment = calendar_folder.Items.Add("IPM.Appointment")
    appointment.Subject = "なんかの予定"
    appointment.Start = start_time
    appointment.End = end_time
    appointment.Save()

    print(f"なんかの予定が追加されました:{month}/{day} 8:30 - 17:00")

まとめ

Pythonを使ってOutlookの予定を追加することで、日々のタスク管理がより効率的になります。日本で働く私たちにとって、時間の補正は国際的なコミュニケーションをスムーズに行う上で非常に重要です。この記事で紹介した方法を活用して、時間管理の精度を高めましょう。

と、ChatGPTさんは言っています。


補足 pip

PythonでOutlookの予定を操作するために、特にwin32com.clientモジュールを使用している場合、pywin32パッケージをインストールする必要があります。pywin32は、PythonからWindowsのCOM(Component Object Model)技術を利用するためのパッケージです。OutlookのようなWindowsアプリケーションを操作する際に必要となります。

pywin32をインストールするには、以下のコマンドを使用します:

pip install pywin32

このコマンドは、Pythonのパッケージマネージャーであるpipを使用して、pywin32パッケージをインストールします。インストール後は、Pythonスクリプト内でwin32com.clientをインポートして、OutlookなどのWindowsアプリケーションとのインタラクションが可能になります。

なお、pywin32はWindows環境専用のパッケージであり、LinuxやmacOSでは使用できません。Pythonスクリプトを実行する環境がWindowsであることを確認してください。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0