🗂

Outlookとgantt

2023/04/21に公開

import win32com.client
import datetime

def get_outlook_calendar_events(start_date, end_date):
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
calendar = namespace.getDefaultFolder(9)
appointments = calendar.Items
appointments.IncludeRecurrences = True
appointments.Sort("[Start]")
appointments = appointments.Restrict("[Start] >= '" + start_date.strftime('%m/%d/%Y %H:%M %p') + "' AND [END] <= '" + end_date.strftime('%m/%d/%Y %H:%M %p') + "'")

events = []
for appointment in appointments:
    event = {}
    event['name'] = appointment.Subject
    event['start'] = appointment.Start
    event['end'] = appointment.End
    events.append(event)

return events

def convert_to_mermaid_gantt(events):
gantt = "gantt\n"
gantt += "dateFormat YYYY-MM-DD HH:mm\n"
gantt += "title Outlook Calendar Events\n"
gantt += "excludes weekdays\n"
gantt += "section Default\n"

for event in events:
    start = event['start'].strftime('%Y-%m-%d %H:%M')
    end = event['end'].strftime('%Y-%m-%d %H:%M')
    name = event['name']
    gantt += name + " : " + start + ", " + end + "\n"

return gantt

start_date = datetime.datetime.now()
end_date = start_date + datetime.timedelta(days=7)
events = get_outlook_calendar_events(start_date, end_date)
gantt = convert_to_mermaid_gantt(events)
print(gantt)

Discussion