本来今天是要研究生选课的,但是西交的选课系统是这样的:
每个课都这么看下来,感觉头都大了。。。遂产生了一个将能选的课可视化到课程表里的念头。程序写的有些急,有些地方也没多想,导致又臭又长的,但是搞得能用吧。
要用到xlutils这个包,本质是将读取的表格copy到另一个表格上,然后在后者上进行修改,修改后再覆盖前者。以下是一个对excel进行追加写入的例子(读取原表格,找到具有两个“班”字以上的单元格,对其进行标蓝)
style = xlwt.easyxf('pattern: pattern solid, fore_colour light_blue;'
'font: colour white, bold True;')
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
xlsc = copy(bk1)
shtc = xlsc.get_sheet(0)
for i in range(0,219):
for j in range(0,7):
wordNum=0
addStr = sh1.cell(i, j).value
addStrNum=len(addStr)
for k in range(addStrNum):
if(addStr[k]=='班'):
wordNum=wordNum+1
if(wordNum>=2):
shtc.write(i, j, addStr,style)
xlsc.save('./chooseEnd.xls')
import xlwt
import xlrd2
from xlutils.copy import copy
def createXls():
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet('sheet1', cell_overwrite_ok=True)
for i in range(1, 21):
str1 = '第' + str(i) + '周'
sheet1.write((i - 1) * 13, 0, str1)
sheet1.write((i - 1) * 13, 1, '星期一')
sheet1.write((i - 1) * 13, 2, '星期二')
sheet1.write((i - 1) * 13, 3, '星期三')
sheet1.write((i - 1) * 13, 4, '星期四')
sheet1.write((i - 1) * 13, 5, '星期五')
sheet1.write((i - 1) * 13, 6, '星期六')
sheet1.write((i - 1) * 13, 7, '星期日')
sheet1.write((i - 1) * 13 + 1, 0, '上午1')
sheet1.write((i - 1) * 13 + 2, 0, '上午2')
sheet1.write((i - 1) * 13 + 3, 0, '上午3')
sheet1.write((i - 1) * 13 + 4, 0, '上午4')
sheet1.write((i - 1) * 13 + 5, 0, '下午5')
sheet1.write((i - 1) * 13 + 6, 0, '下午6')
sheet1.write((i - 1) * 13 + 7, 0, '下午7')
sheet1.write((i - 1) * 13 + 8, 0, '下午8')
sheet1.write((i - 1) * 13 + 9, 0, '晚上9')
sheet1.write((i - 1) * 13 + 10, 0, '晚上10')
sheet1.write((i - 1) * 13 + 11, 0, '晚上11')
workbook.save('./chooseEnd.xls')
def stringToInt(num):
if num=="0":
return int(0)
elif num=="1" or num=="一":
return int(1)
elif num=="2" or num=="二":
return int(2)
elif num=="3" or num=="三":
return int(3)
elif num=="4" or num=="四":
return int(4)
elif num=="5" or num=="五":
return int(5)
elif num=="6" or num=="六":
return int(6)
elif num=="7" or num=="日":
return int(7)
elif num=="8":
return int(8)
elif num=="9":
return int(9)
def main():
createXls()
fname = 'chooseStart.xlsx' # 相对路径
bk = xlrd2.open_workbook(fname) # 获取当前文档的表
try:
sh = bk.sheet_by_name('Sheet1')
except:
print("have not that sheet")
nrows = sh.nrows # 获取行数
row_list = []
for i in range(1, nrows): # 从第二行开始提取数据,第一行是表头
text1=sh.cell(i,4).value
len1=len(str(text1))
datelist=[]
textlist=[]
str0=""
str1=""
flag=0
first_appear=0
#print(text1[0])
for j in range(0,len1):
if text1[j]=='第':
first_appear = 1
flag=1
if flag==1 and text1[j]=='周':
str0 = str0 + text1[j]
flag=0
datelist.append(str0)
str0=""
if flag==1:
str0 = str0+ text1[j]
if flag==0 and first_appear == 1:
str1 = str1+ text1[j]
if (j!=0 and text1[j]=='第') or (j==len1-1):
if j==len1-1:
str1 = str1 + text1[j]
if str1!="":
textlist.append(str1)
str1=""
#print(textlist)
num=0
for date in datelist:
date_len=len(date)
start_date=0
end_date=0
if date_len==5:
start_date=stringToInt(date[1])
end_date=stringToInt(date[3])
elif date_len==6:
start_date = stringToInt(date[1])
end_date = stringToInt(date[3])*10+stringToInt(date[4])
elif date_len==7:
start_date = stringToInt(date[1])*10+stringToInt(date[2])
end_date = stringToInt(date[4])*10+stringToInt(date[5])
for k in range(start_date,end_date+1):
row_num=(k-1)*13
for word in range(len(textlist[num])):
if word < len(textlist[num])-1:
if textlist[num][word]=='星' and textlist[num][word+1]=='期':
col_num=stringToInt(textlist[num][word+2])
continue
if textlist[num][word]=='上' or textlist[num][word]=='下':
#print(num," ",word," ",textlist[num][word]," ",start_date," ",end_date)
if textlist[num][word+1]=='午':
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
num1=stringToInt(textlist[num][word+ + 2])
row_num=(k-1)*13+num1
xlsc=copy(bk1)
shtc=xlsc.get_sheet(0)
if sh1.cell_value(row_num,col_num)=="":
shtc.write(row_num,col_num, sh.cell(i,0).value+sh.cell(i,1).value)
else:
addStr=sh1.cell(row_num,col_num).value+" "+sh.cell(i,0).value+sh.cell(i,1).value
shtc.write(row_num, col_num,addStr)
xlsc.save('./chooseEnd.xls')
else:
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
num1 = stringToInt(textlist[num][word + 1])
row_num = (k - 1) * 13 + num1
xlsc = copy(bk1)
shtc = xlsc.get_sheet(0)
if sh1.cell_value(row_num, col_num) == "":
shtc.write(row_num, col_num, sh.cell(i, 0).value + sh.cell(i, 1).value)
else:
addStr = sh1.cell(row_num, col_num).value + " " + sh.cell(i, 0).value + sh.cell(i,1).value
shtc.write(row_num, col_num, addStr)
xlsc.save('./chooseEnd.xls')
elif textlist[num][word]=='晚':
if textlist[num][word+1]=='上':
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
num1=stringToInt(textlist[num][word+ 2])
if num1==1:
num1=stringToInt(textlist[num][word+ 2])*10+stringToInt(textlist[num][word+ 3])
row_num=(k-1)*13+num1
xlsc=copy(bk1)
shtc=xlsc.get_sheet(0)
if sh1.cell_value(row_num,col_num)=="":
shtc.write(row_num,col_num, sh.cell(i,0).value+sh.cell(i,1).value)
else:
addStr=sh1.cell(row_num,col_num).value+" "+sh.cell(i,0).value+sh.cell(i,1).value
shtc.write(row_num, col_num,addStr)
xlsc.save('./chooseEnd.xls')
else:
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
num1 = stringToInt(textlist[num][word + 1])
if num1==1:
num1=stringToInt(textlist[num][word+ 1])*10+stringToInt(textlist[num][word + 2])
row_num = (k - 1) * 13 + num1
xlsc = copy(bk1)
shtc = xlsc.get_sheet(0)
if sh1.cell_value(row_num, col_num) == "":
shtc.write(row_num, col_num, sh.cell(i, 0).value + sh.cell(i, 1).value)
else:
addStr = sh1.cell(row_num, col_num).value + " " + sh.cell(i, 0).value + sh.cell(i,1).value
shtc.write(row_num, col_num, addStr)
xlsc.save('./chooseEnd.xls')
num=num+1
style = xlwt.easyxf('pattern: pattern solid, fore_colour light_blue;'
'font: colour white, bold True;')
fname1 = 'chooseEnd.xls' # 相对路径
bk1 = xlrd2.open_workbook(fname1) # 获取当前文档的表
try:
sh1 = bk1.sheet_by_name('sheet1')
except:
print("have not that sheet")
xlsc = copy(bk1)
shtc = xlsc.get_sheet(0)
for i in range(0,258):
for j in range(0,7):
wordNum=0
addStr = sh1.cell(i, j).value
addStrNum=len(addStr)
for k in range(addStrNum):
if(addStr[k]=='班'):
wordNum=wordNum+1
if(wordNum>=2):
shtc.write(i, j, addStr,style)
xlsc.save('./chooseEnd.xls')
if __name__=='__main__':
main()
1.将要选的课按以下格式写入chooseStart表格中
2.运行程序
3.结果会显示在chooseEnd表格中(程序只显示至20周,若某学期有20周以上的课需要在程序中做以修改),若有重复会标蓝,如下图:
CSDN:https://download.csdn.net/download/soul_mingling/86474208
百度网盘:链接:https://pan.baidu.com/s/1syXK9RLV_5H1DsWsA4n9MA
提取码:hgte
注:双击exe即可使用,没有图形界面,每次双击exe,chooseEnd文件都会被覆盖重写