把word题库编写为excle 题库,有三种题型,单选题、判断题、多选题新建一个单独的题库,从题库中随机抽取各个题型,生成随机试卷。
danxuan.xlsx duoxuan.xlsx panduan.xlsx 分别为单选题、多选题、判断题题库。
单选题判断题
多选题
from pywebio import start_server
from pywebio.input import *
from pywebio.output import *
from docx import Document
import openpyxl
import random
from pywebio.platform.tornado import webio_handler
from pywebio.platform.tornado import start_server as webio_start_server
def generate_exam(btn_val):
def danxuan(num_questions):
workbook = openpyxl.load_workbook('danxuan.xlsx')
sheet = workbook.active
questions = []
for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, values_only=True):
question = {
'类型': row[0],
'序号': row[1],
'题干': row[2],
'A': row[3],
'B': row[4],
'C': row[5],
'D': row[6],
'正确答案': row[7]
}
questions.append(question)
selected_questions = random.sample(questions, num_questions)
return selected_questions
def panduan(num_questions):
workbook = openpyxl.load_workbook('panduan.xlsx')
sheet = workbook.active
questions = []
for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, values_only=True):
questions.append(row)
return random.sample(questions, num_questions)
def duoxuan(num_questions):
workbook = openpyxl.load_workbook('duoxuan.xlsx')
sheet = workbook.active
questions = []
for row in sheet.iter_rows(min_row=2, max_row=sheet.max_row, values_only=True):
questions.append(row)
return random.sample(questions, num_questions)
# 定义题目数量
num_single_choice = 40
num_true_false = 30
num_multiple_choice = 30
# 创建Word文档
doc = Document()
# 抽取题目
selected_single_choice = danxuan(num_single_choice)
selected_true_false = panduan(num_true_false)
selected_multiple_choice = duoxuan(num_multiple_choice)
# 生成试卷
# 遍历选取的题目信息并生成试卷
# 随机抽取单选题
doc.add_paragraph("一、单选题")
for i, question in enumerate(selected_single_choice, start=1):
question_content = question['题干']
option_A = question['A']
option_B = question['B']
option_C = question['C']
option_D = question['D']
doc.add_paragraph(f"{i}. {question_content}")
doc.add_paragraph(f"A. {option_A} B. {option_B} C. {option_C} D. {option_D}")
# doc.add_paragraph("")
# 随机抽取判断题
doc.add_paragraph("二、判断题")
for i, question in enumerate(selected_true_false, start=1):
question_content = question[1]
# 判断题
doc.add_paragraph(f" {i}. {question_content}")
# 随机抽取多选题
doc.add_paragraph("三、多选题")
for i, question in enumerate(selected_multiple_choice, start=1):
question_content = question[1]
option_A = question[2]
option_B = question[3]
option_C = question[4]
option_D = question[5]
option_E = question[6]
# 判断题目是否有选项E
if 'E' in question:
option_E = question['E']
else:
option_E = None
# 题目排序
doc.add_paragraph(f"{i}. {question_content}")
doc.add_paragraph(f"{option_A} {option_B} {option_C} {option_D}")
# 添加选项E(如果存在)
if option_E:
doc.add_paragraph(f" {option_E}")
# doc.add_paragraph("") # 添加空行
#题目与答案分隔符 题目结束
doc.add_page_break()
# 添加单选题答案部分
doc.add_paragraph("单选题答案:")
answers = ""
for i, question in enumerate(selected_single_choice, start=1):
correct_answer = question['正确答案']
answers += f"{i}. {correct_answer} "
doc.add_paragraph(answers)
# 添加判断题答案部分
doc.add_paragraph("判断题答案:")
answers = ""
for i, question in enumerate(selected_true_false, start=1):
correct_answer = question[2] # 获取元组中的第二个元素作为正确答案
answers += f"{i}. {correct_answer} "
doc.add_paragraph(answers)
# 添加多选题答案部分
doc.add_paragraph("多选题答案:")
answer =""
for i, question in enumerate(selected_multiple_choice, start=1):
correct_options = question[-1] # 获取元组中的最后一个元素作为正确选项的字符串
answer +=f"{i}. {correct_options}"
doc.add_paragraph(answer)
# 保存生成的试卷文件
doc.save("生成的试卷.docx")
print("试卷已生成")
put_markdown("## 试卷已生成,请下载")
# 以二进制形式读取生成的试卷文件,并传递给put_file()函数
with open('生成的试卷.docx', 'rb') as f:
content_bytes = f.read()
put_file('生成的试卷.docx', content=content_bytes)
def main():
# title = "2024年“产业工人杯”职业技能竞赛"
# put_html(f'<head><title>{title}</title></head>')
put_html('<span style="font-size:30px;">2024年儋州洋浦“产业工人杯”职业技能竞赛题库</span>')
put_buttons(['请点击,随机生成试卷'], onclick=generate_exam)
# print('2024年“产业工人杯”职业技能竞赛 油品储运操作工题库')
if __name__ == '__main__':
webio_start_server(main, port=8081, cdn=False, handler_kwags={'allowed_origins': ['*']},
title="2024年“产业工人杯”职业技能竞赛题库")
因为后续需求,开发了一个web 界面
可下载,仅供监考老师下载。 有什么问题可私信我。
新加题目与答案分割页,答案之间之前是一个答案一行,现在改为一行多条答案