Outlookカレンダーに、CSVから予定を追加する

■ Outlook カレンダーへの予定追加

def insert_outlook_events(calendar_address, calendar_name, work_file, dry_run=True, max_items=500):
    """
    指定されたOutlookカレンダーに、CSVから条件に合う予定を追加する
    """
    outlook = win32com.client.Dispatch("Outlook.Application")
    namespace = outlook.GetNamespace("MAPI")
    calendar_folder = namespace.Folders[calendar_address].Folders[calendar_name]

    JST = pytz.timezone("Asia/Tokyo")
    now = datetime.now(JST)

    # 削除対象を判定
    add_events_from_csv(work_file, calendar_folder, now, dry_run, max_items)

■ Outlook カレンダーから、件名が条件に合う且つ今日以降の予定を削除

def delete_jibai_events(account_name: str, calendar_name: str, dry_run: bool = True, max_items: int = 500):
    """
    指定されたOutlookカレンダーから、件名が条件に合
    今日以降の予定を削除するdry_run=Trueなら削除せず表示のみ
    """
    outlook = win32com.client.Dispatch("Outlook.Application")
    namespace = outlook.GetNamespace("MAPI")
    calendar_folder = namespace.Folders[account_name].Folders[calendar_name]

    JST = pytz.timezone("Asia/Tokyo")
    now = datetime.now(JST)
    #now_utc = datetime.now(timezone.utc)
    restriction = f"[Start] >= '{now.strftime('%m/%d/%Y %I:%M %p')}'"
    print(f"使用中のフィルター: {restriction}")

    items = calendar_folder.Items
    #items.IncludeRecurrences = True # 繰り返し予定が全部展開
    items.IncludeRecurrences = False
    items.Sort("[Start]")
    filtered_items = items.Restrict(restriction)

    prefixes = ("件名条件")
    delete_count = 0
    scanned = 0

    for item in filtered_items:
        try:
            subject = item.Subject
            if subject.startswith(prefixes):
                print(f"{'[削除予定]' if dry_run else '[削除実行]'} {subject} ({item.Start})")
                if not dry_run:
                    item.Delete()
                delete_count += 1

            scanned += 1
            if scanned >= max_items:
                print(f"⚠️ 最大処理件数 {max_items} 件に達したため中断しました。")
                break
        except Exception as e:
            print(f"削除時エラー: {e}")

    print(f"対象件数: {delete_count} 件(スキャン数: {scanned} 件)")

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

上部へスクロール