import xlrd
import re
import time
set_termstart='2021-03-01'#设置课程该范围时间的开始,若没有特殊要求,可以设置为学期初
set_termend='2021-07-04'#设置课程该范围时间的结束,若没有特殊要求,可以设置为学期末
set_course='软件工程'#设置需要可安排时间的课程
chat_freetime=''#可以设置星期一晚上、星期二晚上等,不设置默认全部
def basedata():#存储基础数据
#打开课程安排表,打开工作表
data_main=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\主要-开课课程计划.xlsx')
table_main=data_main.sheet_by_name('Sheet2')
#字典存储各课程上课班级
dict_CourseListClass={}
for table_main_row in range(1,table_main.nrows):
dict_CourseListClass[table_main.cell_value(table_main_row,3)]=[]
for table_main_row in range(1,table_main.nrows):
dict_CourseListClass[table_main.cell_value(table_main_row,3)].append(table_main.cell_value(table_main_row,1))
#print(dict_CourseListClass)
#字典存储各课程上课老师
dict_CourseListTeacher={}
for table_main_row in range(1,table_main.nrows):
dict_CourseListTeacher[table_main.cell_value(table_main_row,3)]=[]
for table_main_row in range(1,table_main.nrows):
dict_CourseListTeacher[table_main.cell_value(table_main_row,3)].append(table_main.cell_value(table_main_row,5))
#print(dict_CourseListTeacher)
return dict_CourseListClass,dict_CourseListTeacher
def return_allformday(table_allform_day):#总表日期
day=time.strftime('%Y-%m-%d', time.localtime((table_allform_day-25569) * 86400.0))
allformdayturntime=int(time.mktime(time.strptime(day, "%Y-%m-%d")))
return day,allformdayturntime
def return_setday(time_term):
setdayturntime=int(time.mktime(time.strptime(time_term, "%Y-%m-%d")))
return setdayturntime
def time_count():
#打开课程总表
data_allform=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\安排-课程总表.xlsx')
#字典存储教室可上课日期及时间
dict_timecount={}
list_classroom=['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']
list_weekday=['星期一晚上','星期二晚上','星期三晚上','星期四晚上','星期五晚上','星期六上午','星期六下午1','星期六下午2','星期六晚上','星期日上午','星期日下午1','星期日下午2','星期日晚上']
for num_classroom in list_classroom:
dict_timecount[num_classroom]={}
for num_weekday in list_weekday:
dict_timecount[num_classroom][num_weekday]=[]
#字典存储日期的上课课程
dict_othertimecount={}
#for遍历课程总表,存储可上课时间
for data_allform_sheet in range(1,19):
table_allform=data_allform.sheet_by_name(str(data_allform_sheet))
#for遍历教室
for table_allform_class in range(1,17):
try:
classroom=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
except:
classroom=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
#遍历星期时间段
for table_allform_weekday in range(2,15):
if table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)=='':
dict_timecount[classroom][table_allform.cell_value(2,table_allform_weekday)+table_allform.cell_value(4,table_allform_weekday)].append(return_allformday(table_allform.cell_value(3,table_allform_weekday))[0])
else:
try:
dict_othertimecount[return_allformday(table_allform.cell_value(3,table_allform_weekday))[0]+table_allform.cell_value(4,table_allform_weekday)].append(table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))
except:
dict_othertimecount[return_allformday(table_allform.cell_value(3,table_allform_weekday))[0]+table_allform.cell_value(4,table_allform_weekday)]=[]
dict_othertimecount[return_allformday(table_allform.cell_value(3,table_allform_weekday))[0]+table_allform.cell_value(4,table_allform_weekday)].append(table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))
return dict_timecount,dict_othertimecount
def time_othercount(dict_CourseListClass,CourseListTeacher,timecount):
#打开课程总表
data_allform=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\xxxx大学排课\辅助-课程总表.xlsx')
#for遍历课程总表
for data_allform_sheet in range(1,19):
table_allform=data_allform.sheet_by_name(str(data_allform_sheet))
#for遍历教室
for table_allform_class in range(1,17):
try:
classroom=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
except:
classroom=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
#遍历星期时间段
for table_allform_weekday in range(2,15):
#如果在规定时间内
if return_setday(set_termstart)<=return_allformday(table_allform.cell_value(3,table_allform_weekday))[1] and return_setday(set_termend)>=return_allformday(table_allform.cell_value(3,table_allform_weekday))[1]:
#如果当日有课且不是节假日
if table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)!='' and (('放假' in table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday))==False):
day_eachday=return_allformday(table_allform.cell_value(3,table_allform_weekday))[0]
day_eachcourse=table_allform.cell_value((table_allform_class-1)*2+5,table_allform_weekday)
day_eachweekdayANDeachdaytime=table_allform.cell_value(2,table_allform_weekday)+table_allform.cell_value(4,table_allform_weekday)
try:
day_eachclass=re.findall('\\d\\d\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
except:
day_eachclass=re.findall('机房\\d',table_allform.cell_value((table_allform_class-1)*2+6,0))[0]
#如果两门课的班级和老师不冲突
if list(set(dict_CourseListClass[day_eachcourse]) & set(dict_CourseListClass[set_course]))!=[] or list(set(CourseListTeacher[day_eachcourse]) & set(CourseListTeacher[set_course]))!=[]:
# print(day_eachcourse)
# print(day_eachday)
# print(day_eachweekdayANDeachdaytime)
# print(day_eachclass)
list_class=['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']
for each_class in list_class:
try:
timecount[0][each_class][day_eachweekdayANDeachdaytime].remove(day_eachday)
except:
continue
return timecount[0]
freetime=time_othercount(basedata()[0],basedata()[1],time_count())
#打印可安排时间
for chat_eachclassfreetime in ['401','402','403','机房1','机房2','601','602','603','604','605','606','701','702','703','704','705']:
if chat_freetime!='':
print(chat_eachclassfreetime+' '+chat_freetime)
print(freetime[chat_eachclassfreetime][chat_freetime])
print('')
else:
for chat_eachdayfreetime in ['星期一晚上','星期二晚上','星期三晚上','星期四晚上','星期五晚上','星期六上午','星期六下午1','星期六下午2','星期六晚上','星期日上午','星期日下午1','星期日下午2','星期日晚上']:
print(chat_eachclassfreetime+' '+chat_eachdayfreetime)
print(freetime[chat_eachclassfreetime][chat_eachdayfreetime])
print('')
#打印已安排课程
#print(time_count()[1])