import xlwt
import random
c=int(input("輸入國文科總人數:"))
cc=input("輸入國文科老師的監考節數:").split()
cn=[int(x) for x in cc]#每個國文老師的監考節數
e=int(input("輸入英文科總人數:"))
ee=input("輸入英文科老師的監考節數:").split()
en=[int(x) for x in ee]#每個英文老師的監考節數
m=int(input("輸入數學科總人數:"))
mm=input("輸入數學科老師的監考節數:").split()
mn=[int(x) for x in mm]#每個數學老師的監考節數
def t_sum(a):#把每一科老師監考時數加起來
s=0
for i in range(len(a)):
s+=a[i]
return s
teacher_sum=t_sum(cn)+t_sum(en)+t_sum(mn)#所有科的老師
person=[[0]*10 for i in range(c+e+m)]#每個老師的數值
exam=input("輸入第一到第十節的總節數(10個數字,以空格隔開):").split()
examth=[int(x) for x in exam]
workbook = xlwt.Workbook(encoding='utf-8') #新建工作簿
sheet1 = workbook.add_sheet("月考") #新建sheet
sheet1.write(0,0,"姓名")
sheet1.write(0,1,"編號")
sheet1.write(0,2,"監考時數")
sheet1.write(0,3,"第一節")
sheet1.write(0,4,"第二節")
sheet1.write(0,5,"第三節")
sheet1.write(0,6,"第四節")
sheet1.write(0,7,"第五節")
sheet1.write(0,8,"第六節")
sheet1.write(0,9,"第七節")
sheet1.write(0,10,"第八節")
sheet1.write(0,11,"第九節")
sheet1.write(0,12,"第十節")
sheet1.write(0,13,"個人總計")
ten1=[0,1,2,3,4]#欲填入的1的各節為第一天5節
ten2=[5,6,7,8,9]#欲填入的1的各節為第二天5節
ten12=[0,1,2,3,4,5,6,7,8,9]#節數超過5節,二天任意排
chinesesum=0
englishsum=0
mathsum=0
for z in range(100):
for i in range(c):
ss=0
sheet1.write(i+1,0,"國文")
sheet1.write(i+1,1,i+1) #填入編號
sheet1.write(i+1,2,cn[i])#填入監考節數
if cn[i]<=5 and cn[i]>=1: #監考時數小於5節,排在同一天
rand=random.randint(1,cn[i])#實際排的節數需小於應排節數
s1=random.sample(ten1,rand)#第一天的隨機串列
s2=random.sample(ten2,rand)#第二天的隨機串列
s=[s1,s2]
com=random.choice(s)#二天的隨機串列再隨機選
for j in range(rand):
person[i][com[j]]=1
else:
s4=random.sample(ten12,cn[i])#監考時數超過5節,需2天都排
for j in range(cn[i]):
person[i][s4[j]]=1
for j in range(3,13):#寫入EXCL
ss+=person[i][j-3]#總計個人實際監考節數
sheet1.write(i+1,j,person[i][j-3])
sheet1.write(i+1,13,ss)
chinesesum+=ss #國文科總和
for i in range(e):
ss=0
sheet1.write(c+i+1,0,"英文")
sheet1.write(c+i+1,1,i+1)#填入編號
sheet1.write(c+i+1,2,en[i])#填入監考節數
if en[i]<=5 and en[i]>=1:#監考時數小於5節,排在同一天
rand=random.randint(1,en[i])#實際排的節數需小於應排節數
s1=random.sample(ten1,rand)#第一天的隨機串列
s2=random.sample(ten2,rand)#第二天的隨機串列
s=[s1,s2]
com=random.choice(s)#二天的隨機串列再隨機選
for j in range(rand):
person[c+i][com[j]]=1
else:
s4=random.sample(ten12,en[i])#監考時數超過5節,需2天都排
for j in range(en[i]):
person[c+i][s4[j]]=1
for j in range(3,13):#寫入EXCEL
ss+=person[c+i][j-3]#總計個人實際監考節數
sheet1.write(c+i+1,j,person[c+i][j-3])
sheet1.write(c+i+1,13,ss)
englishsum+=ss#英文科總和
for i in range(m):
ss=0
sheet1.write(c+e+i+1,0,"數學")
sheet1.write(c+e+i+1,1,i+1) #填入編號
sheet1.write(c+e+i+1,2,mn[i])#填入監考節數
if mn[i]<=5 and mn[i]>=1:#監考時數小於5節,排在同一天
rand=random.randint(1,mn[i])#實際排的節數需小於應排節數
s1=random.sample(ten1,rand)#第一天的隨機串列
s2=random.sample(ten2,rand)#第二天的隨機串列
s=[s1,s2]
com=random.choice(s)#二天的隨機串列再隨機選
for j in range(rand):
person[c+e+i][com[j]]=1
else:
s4=random.sample(ten12,mn[i])#監考時數超過5節,需2天都排
for j in range(mn[i]):
person[c+e+i][s4[j]]=1
for j in range(3,13):#寫入EXCEL
ss+=person[c+e+i][j-3]#總計個人實際監考節數
sheet1.write(c+e+i+1,j,person[c+e+i][j-3])
sheet1.write(c+e+i+1,13,ss)
mathsum+=ss#數學科總和
total=chinesesum+englishsum+mathsum#每人的實際監考總和
sheet1.write(c+e+m+1,0,"目前總計")
sheet1.write(c+e+m+1,13,total) #所有人實際監考總節數
true1to10=[] #總計目前第一到第十節的總節數
for j in range(10):
s=0
for i in range(c+e+m):
s+=person[i][j]
true1to10.append(s)
for j in range(3,13):
sheet1.write(c+e+m+1,j,true1to10[j-3])
sheet1.write(c+e+m+2,0,"實際要監考節數")
for j in range(3,13):#寫入EXCEL
sheet1.write(c+e+m+2,j,examth[j-3])
sheet1.write(c+e+m+1,2,teacher_sum) #目前老師監考時數和
sheet1.write(c+e+m+2,13,sum(examth))
workbook.save(r'D:\監考表.xls') #儲存excel檔
if true1to10==examth:
break