2021年度第4回卒研セミナー(2021/05/06)

関連サイトと資料

icalendarライブラリを用いたicsファイル作成

ical1.py
from icalendar import Calendar, Event
from datetime import datetime
  
if __name__ == '__main__':
    ical = Calendar()
    event = Event()
    event.add('summary', '教室会議')
    #event.add('location', '場所')
    event.add('dtstart', datetime(2021, 5, 12, hour=16, minute=40)) # 日付はdatetimeを使える
    event.add('dtend', datetime(2021, 5, 12, hour=18, minute=40))
    ical.add_component(event)
    ical.add('version', '2.0')
    ical.add('prodid', '-//test//hoge//EN')
  
    with open('sample1.ics', 'wb') as f:
        f.write(ical.to_ical())
    

ical2.py
from icalendar import Calendar, Event
from datetime import datetime
  
if __name__ == '__main__':
    listDates = [ '6/9', '7/7', '9/8', '10/6', '11/10', '12/8', '1/12' ]
  
    ical = Calendar()
  
    for d in listDates:
        month = int(d[:d.find('/')])
        day = int(d[(d.find('/')+1):])
  
        year = 2021
        if month < 4:
            year = 2022
  
        # print(year, month, day)
  
        event = Event()
        event.add('summary', '教室会議')
        event.add('dtstart', datetime(year, month, day, hour=16, minute=40))
        event.add('dtend', datetime(year, month, day, hour=18, minute=40))
  
        ical.add_component(event)
  
    ical.add('version', '2.0')
    ical.add('prodid', '-//test//hoge//EN')
  
    with open('sample2.ics', 'wb') as f:
        f.write(ical.to_ical())
    

mkSchedule.py
from icalendar import Calendar, Event
from datetime import datetime
  
class Schedule:
    def __init__(self):
        self.ical = Calendar()
        self.ical.add('version', '2.0')
        self.ical.add('prodid', '-//test//hoge//EN')
  
    def add(self, year, month, day, h1, m1, h2, m2, title):
        event = Event()
        event.add('summary', title)
        event.add('dtstart', datetime(year, month, day, h1, m1))
        event.add('dtend', datetime(year, month, day, h2, m2))
        self.ical.add_component(event)
  
    def saveAsICS(self, fname):
        with open(fname, 'wb') as f:
            f.write(self.ical.to_ical())
  
if __name__ == '__main__':
    s = Schedule()

    s.add(2021, 6, 9, 11, 30, 13, 0, '健康診断(男性)')
    s.add(2021, 6, 10, 8, 30, 11, 30, '健康診断(男性)')
    s.add(2021, 6, 11, 8, 30, 11, 30, '健康診断(男性)')

    s.saveAsICS('2021健康診断.ics')