经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MySQL » 查看文章
Python+MySQL随机试卷及答案生成程序的示例代码
来源:jb51  时间:2021/2/1 13:48:30  对本文有异议

一、背景

本文章主要是分享如何使用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. # _*_ coding:utf-8 _*_
  2. import random,os,pymysql
  3. from docx import Document
  4. from docx.shared import Inches,Pt
  5. from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_LINE_SPACING
  6. from docx.oxml.ns import qn
  7. from docx.shared import Inches
  8.  
  9. class SunckSql():
  10. def __init__(self, host, user, passwd, dbName='', charset='utf8'):
  11. self.host = host
  12. self.user = user
  13. self.passwd = passwd
  14. self.dbName = dbName
  15. self.charset = charset
  16.  
  17. def connet(self):
  18. self.db = pymysql.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbName,
  19. charset=self.charset) # 连接数据库
  20. self.cursor = self.db.cursor() # 获取操作游标
  21.  
  22. def close(self):
  23. self.cursor.close() # 释放游标
  24. self.db.close() # 关闭数据库连接
  25.  
  26. # 查询
  27. def get_all(self, sql):
  28. res = None
  29. try:
  30. self.connet()
  31. self.cursor.execute(sql) # 执行sql语句
  32. res = self.cursor.fetchall() # 返回查询所有结果
  33. except Exception as e:
  34. print('查询失败:%s' % e)
  35. finally:
  36. self.close()
  37. return res
  38.  
  39. # 增加、删除、修改
  40. def shell_sql(self, sql):
  41. "执行sql语句"
  42. print(sql)
  43. count = 0
  44. try:
  45. self.connet()
  46. count = self.cursor.execute(sql) # 执行sql语句
  47. self.db.commit() # 提交
  48. except Exception as e:
  49. print('事务提交失败:%s' % e)
  50. self.db.rollback() # 如果提交失败,回滚到上一次数据
  51. finally:
  52. self.close()
  53. return count
  54.  
  55. def router_docx(choice1='', choice2='', choice3='', choice5='', choice6='', choice7='',paper_path='',name='1'):
  56. "生成网络通信方向试题及答案"
  57. docx1 = Document()
  58. docx2 = Document()
  59. docx1.styles['Normal'].font.name = '宋体' #选择字体
  60. docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #默认字体
  61. docx1.styles['Normal'].font.size = Pt(11) #默认字号大小
  62. docx1.styles['Normal'].paragraph_format.space_before = Pt(0) #默认段前间距
  63. docx1.styles['Normal'].paragraph_format.space_after = Pt(0) #默认段后间距
  64. docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默认单倍行距
  65. sec = docx1.sections[0] # sections对应文档中的“节”
  66. sec.left_margin = Inches(1) # 设置左页面边距
  67. sec.right_margin = Inches(1) #设置右页面边距
  68. sec.top_margin = Inches(0.5) # 设置上页面边距
  69. sec.bottom_margin = Inches(0.5) #设置下页面边距
  70.  
  71. p=docx1.add_paragraph() #添加段落
  72. run = p.add_run('软件测试(网络通信)方向试题(%s)' % name) #使用add_run添加文字
  73. run.font.name = '微软雅黑' #设置字体
  74. run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体
  75. run.font.size = Pt(18) #字体大小设置
  76. p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置
  77. docx1.add_paragraph('【说明】') # 添加段落文字
  78. docx1.add_paragraph('1.笔试时间为60分钟。')
  79. docx1.add_paragraph('2.请将答案写在答题卡上,且不允许在试题卷上做任何涂写和标记。')
  80. q=docx2.add_paragraph() #添加段落
  81. run = q.add_run('软件测试(网络通信)方向试题答案(%s)' % name) #使用add_run添加文字
  82. run.font.name = '微软雅黑' #设置字体
  83. run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体
  84. run.font.size = Pt(18) #字体大小设置
  85. q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置
  86.  
  87. p1 = docx1.add_paragraph()
  88. p1.paragraph_format.space_before = Pt(12) #设置段前间距
  89. docx2.add_paragraph('一、选择题')
  90. run = p1.add_run('一、选择题(每题3分共45分)')
  91. run.bold = True # 字体加粗
  92. list1=random.sample(range(0,len(choice1)-1),3) #len范围内获取指定的数量
  93. x=1
  94. for y in list1:
  95. docx1.add_paragraph(str(x)+'、'+choice1[y][1])
  96. docx1.add_paragraph(choice1[y][2])
  97. docx1.add_paragraph(choice1[y][3])
  98. docx1.add_paragraph(choice1[y][4])
  99. p11=docx1.add_paragraph(choice1[y][5])
  100. p11.paragraph_format.space_after = Pt(12) #段后间距
  101. docx2.add_paragraph(str(x)+'、'+choice1[y][6])
  102. x+=1
  103.  
  104. list2=random.sample(range(0,len(choice2)-1),7)
  105. x=1
  106. for y in list2:
  107. docx1.add_paragraph(str(x+3)+'、'+choice2[y][1])
  108. docx1.add_paragraph(choice2[y][2])
  109. docx1.add_paragraph(choice2[y][3])
  110. docx1.add_paragraph(choice2[y][4])
  111. p11=docx1.add_paragraph(choice2[y][5])
  112. p11.paragraph_format.space_after = Pt(12)
  113. docx2.add_paragraph(str(x+3)+'、'+choice2[y][6])
  114. x+=1
  115.  
  116. list3=random.sample(range(0,len(choice3)-1),5)
  117. x=1
  118. for y in list3:
  119. docx1.add_paragraph(str(x+10)+'、'+choice3[y][1])
  120. docx1.add_paragraph(choice3[y][2])
  121. docx1.add_paragraph(choice3[y][3])
  122. docx1.add_paragraph(choice3[y][4])
  123. p11=docx1.add_paragraph(choice3[y][5])
  124. p11.paragraph_format.space_after = Pt(12)
  125. docx2.add_paragraph(str(x+10)+'、'+choice3[y][6])
  126. x+=1
  127.  
  128. p2 = docx1.add_paragraph()
  129. p2.paragraph_format.space_before = Pt(12)
  130. docx2.add_paragraph('二、填空题')
  131. run = p2.add_run('二、填空题(每题3分,共15分)')
  132. run.bold = True
  133. list2 = random.sample(range(0, len(choice5)-1), 5)
  134. i = 1
  135. for j in list2:
  136. docx1.add_paragraph(str(i) + '、' + choice5[j][1])
  137. docx2.add_paragraph(str(i) + '、' + str(choice5[j][2]))
  138. i += 1
  139.  
  140. p3 = docx1.add_paragraph()
  141. p3.paragraph_format.space_before = Pt(12)
  142. docx2.add_paragraph('三、简答题')
  143. run = p3.add_run('三、简答题(每题10分,共20分)')
  144. run.bold = True
  145. list3 = random.sample(range(0, len(choice6)-1), 2)
  146. n = 1
  147. for m in list3:
  148. docx1.add_paragraph(str(n) + '、' + choice6[m][1])
  149. docx1.add_paragraph('\r')
  150. docx2.add_paragraph(str(n) + '、' + choice6[m][2])
  151. n += 1
  152.  
  153. p4 = docx1.add_paragraph()
  154. p4.paragraph_format.space_before = Pt(12)
  155. docx2.add_paragraph('四、综合题')
  156. run = p4.add_run('四、综合题(共20分)')
  157. run.bold = True
  158. list4 = random.randint(0, len(choice7)-1)
  159. docx1.add_paragraph('1、' + choice7[list4][1])
  160. docx2.add_paragraph(choice7[list4][2])
  161.  
  162. docx1.save(os.path.join(paper_path, '网络通信试题(%s).docx' % name)) #保存试题
  163. docx2.save(os.path.join(paper_path, '网络通信试题答案(%s).docx' % name)) #保存答案
  164.  
  165. def android_docx(choice1, choice2, choice4, choice5, choice6, choice8,paper_path,name):
  166. """生成智能终端方向的试题"""
  167. docx1 = Document()
  168. docx2 = Document()
  169. docx1.styles['Normal'].font.name = '宋体' #选择字体
  170. docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') #默认字体
  171. docx1.styles['Normal'].font.size = Pt(11) #默认字号大小
  172. docx1.styles['Normal'].paragraph_format.space_before = Pt(0) #默认段前间距
  173. docx1.styles['Normal'].paragraph_format.space_after = Pt(0) #默认段后间距
  174. docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默认单倍行距
  175. sec = docx1.sections[0] # sections对应文档中的“节”
  176. sec.left_margin = Inches(1) # 设置左页面边距
  177. sec.right_margin = Inches(1) #设置右页面边距
  178. sec.top_margin = Inches(0.5) # 设置上页面边距
  179. sec.bottom_margin = Inches(0.5) #设置下页面边距
  180.  
  181. p=docx1.add_paragraph() #添加段落
  182. run = p.add_run('软件测试(智能终端)方向试题(%s)' % name) #使用add_run添加文字
  183. run.font.name = '微软雅黑' #设置字体
  184. run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') #设置字体
  185. run.font.size = Pt(18) #字体大小设置
  186. p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER #段落文字居中设置
  187. docx1.add_paragraph('【说明】') # 添加段落文字
  188. docx1.add_paragraph('1.笔试时间为60分钟。')
  189. docx1.add_paragraph('2.请将答案写在答题卡上,且不允许在试题卷上做任何涂写和标记。')
  190. q = docx2.add_paragraph() # 添加段落
  191. run = q.add_run('软件测试(智能终端)方向试题答案(%s)' % name) # 使用add_run添加文字
  192. run.font.name = '微软雅黑' # 设置字体
  193. run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') # 设置字体
  194. run.font.size = Pt(18) # 字体大小设置
  195. q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 段落文字居中设置
  196.  
  197. p1 = docx1.add_paragraph()
  198. p1.paragraph_format.space_before = Pt(12) #设置段前间距
  199. docx2.add_paragraph('一、选择题')
  200. run = p1.add_run('一、选择题(每题3分共45分)')
  201. run.bold = True # 字体加粗
  202. list1=random.sample(range(0,len(choice1)-1),3)
  203. x=1
  204. for y in list1:
  205. docx1.add_paragraph(str(x)+'、'+choice1[y][1])
  206. docx1.add_paragraph(choice1[y][2])
  207. docx1.add_paragraph(choice1[y][3])
  208. docx1.add_paragraph(choice1[y][4])
  209. p11=docx1.add_paragraph(choice1[y][5])
  210. p11.paragraph_format.space_after = Pt(12) #段后间距
  211. docx2.add_paragraph(str(x)+'、'+choice1[y][6])
  212. x+=1
  213.  
  214. list2=random.sample(range(0,len(choice2)-1),7)
  215. x=1
  216. for y in list2:
  217. docx1.add_paragraph(str(x+3)+'、'+choice2[y][1])
  218. docx1.add_paragraph(choice2[y][2])
  219. docx1.add_paragraph(choice2[y][3])
  220. docx1.add_paragraph(choice2[y][4])
  221. p11=docx1.add_paragraph(choice2[y][5])
  222. p11.paragraph_format.space_after = Pt(12)
  223. docx2.add_paragraph(str(x+3)+'、'+choice2[y][6])
  224. x+=1
  225.  
  226. list3=random.sample(range(0,len(choice4)-1),5)
  227. x=1
  228. for y in list3:
  229. docx1.add_paragraph(str(x+10)+'、'+choice4[y][1])
  230. docx1.add_paragraph(choice4[y][2])
  231. docx1.add_paragraph(choice4[y][3])
  232. docx1.add_paragraph(choice4[y][4])
  233. p11=docx1.add_paragraph(choice4[y][5])
  234. p11.paragraph_format.space_after = Pt(12)
  235. docx2.add_paragraph(str(x+10)+'、'+choice4[y][6])
  236. x+=1
  237.  
  238. p2 = docx1.add_paragraph()
  239. p2.paragraph_format.space_before = Pt(12)
  240. docx2.add_paragraph('二、填空题')
  241. run = p2.add_run('二、填空题(每题3分,共15分)')
  242. run.bold = True
  243. list2 = random.sample(range(0, len(choice5)-1), 5)
  244. i = 1
  245. for j in list2:
  246. docx1.add_paragraph(str(i) + '、' + choice5[j][1])
  247. docx2.add_paragraph(str(i) + '、' + str(choice5[j][2]))
  248. i += 1
  249.  
  250. p3 = docx1.add_paragraph()
  251. p3.paragraph_format.space_before = Pt(12)
  252. docx2.add_paragraph('三、简答题')
  253. run = p3.add_run('三、简答题(每题10分,共20分)')
  254. run.bold = True
  255. list3 = random.sample(range(0, len(choice6)-1), 2)
  256. n = 1
  257. for m in list3:
  258. docx1.add_paragraph(str(n) + '、' + choice6[m][1])
  259. docx1.add_paragraph('\r')
  260. docx2.add_paragraph(str(n) + '、' + choice6[m][2])
  261. n += 1
  262.  
  263. p4 = docx1.add_paragraph()
  264. p4.paragraph_format.space_before = Pt(12)
  265. docx2.add_paragraph('四、综合题')
  266. run = p4.add_run('四、综合题(共20分)')
  267. run.bold = True
  268. list4 = random.randint(0, len(choice8)-1)
  269. docx1.add_paragraph('1、' + choice8[list4][1])
  270. docx2.add_paragraph(choice8[list4][2])
  271.  
  272. docx1.save(os.path.join(paper_path, '智能终端试题(%s).docx' % name))
  273. docx2.save(os.path.join(paper_path, '智能终端试题答案(%s).docx' % name))
  274.  
  275. def main(ip,name,passwd,db_name):
  276. paper_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '试卷') #试卷存放路径
  277. if not os.path.exists(paper_path):
  278. os.mkdir(paper_path) #创建试卷文件夹
  279. my = SunckSql(ip,name,passwd,db_name) #连接数据库
  280. choice1 = my.get_all("select * from %s" % '计算机基础选择题') #查询数据库中的试题
  281. choice2 = my.get_all("select * from %s" % '测试基础选择题')
  282. choice3 = my.get_all("select * from %s" % '网络通信选择题')
  283. choice4 = my.get_all("select * from %s" % '智能终端选择题')
  284. choice5 = my.get_all("select * from %s" % '填空题')
  285. choice6 = my.get_all("select * from %s" % '简答题')
  286. choice7 = my.get_all("select * from %s" % '网络通信综合题')
  287. choice8 = my.get_all("select * from %s" % '智能终端综合题')
  288. for i in range(1,4): #同时生成3份试卷及答案
  289. router_docx(choice1, choice2, choice3, choice5, choice6, choice7, paper_path, i)
  290. android_docx(choice1, choice2, choice4, choice5, choice6, choice8, paper_path, i)
  291.  
  292. if __name__ == "__main__":
  293. main(ip='数据库ip地址', name='mysql账号', passwd='mysql密码', db_name='软件测试试题库')

到此这篇关于Python+MySQL随机试卷及答案生成程序的文章就介绍到这了,更多相关Python MySQL随机试卷内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号