一、背景
本文章主要是分享如何使用Python从MySQL数据库中面抽取试题,生成的试卷每一份都不一样。
二、准备工作
1.安装Python3
下载地址:https://www.python.org/downloads/windows/
2.安装库
pip install python-docx==0.8.10
pip install PyMySQL==1.0.2
3.试题库.xlsx
开发程序前需要先收集试题,本文是将试题收集存放MySQL数据库中,格式如下:
选择题数据库截图:

填空题/解答题/综合题数据库截图:

三、代码
Python+MySQL随机试卷及答案生成程序.py
# _*_ coding:utf-8 _*_
import random,os,pymysql
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
class SunckSql():
def __init__(self, host, user, passwd, dbName='', charset='utf8'):
self.host = host
self.user = user
self.passwd = passwd
self.dbName = dbName
self.charset = charset
def connet(self):
self.db = pymysql.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbName,
charset=self.charset) # 连接数据库
self.cursor = self.db.cursor() # 获取操作游标
def close(self):
self.cursor.close() # 释放游标
self.db.close() # 关闭数据库连接
# 查询
def get_all(self, sql):
res = None
try:
self.connet()
self.cursor.execute(sql) # 执行sql语句
res = self.cursor.fetchall() # 返回查询所有结果
except Exception as e:
print('查询失败:%s' % e)
finally:
self.close()
return res
# 增加、删除、修改
def shell_sql(self, sql):
"执行sql语句"
print(sql)
count = 0
try:
self.connet()
count = self.cursor.execute(sql) # 执行sql语句
self.db.commit() # 提交
except Exception as e:
print('事务提交失败:%s' % e)
self.db.rollback() # 如果提交失败,回滚到上一次数据
finally:
self.close()
return count
def router_docx(choice1='', choice2='', choice3='', choice5='', choice6='', choice7='',paper_path='',name='1'):
"生成网络通信方向试题及答案"
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) #len范围内莶6"ТТf7C#ТFEw&7G"2~8r6%ТFEw&6%%ТFEw&6%5ТFEw&6%EТEw&6%UТ&w&B6UW"B"ТFEw&7G"2~8r6%eТРТ7C36&VBТТf7C3ТFEw&7G"~8r6EТFEw&6E%ТFEw&6E5ТFEw&6EEТEw&6EUТ&w&B6UW"B"ТFEw&7G"~8r6EeТРТ"FEw&Т"&w&B6UB"ТFEw&~KZz)rТ'V"E~KZz)n^XnТ'VG'VPТ7C"&6&RRТТf7C#ТFEw&7G"~8r6UТFEw&7G"~8r7G"6U%ТРТ2FEw&Т2&w&B6UB"ТFEw&~K8z)rТ'V2E~K8z)Xn#XnТ'VG'VPТ7C2&6&b"ТТf7C3ТFEw&7G"~8r6eТFEw&urТFEw&7G"~8r6e%ТРТBFEw&ТB&w&B6UB"ТFEw&~Y8{)rТ'VBE~Y8{)#XnТ'VG'VPТ7CB&&ТFEw&s8r67CEТFEw&67CE%РТFfRFW%~i;zW27rRRТFfRFW%~i;zzjW27rRRРЦFVb↗77vB%RТW%F2F&RF7F~r>ZiKbF7G2W%ТF"W%>X[ih~KnZKТ7V777vB%R>i[h[0Т6E'6V7Bg&2"R~Yh)r>ii[h[>KNТ6"E'6V7Bg&2"R~kXzh)rТ62E'6V7Bg&2"R~{{Θ Kh)rТ6BE'6V7Bg&2"R~i;z)rТ6RE'6V7Bg&2"R~Zz)rТ6bE'6V7Bg&2"R~z)rТ6rE'6V7Bg&2"R~{{Θ KY)rТ6E'6V7Bg&2"R~i;z)rТf>Y{nyIXjТ&%66"626R6b6rW%ТF66"6B6R6b6W%РЦb%#Т↗i[h[6YYrS7JnX77vC7rF%SKnkX)[2r&SУFcУ^K[i5[h[>Kh)^XyNXh^ZiNZIX[>KF5y^XyNXNiik:zKX[nZX[>ih~z |