|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在下文科教师,学了半个月python,想一个考场编排系统,就是把学生按设计的人数随机编排试室,并生成考试号。
在CSDN找了这个开源代码,想改动,但运行后报错:“输入的文件名有误,请重新输入”。请高手指教。
from pandas import read_excel
from pandas import ExcelWriter
from openpyxl import Workbook
from openpyxl.styles import Alignment,Font,Border,Side
# import tkinter as tk
from tkinter import filedialog,Tk,Label,LabelFrame,Entry,Button,StringVar,IntVar,Checkbutton
root = Tk()
root.geometry("500x400")
root.title("学生考试安排软件V4.0(By:Marvinmao For:TT)")
studentsfile = ''
randomfile = ''
markfile = ''
# 手动上传考号文件函数
def upload_randomfile():
global randomfile
filename2 = filedialog.askopenfilename(title='上传考号名单',filetypes=[('xls','*xls'),('xlsx','*.xlsx')])
entry_filename2.delete(0,'end')
entry_filename2.insert('insert',filename2)
randomfile = filename2
markframe = LabelFrame(root, text="生成座位签", padx=5, pady=5)
markframe.pack(padx=10, pady=10)
# 上传考号文件按钮
fn = StringVar()
upload_button2 = Button(markframe,text='上传考号名单:',command=upload_randomfile).grid(row=0,column=0)
# 设置保存文件名和路径的entry
entry_filename2 = Entry(markframe,width=30,text=fn)
fn.set('第一行标题包含:班级、姓名、考号、考室、座位号')
entry_filename2.grid(row=0,column=1)
# 填写考试名称
examname = StringVar()
label_examname = Label(markframe,text='考试名称:').grid(row=1,column=0)
entry_examname = Entry(markframe,textvariable=examname,width=30)
entry_examname.grid(row=1,column=1)
# 设置座位签大小
marksize= IntVar()
marksize.set('1')
label_marksize = Label(markframe,text='座签大小:')
label_marksize.grid(row=4,column=0)
entry_marksize = Entry(markframe,textvariable=marksize,width=30)
entry_marksize.grid(row=4,column=1)
marksize_info = Label(markframe,text='输入值1或2或3,对应尺寸小或中或大')
marksize_info.grid(row=5,column=1)
def make_mark():
global markfile,randomfile
exam_name=entry_examname.get()
mark_size=int(entry_marksize.get())
try:
students = read_excel(randomfile)
wb = Workbook()
ws = wb.worksheets[0]
ws.title = '座位签'
font1 = Font(u'黑体',size = fontsize[mark_size][0],bold=False,italic=False,strike=False,color='000000') # 设置字体和字号颜色
font2 = Font(u'黑体',size = fontsize[mark_size][1],bold=False,italic=False,strike=False,color='000000') # 设置字体和字号颜色
font3 = Font(u'黑体',size = fontsize[mark_size][2],bold=False,italic=False,strike=False,color='000000') # 设置字体和字号颜色
# 设置行高和列宽
for i in range(len(cells)):
if i%2==0:
ws.column_dimensions[cells[i]].width = width[mark_size][0]
else:
ws.column_dimensions[cells[i]].width = width[mark_size][1]
perrow = cols[mark_size-1]/2 # 每行几个学生
rows = int(len(students)//perrow + 1 )*2 # 每个学生2行一共多少行
for row in range(rows+1):
if row%2==0:
ws.row_dimensions[row].height = height[mark_size][1]
else:
ws.row_dimensions[row].height = height[mark_size][0]
# rows = int(len(students)//(len(cells)/2-mark_size)) # 所有的学生被分成多少行
for idx in range(len(students)):
# print(idx,'tttt')
# 这个学生占了哪两行
r1,r2 = int(idx//perrow*2+1),int(idx//perrow*2 +2)
# 这个学生占了哪两列
c1,c2 = int((idx+1)%perrow*2-2),int((idx+1)%perrow*2-1)
if (idx+1)%perrow==0:
c1,c2 = int(perrow*2-2),int(perrow*2-1)
# 开始填充内容
# 第一格
ws.merge_cells(start_row=r1, start_column=c1+1, end_row=r1, end_column=c2+1)
ws[str(cells[c1]+str(r1))].alignment=Alignment(horizontal='center',
vertical='center',
text_rotation=0,
wrap_text=True,
shrink_to_fit=False,
indent=0)
ws[str(cells[c1]+str(r1))] = exam_name
ws[str(cells[c1]+str(r1))].font = font1
ws[str(cells[c1]+str(r1))].border = border1
ws[str(cells[c2]+str(r1))].border = border4
# 第二格,座位号
ws[str(cells[c1]+str(r2))] = str(students.loc[idx,'座位号'])
ws[str(cells[c1]+str(r2))].font = font3
ws[str(cells[c1]+str(r2))].border = border2
# 第三个,学生信息
ws[str(cells[c2]+str(r2))].alignment=Alignment(horizontal='left',
vertical='center',
text_rotation=0,
wrap_text=True,
shrink_to_fit=False,
indent=0)
ws[str(cells[c2])+str(r2)] = '姓名:' +students.loc[idx,'姓名']+"\n"+'班级:' + \
str(students.loc[idx,'班级']) + "\n" + '考室:' + str(students.loc[idx,'考室']) +"\n"+\
'考号:' + str(students.loc[idx,'考号'])
ws[str(cells[c2])+str(r2)].font = font2
ws[str(cells[c2])+str(r2)].border = border3
# 保存表格
wb.save(exam_name + '考生座位签.xls')
# 表格名称
markfile = exam_name + '考生座位签.xls'
label_j = Label(markframe,text='考生座位签文件:' + markfile).grid(row=7,column=0,columnspan=2,ipadx=1)
except:
print("输入的文件名有误,请重新输入。")
# 生成座位签按钮
mark_button = Button(markframe,text='生成座位签',command=make_mark).grid(row=6,column=0,pady=2)
root.mainloop()
|
|