自动化办公-Python处理Excel生成试卷
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                自动化办公-Python处理Excel生成试卷
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                效果圖
 
 
思路
通過處理excel中不同的sheet,將每個題的順序還有題內(nèi)選項的順序隨機處理后,生成不同版本的試題
 使用到的庫
獲取試題列表
wb = xlrd.open_workbook('./file/試題2.xls')創(chuàng)建試題列表
class Question:passdef createQuestion(sheet):# 創(chuàng)建試題列表questionList = []for i in range(sheet.nrows):if i > 1:obj = Question()# 題目obj.subject = sheet.cell_value(i, 1, )# 題型obj.questionType = sheet.cell_value(i, 2, )# 選項A,B,C,Dobj.option = []obj.option.append(sheet.cell_value(i, 3))obj.option.append(sheet.cell_value(i, 4))obj.option.append(sheet.cell_value(i, 5))obj.option.append(sheet.cell_value(i, 6))# 分值obj.score = sheet.cell_value(i, 7)questionList.append(obj)# 將序列中所有元素隨機排序random.shuffle(questionList)return questionList生成Word試卷模板
# 生成word試卷模板 def createPaper(filename,papername,questionlist):document = Document()# 頁眉頁腳section = document.sections[0]header = section.headerp1=header.paragraphs[0]p1.text = papernamefooter = section.footerp2=footer.paragraphs[0]p2.text = '內(nèi)部試題,禁止泄露!!!'# 試卷基本信息title= document.add_heading(papername,level=1)title.alignment = WD_ALIGN_PARAGRAPH.CENTERp3 = document.add_paragraph()p3.add_run('姓名:________')p3.add_run('所屬部門:________')p3.alignment = WD_ALIGN_PARAGRAPH.CENTER# 試題信息number =0for question in questionlist:number+=1subject = document.add_paragraph()run = subject.add_run('%d '%number+question.subject)# 題目加粗run.blod=Truesubject.add_run('[%s]分'%str(question.score))# 打亂選項的順序random.shuffle(question.option)for index,option in enumerate(question.option):document.add_paragraph(('ABCD')[index]+str(option))document.save(filename)創(chuàng)建試卷
for j in range(len(wb.sheets())):sheet = wb.sheet_by_index(j)for i in range(5):questionList = createQuestion(sheet)createPaper('./file/%s'%sheet.name+str(i+1)+'.docx','2022年%s'%sheet.name+'第一次考試',questionList)總結(jié)
以上是生活随笔為你收集整理的自动化办公-Python处理Excel生成试卷的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 计算硬盘的计算机,硬盘整数分区计算器免费
- 下一篇: 为什么中国电气自动化工程师这么难招
