html日历功课表代码,课程表生成日历.py

from icalendar import Calendar, Event, Alarm

from datetime import datetime, timedelta, date, time

from uuid import uuid1

import xlwings

# 读取课表文件

app = xlwings.App(visible=False, add_book=False)

table = app.books.open('课程表.xlsx')

sheet = table.sheets[0]

classnum = sheet.used_range.last_cell.row-1

classinfo = []

for num in range(classnum):

i = str(num+2)

singleclassinfo = {}

singleclassinfo['classname'] = sheet.range('A'+i).value

singleclassinfo['teacher'] = sheet.range('B'+i).value

singleclassinfo['location'] = sheet.range('C'+i).value

try:

singleclassinfo['classnum'] = sheet.range('D'+i).value.split(',')

except:

singleclassinfo['classnum'] = [sheet.range('D'+i).value]

try:

singleclassinfo['classday'] = sheet.range('E'+i).value.split(',')

except:

singleclassinfo['classday'] = [sheet.range('E'+i).value]

singleclassinfo['weekstart'] = sheet.range('F'+i).value

singleclassinfo['weekend'] = sheet.range('G'+i).value

classinfo.append(singleclassinfo)

table.close()

app.quit()

start_mondey = date(2020, 2, 24)

term_startyear = start_mondey.year

term_startweek = start_mondey.isocalendar()[1]

#timetable = {'morning':[8,0],'afternoon':[9,0],'evenning':[19,0]}

timetable = {'morning1_2': time(8, 0, 0),

'morning3_4': time(10, 10, 10),

'afternoon5_6': time(14, 30, 0),

'afternoon7_8': time(16, 30, 0),

'evenning9_11': time(19, 0, 0)}

# 创建日历

MyCalender = Calendar()

# 添加属性

MyCalender.add('X-WR-CALNAME', '课程表')

MyCalender.add('prodid', '-//My calendar//luan//CN')

MyCalender.add('version', '2.0')

MyCalender.add('METHOD', 'PUBLISH')

MyCalender.add('CALSCALE', 'GREGORIAN') # 历法:公历

MyCalender.add('X-WR-TIMEZONE', 'Asia/Shanghai') # 通用扩展属性,表示时区

# 循环添加课程

for info in classinfo:

for day in info['classday']:

for num in info['classnum']:

num = int(num)

event_uuid = str(uuid1())+'@luan'

event_date = datetime.fromisocalendar(

term_startyear, term_startweek+int(info['weekstart'])-1, int(day))

if num <= 2:

event_start_time = datetime.combine(

event_date, timetable['morning1_2']) + timedelta(minutes=(int(num)-1)*60)

elif num <= 4:

event_start_time = datetime.combine(

event_date, timetable['morning3_4']) + timedelta(minutes=(int(num)-3)*60)

elif num <= 6:

event_start_time = datetime.combine(

event_date, timetable['afternoon5_6']) + timedelta(minutes=(int(num)-5)*60)

elif num <= 8:

event_start_time = datetime.combine(

event_date, timetable['afternoon7_8']) + timedelta(minutes=(int(num)-7)*60)

else:

event_start_time = datetime.combine(

event_date, timetable['evenning9_11']) + timedelta(minutes=(int(num)-9)*60)

event_end_time = event_start_time + timedelta(minutes=50)

# 添加事件

event = Event()

event.add('uid', event_uuid)

event.add('summary', info['classname'])

event.add('dtstart', event_start_time)

event.add('dtend', event_end_time)

event.add('dtstamp', datetime.now())

event.add('location', info['location'])

event.add('description', '授课老师:'+info['teacher'])

event.add('rrule', {'freq': 'weekly',

'interval': 1,

'count': int(info['weekend'])-int(info['weekstart'])+1})

# 编辑提醒

if num in [1, 3, 5, 7, 9]:

alarm = Alarm()

alarm.add('action', 'DISPLAY')

alarm.add('description', 'Reminder')

alarm.add('trigger;related=start', '-PT30M')

event.add_component(alarm)

elif num in [2, 4, 6, 8, 10]:

alarm = Alarm()

alarm.add('action', 'DISPLAY')

alarm.add('description', 'Reminder')

alarm.add('trigger;related=start', '-PT2M')

event.add_component(alarm)

# 将事件添加到日历中

MyCalender.add_component(event)

# 写入文件

with open('课程表.ics', 'wb') as file:

file.write(MyCalender.to_ical().replace(b'\r\n', b'\n').strip())

print('导出完毕!')

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值