一、背景
本文章主要是分享如何使用python从mysql数据库中面抽取试题,生成的试卷每一份都不一样。
二、准备工作
1.安装python3
下载地址:https://www.python.org/downloads/windows/
2.安装库
pip installpython-docx==0.8.10
pip installpymysql==1.0.2
3.试题库.xlsx
开发程序前需要先收集试题,本文是将试题收集存放mysql数据库中,格式如下:
选择题数据库截图:
填空题/解答题/综合题数据库截图:
三、代码
python+mysql随机试卷及答案生成程序.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# _*_ 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范围内获取指定的数量
x = 1
for y in list1:
docx1.add_paragraph( str (x) + '、' + choice1[y][ 1 ])
docx1.add_paragraph(choice1[y][ 2 ])
docx1.add_paragraph(choice1[y][ 3 ])
docx1.add_paragraph(choice1[y][ 4 ])
p11 = docx1.add_paragraph(choice1[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 ) #段后间距
docx2.add_paragraph( str (x) + '、' + choice1[y][ 6 ])
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][ 1 ])
docx1.add_paragraph(choice2[y][ 2 ])
docx1.add_paragraph(choice2[y][ 3 ])
docx1.add_paragraph(choice2[y][ 4 ])
p11 = docx1.add_paragraph(choice2[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 )
docx2.add_paragraph( str (x + 3 ) + '、' + choice2[y][ 6 ])
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][ 1 ])
docx1.add_paragraph(choice3[y][ 2 ])
docx1.add_paragraph(choice3[y][ 3 ])
docx1.add_paragraph(choice3[y][ 4 ])
p11 = docx1.add_paragraph(choice3[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 )
docx2.add_paragraph( str (x + 10 ) + '、' + choice3[y][ 6 ])
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][ 1 ])
docx2.add_paragraph( str (i) + '、' + str (choice5[j][ 2 ]))
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][ 1 ])
docx1.add_paragraph( '\r' )
docx2.add_paragraph( str (n) + '、' + choice6[m][ 2 ])
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][ 1 ])
docx2.add_paragraph(choice7[list4][ 2 ])
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][ 1 ])
docx1.add_paragraph(choice1[y][ 2 ])
docx1.add_paragraph(choice1[y][ 3 ])
docx1.add_paragraph(choice1[y][ 4 ])
p11 = docx1.add_paragraph(choice1[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 ) #段后间距
docx2.add_paragraph( str (x) + '、' + choice1[y][ 6 ])
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][ 1 ])
docx1.add_paragraph(choice2[y][ 2 ])
docx1.add_paragraph(choice2[y][ 3 ])
docx1.add_paragraph(choice2[y][ 4 ])
p11 = docx1.add_paragraph(choice2[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 )
docx2.add_paragraph( str (x + 3 ) + '、' + choice2[y][ 6 ])
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][ 1 ])
docx1.add_paragraph(choice4[y][ 2 ])
docx1.add_paragraph(choice4[y][ 3 ])
docx1.add_paragraph(choice4[y][ 4 ])
p11 = docx1.add_paragraph(choice4[y][ 5 ])
p11.paragraph_format.space_after = pt( 12 )
docx2.add_paragraph( str (x + 10 ) + '、' + choice4[y][ 6 ])
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][ 1 ])
docx2.add_paragraph( str (i) + '、' + str (choice5[j][ 2 ]))
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][ 1 ])
docx1.add_paragraph( '\r' )
docx2.add_paragraph( str (n) + '、' + choice6[m][ 2 ])
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][ 1 ])
docx2.add_paragraph(choice8[list4][ 2 ])
docx1.save(os.path.join(paper_path, '智能终端试题(%s).docx' % name))
docx2.save(os.path.join(paper_path, '智能终端试题答案(%s).docx' % name))
def main(ip,name,passwd,db_name):
paper_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '试卷' ) #试卷存放路径
if not os.path.exists(paper_path):
os.mkdir(paper_path) #创建试卷文件夹
my = suncksql(ip,name,passwd,db_name) #连接数据库
choice1 = my.get_all( "select * from %s" % '计算机基础选择题' ) #查询数据库中的试题
choice2 = my.get_all( "select * from %s" % '测试基础选择题' )
choice3 = my.get_all( "select * from %s" % '网络通信选择题' )
choice4 = my.get_all( "select * from %s" % '智能终端选择题' )
choice5 = my.get_all( "select * from %s" % '填空题' )
choice6 = my.get_all( "select * from %s" % '简答题' )
choice7 = my.get_all( "select * from %s" % '网络通信综合题' )
choice8 = my.get_all( "select * from %s" % '智能终端综合题' )
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(ip = '数据库ip地址' , name = 'mysql账号' , passwd = 'mysql密码' , db_name = '软件测试试题库' )
|
到此这篇关于python+mysql随机试卷及答案生成程序的文章就介绍到这了,更多相关python mysql随机试卷内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/airb/archive/2021/02/01/14273517.html