Python+Excel随机试卷及答案生成程序
一、背景
本文章主要是分享如何使用Python从试题库中面抽取试题,生成的试卷每一份都不一样。
二、准备工作
1.安装Python3
下载地址:https://www.python.org/downloads/windows/
2.安装库
pip install python-docx==0.8.10
pip install xlrd==1.2.0
3.试题库.xlsx
开发程序前需要先收集试题,本文是将试题收集存放到Excel表中(如果题量比较大,可以存放到数据库中),格式如下:
选择题:
填空题/解答题/综合题:
三、代码
Test_question.py
# _*_ coding:utf-8 _*_ import random,xlrd,os from docx import Document from docx.shared import Inches,Pt from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_LINE_SPACING from docx.oxml.ns import qn from docx.shared import Inches def get_xlsx(sheet): "获取指定Excel数据" excel_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '软件测试试题库.xlsx') # 获取用例文件路径 file = xlrd.open_workbook(excel_path) # 打开excel list = [] sheet = file.sheet_by_name(sheet) # 获得指定sheet数据 row_value1 =sheet.row_values(0) # 获取第1行的标题 nrows = sheet.nrows # 获取当前sheet行数 ncols = sheet.ncols # 获取当前sheet列数 for i in range(1, nrows): # 从第2行遍历当前sheet row = sheet.row_values(i) # 获取行数据 dict = {} # 创建空字典 for j in range(0, ncols): # 遍历sheet列,组成字典 if row_value1[j] == '序号': dict[row_value1[j]] = int(row[j]) else: dict[row_value1[j]] = row[j] # 从第一列开始,将每一列的数据与第1行的数据组成一个键值对,形成字典 list.append(dict) # 将字典添加list中 return list def router_docx(choice1,choice2,choice3,choice5,choice6,choice7,paper_path,name): "生成网络通信方向试题及答案" docx1 = Document() docx2 = Document() docx1.styles['Normal'].font.name = '宋体' #选择字体 docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #默认字体 docx1.styles['Normal'].font.size = Pt(11) #默认字号大小 docx1.styles['Normal'].paragraph_format.space_before = Pt(0) #默认段前间距 docx1.styles['Normal'].paragraph_format.space_after = Pt(0) #默认段后间距 docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默认单倍行距 sec = docx1.sections[0] # sections对应文档中的“节” sec.left_margin = Inches(1) # 设置左页面边距 sec.right_margin = Inches(1) #设置右页面边距 sec.top_margin = Inches(0.5) # 设置上页面边距 sec.bottom_margin = Inches(0.5) #设置下页面边距 p=docx1.add_paragraph() #添加段落 run = p.add_run('软件测试(网络通信)方向试题(%s)' % name) #使用add_run添加文字 run.font.name = '微软雅黑' #设置字体 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体 run.font.size = Pt(18) #字体大小设置 p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置 docx1.add_paragraph('【说明】') # 添加段落文字 docx1.add_paragraph('1.笔试时间为60分钟。') docx1.add_paragraph('2.请将答案写在答题卡上,且不允许在试题卷上做任何涂写和标记。') q=docx2.add_paragraph() #添加段落 run = q.add_run('软件测试(网络通信)方向试题答案(%s)' % name) #使用add_run添加文字 run.font.name = '微软雅黑' #设置字体 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体 run.font.size = Pt(18) #字体大小设置 q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置 p1 = docx1.add_paragraph() p1.paragraph_format.space_before = Pt(12) #设置段前间距 docx2.add_paragraph('一、选择题') run = p1.add_run('一、选择题(每题3分共45分)') run.bold = True # 字体加粗 list1=random.sample(range(0,len(choice1)-1),3) x=1 for y in list1: docx1.add_paragraph(str(x)+'、'+choice1[y]['题目']) docx1.add_paragraph(choice1[y]['选项A']) docx1.add_paragraph(choice1[y]['选项B']) docx1.add_paragraph(choice1[y]['选项C']) p11=docx1.add_paragraph(choice1[y]['选项D']) p11.paragraph_format.space_after = Pt(12) #段后间距 docx2.add_paragraph(str(x)+'、'+choice1[y]['答案']) x+=1 list2=random.sample(range(0,len(choice2)-1),7) x=1 for y in list2: docx1.add_paragraph(str(x+3)+'、'+choice2[y]['题目']) docx1.add_paragraph(choice2[y]['选项A']) docx1.add_paragraph(choice2[y]['选项B']) docx1.add_paragraph(choice2[y]['选项C']) p11=docx1.add_paragraph(choice2[y]['选项D']) p11.paragraph_format.space_after = Pt(12) docx2.add_paragraph(str(x+3)+'、'+choice2[y]['答案']) x+=1 list3=random.sample(range(0,len(choice3)-1),5) x=1 for y in list3: docx1.add_paragraph(str(x+10)+'、'+choice3[y]['题目']) docx1.add_paragraph(choice3[y]['选项A']) docx1.add_paragraph(choice3[y]['选项B']) docx1.add_paragraph(choice3[y]['选项C']) p11=docx1.add_paragraph(choice3[y]['选项D']) p11.paragraph_format.space_after = Pt(12) docx2.add_paragraph(str(x+10)+'、'+choice3[y]['答案']) x+=1 p2 = docx1.add_paragraph() p2.paragraph_format.space_before = Pt(12) docx2.add_paragraph('二、填空题') run = p2.add_run('二、填空题(每题3分,共15分)') run.bold = True list2 = random.sample(range(0, len(choice5)-1), 5) i = 1 for j in list2: docx1.add_paragraph(str(i) + '、' + choice5[j]['题目']) docx2.add_paragraph(str(i) + '、' + str(choice5[j]['答案'])) i += 1 p3 = docx1.add_paragraph() p3.paragraph_format.space_before = Pt(12) docx2.add_paragraph('三、简答题') run = p3.add_run('三、简答题(每题10分,共20分)') run.bold = True list3 = random.sample(range(0, len(choice6)-1), 2) n = 1 for m in list3: docx1.add_paragraph(str(n) + '、' + choice6[m]['题目']) docx1.add_paragraph('\r') docx2.add_paragraph(str(n) + '、' + choice6[m]['答案']) n += 1 p4 = docx1.add_paragraph() p4.paragraph_format.space_before = Pt(12) docx2.add_paragraph('四、综合题') run = p4.add_run('四、综合题(共20分)') run.bold = True list4 = random.randint(0, len(choice7)-1) docx1.add_paragraph('1、' + choice7[list4]['题目']) docx2.add_paragraph(choice7[list4]['答案']) docx1.save(os.path.join(paper_path, '网络通信试题(%s).docx' % name)) #保存试题 docx2.save(os.path.join(paper_path, '网络通信试题答案(%s).docx' % name)) #保存答案 def android_docx(choice1, choice2, choice4, choice5, choice6, choice8,paper_path,name): """生成智能终端方向的试题""" docx1 = Document() docx2 = Document() docx1.styles['Normal'].font.name = '宋体' #选择字体 docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #默认字体 docx1.styles['Normal'].font.size = Pt(11) #默认字号大小 docx1.styles['Normal'].paragraph_format.space_before = Pt(0) #默认段前间距 docx1.styles['Normal'].paragraph_format.space_after = Pt(0) #默认段后间距 docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默认单倍行距 sec = docx1.sections[0] # sections对应文档中的“节” sec.left_margin = Inches(1) # 设置左页面边距 sec.right_margin = Inches(1) #设置右页面边距 sec.top_margin = Inches(0.5) # 设置上页面边距 sec.bottom_margin = Inches(0.5) #设置下页面边距 p=docx1.add_paragraph() #添加段落 run = p.add_run('软件测试(智能终端)方向试题(%s)' % name) #使用add_run添加文字 run.font.name = '微软雅黑' #设置字体 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体 run.font.size = Pt(18) #字体大小设置 p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置 docx1.add_paragraph('【说明】') # 添加段落文字 docx1.add_paragraph('1.笔试时间为60分钟。') docx1.add_paragraph('2.请将答案写在答题卡上,且不允许在试题卷上做任何涂写和标记。') q = docx2.add_paragraph() # 添加段落 run = q.add_run('软件测试(智能终端)方向试题答案(%s)' % name) # 使用add_run添加文字 run.font.name = '微软雅黑' # 设置字体 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') # 设置字体 run.font.size = Pt(18) # 字体大小设置 q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 段落文字居中设置 p1 = docx1.add_paragraph() p1.paragraph_format.space_before = Pt(12) #设置段前间距 docx2.add_paragraph('一、选择题') run = p1.add_run('一、选择题(每题3分共45分)') run.bold = True # 字体加粗 list1=random.sample(range(0,len(choice1)-1),3) x=1 for y in list1: docx1.add_paragraph(str(x)+'、'+choice1[y]['题目']) docx1.add_paragraph(choice1[y]['选项A']) docx1.add_paragraph(choice1[y]['选项B']) docx1.add_paragraph(choice1[y]['选项C']) p11=docx1.add_paragraph(choice1[y]['选项D']) p11.paragraph_format.space_after = Pt(12) #段后间距 docx2.add_paragraph(str(x)+'、'+choice1[y]['答案']) x+=1 list2=random.sample(range(0,len(choice2)-1),7) x=1 for y in list2: docx1.add_paragraph(str(x+3)+'、'+choice2[y]['题目']) docx1.add_paragraph(choice2[y]['选项A']) docx1.add_paragraph(choice2[y]['选项B']) docx1.add_paragraph(choice2[y]['选项C']) p11=docx1.add_paragraph(choice2[y]['选项D']) p11.paragraph_format.space_after = Pt(12) docx2.add_paragraph(str(x+3)+'、'+choice2[y]['答案']) x+=1 list3=random.sample(range(0,len(choice4)-1),5) x=1 for y in list3: docx1.add_paragraph(str(x+10)+'、'+choice4[y]['题目']) docx1.add_paragraph(choice4[y]['选项A']) docx1.add_paragraph(choice4[y]['选项B']) docx1.add_paragraph(choice4[y]['选项C']) p11=docx1.add_paragraph(choice4[y]['选项D']) p11.paragraph_format.space_after = Pt(12) docx2.add_paragraph(str(x+10)+'、'+choice4[y]['答案']) x+=1 p2 = docx1.add_paragraph() p2.paragraph_format.space_before = Pt(12) docx2.add_paragraph('二、填空题') run = p2.add_run('二、填空题(每题3分,共15分)') run.bold = True list2 = random.sample(range(0, len(choice5)-1), 5) i = 1 for j in list2: docx1.add_paragraph(str(i) + '、' + choice5[j]['题目']) docx2.add_paragraph(str(i) + '、' + str(choice5[j]['答案'])) i += 1 p3 = docx1.add_paragraph() p3.paragraph_format.space_before = Pt(12) docx2.add_paragraph('三、简答题') run = p3.add_run('三、简答题(每题10分,共20分)') run.bold = True list3 = random.sample(range(0, len(choice6)-1), 2) n = 1 for m in list3: docx1.add_paragraph(str(n) + '、' + choice6[m]['题目']) docx1.add_paragraph('\r') docx2.add_paragraph(str(n) + '、' + choice6[m]['答案']) n += 1 p4 = docx1.add_paragraph() p4.paragraph_format.space_before = Pt(12) docx2.add_paragraph('四、综合题') run = p4.add_run('四、综合题(共20分)') run.bold = True list4 = random.randint(0, len(choice8)-1) docx1.add_paragraph('1、' + choice8[list4]['题目']) docx2.add_paragraph(choice8[list4]['答案']) docx1.save(os.path.join(paper_path, '智能终端试题(%s).docx' % name)) docx2.save(os.path.join(paper_path, '智能终端试题答案(%s).docx' % name)) def main(): paper_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '试卷') #试卷存放路径 if not os.path.exists(paper_path): os.mkdir(paper_path) choice1 = get_xlsx('计算机基础') #从excel中获得试题 choice2 = get_xlsx('测试基础') choice3 = get_xlsx('选择题(网络通信)') choice4 = get_xlsx('选择题(智能终端)') choice5 = get_xlsx('填空题') choice6 = get_xlsx('简答题') choice7 = get_xlsx('综合题(网络通信)') choice8 = get_xlsx('综合题(智能终端)') for i in range(1,4): #同时生成3份试卷及答案 router_docx(choice1, choice2, choice3, choice5, choice6, choice7, paper_path, i) android_docx(choice1, choice2, choice4, choice5, choice6, choice8, paper_path, i) if __name__ == "__main__": main()
—————————————————————————————— 选择正确的事、再把事做正确
——————————————————————————————