最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+MySQL隨機(jī)試卷及答案生成程序的示例代碼

 更新時(shí)間:2021年02月01日 11:45:30   作者:瘋狂的機(jī)器人  
這篇文章主要介紹了Python+MySQL隨機(jī)試卷及答案生成程序的示例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、背景

本文章主要是分享如何使用Python從MySQL數(shù)據(jù)庫(kù)中面抽取試題,生成的試卷每一份都不一樣。

二、準(zhǔn)備工作

1.安裝Python3

下載地址:https://www.python.org/downloads/windows/

2.安裝庫(kù)

pip installpython-docx==0.8.10

pip installPyMySQL==1.0.2

3.試題庫(kù).xlsx

開(kāi)發(fā)程序前需要先收集試題,本文是將試題收集存放MySQL數(shù)據(jù)庫(kù)中,格式如下:

選擇題數(shù)據(jù)庫(kù)截圖:

填空題/解答題/綜合題數(shù)據(jù)庫(kù)截圖:

三、代碼

Python+MySQL隨機(jī)試卷及答案生成程序.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) # 連接數(shù)據(jù)庫(kù)
  self.cursor = self.db.cursor() # 獲取操作游標(biāo)

 def close(self):
  self.cursor.close() # 釋放游標(biāo)
  self.db.close() # 關(guān)閉數(shù)據(jù)庫(kù)連接

 # 查詢
 def get_all(self, sql):
  res = None
  try:
   self.connet()
   self.cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
   res = self.cursor.fetchall() # 返回查詢所有結(jié)果
  except Exception as e:
   print('查詢失敗:%s' % e)
  finally:
   self.close()
  return res

 # 增加、刪除、修改
 def shell_sql(self, sql):
  "執(zhí)行sql語(yǔ)句"
  print(sql)
  count = 0
  try:
   self.connet()
   count = self.cursor.execute(sql) # 執(zhí)行sql語(yǔ)句
   self.db.commit() # 提交
  except Exception as e:
   print('事務(wù)提交失敗:%s' % e)
   self.db.rollback() # 如果提交失敗,回滾到上一次數(shù)據(jù)
  finally:
   self.close()
  return count

def router_docx(choice1='', choice2='', choice3='', choice5='', choice6='', choice7='',paper_path='',name='1'):
 "生成網(wǎng)絡(luò)通信方向試題及答案"
 docx1 = Document()
 docx2 = Document()
 docx1.styles['Normal'].font.name = '宋體'         #選擇字體
 docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋體') #默認(rèn)字體
 docx1.styles['Normal'].font.size = Pt(11)        #默認(rèn)字號(hào)大小
 docx1.styles['Normal'].paragraph_format.space_before = Pt(0)    #默認(rèn)段前間距
 docx1.styles['Normal'].paragraph_format.space_after = Pt(0)    #默認(rèn)段后間距
 docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默認(rèn)單倍行距
 sec = docx1.sections[0]             # sections對(duì)應(yīng)文檔中的“節(jié)”
 sec.left_margin = Inches(1)            # 設(shè)置左頁(yè)面邊距
 sec.right_margin = Inches(1)            #設(shè)置右頁(yè)面邊距
 sec.top_margin = Inches(0.5)            # 設(shè)置上頁(yè)面邊距
 sec.bottom_margin = Inches(0.5)           #設(shè)置下頁(yè)面邊距

 p=docx1.add_paragraph()             #添加段落
 run = p.add_run('軟件測(cè)試(網(wǎng)絡(luò)通信)方向試題(%s)' % name)      #使用add_run添加文字
 run.font.name = '微軟雅黑'             #設(shè)置字體
 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微軟雅黑')     #設(shè)置字體
 run.font.size = Pt(18)             #字體大小設(shè)置
 p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER     #段落文字居中設(shè)置
 docx1.add_paragraph('【說(shuō)明】')           # 添加段落文字
 docx1.add_paragraph('1.筆試時(shí)間為60分鐘。')
 docx1.add_paragraph('2.請(qǐng)將答案寫(xiě)在答題卡上,且不允許在試題卷上做任何涂寫(xiě)和標(biāo)記。')
 q=docx2.add_paragraph()             #添加段落
 run = q.add_run('軟件測(cè)試(網(wǎng)絡(luò)通信)方向試題答案(%s)' % name)     #使用add_run添加文字
 run.font.name = '微軟雅黑'             #設(shè)置字體
 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微軟雅黑')     #設(shè)置字體
 run.font.size = Pt(18)             #字體大小設(shè)置
 q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER     #段落文字居中設(shè)置

 p1 = docx1.add_paragraph()
 p1.paragraph_format.space_before = Pt(12)        #設(shè)置段前間距
 docx2.add_paragraph('一、選擇題')
 run = p1.add_run('一、選擇題(每題3分共45分)')
 run.bold = True               # 字體加粗
 list1=random.sample(range(0,len(choice1)-1),3)       #len范圍內(nèi)獲取指定的數(shù)量
 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('三、簡(jiǎn)答題')
 run = p3.add_run('三、簡(jiǎn)答題(每題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, '網(wǎng)絡(luò)通信試題(%s).docx' % name))    #保存試題
 docx2.save(os.path.join(paper_path, '網(wǎng)絡(luò)通信試題答案(%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'), '宋體')   #默認(rèn)字體
 docx1.styles['Normal'].font.size = Pt(11)          #默認(rèn)字號(hào)大小
 docx1.styles['Normal'].paragraph_format.space_before = Pt(0)     #默認(rèn)段前間距
 docx1.styles['Normal'].paragraph_format.space_after = Pt(0)      #默認(rèn)段后間距
 docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE #默認(rèn)單倍行距
 sec = docx1.sections[0]               # sections對(duì)應(yīng)文檔中的“節(jié)”
 sec.left_margin = Inches(1)              # 設(shè)置左頁(yè)面邊距
 sec.right_margin = Inches(1)             #設(shè)置右頁(yè)面邊距
 sec.top_margin = Inches(0.5)             # 設(shè)置上頁(yè)面邊距
 sec.bottom_margin = Inches(0.5)             #設(shè)置下頁(yè)面邊距

 p=docx1.add_paragraph()               #添加段落
 run = p.add_run('軟件測(cè)試(智能終端)方向試題(%s)' % name)        #使用add_run添加文字
 run.font.name = '微軟雅黑'              #設(shè)置字體
 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微軟雅黑')       #設(shè)置字體
 run.font.size = Pt(18)               #字體大小設(shè)置
 p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER      #段落文字居中設(shè)置
 docx1.add_paragraph('【說(shuō)明】')             # 添加段落文字
 docx1.add_paragraph('1.筆試時(shí)間為60分鐘。')
 docx1.add_paragraph('2.請(qǐng)將答案寫(xiě)在答題卡上,且不允許在試題卷上做任何涂寫(xiě)和標(biāo)記。')
 q = docx2.add_paragraph()              # 添加段落
 run = q.add_run('軟件測(cè)試(智能終端)方向試題答案(%s)' % name)       # 使用add_run添加文字
 run.font.name = '微軟雅黑'              # 設(shè)置字體
 run._element.rPr.rFonts.set(qn('w:eastAsia'), '微軟雅黑')       # 設(shè)置字體
 run.font.size = Pt(18)               # 字體大小設(shè)置
 q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER      # 段落文字居中設(shè)置

 p1 = docx1.add_paragraph()
 p1.paragraph_format.space_before = Pt(12)          #設(shè)置段前間距
 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('三、簡(jiǎn)答題')
 run = p3.add_run('三、簡(jiǎn)答題(每題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)              #創(chuàng)建試卷文件夾
 my = SunckSql(ip,name,passwd,db_name)           #連接數(shù)據(jù)庫(kù)
 choice1 = my.get_all("select * from %s" % '計(jì)算機(jī)基礎(chǔ)選擇題')      #查詢數(shù)據(jù)庫(kù)中的試題
 choice2 = my.get_all("select * from %s" % '測(cè)試基礎(chǔ)選擇題')
 choice3 = my.get_all("select * from %s" % '網(wǎng)絡(luò)通信選擇題')
 choice4 = my.get_all("select * from %s" % '智能終端選擇題')
 choice5 = my.get_all("select * from %s" % '填空題')
 choice6 = my.get_all("select * from %s" % '簡(jiǎn)答題')
 choice7 = my.get_all("select * from %s" % '網(wǎng)絡(luò)通信綜合題')
 choice8 = my.get_all("select * from %s" % '智能終端綜合題')
 for i in range(1,4):               #同時(shí)生成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='數(shù)據(jù)庫(kù)ip地址', name='mysql賬號(hào)', passwd='mysql密碼', db_name='軟件測(cè)試試題庫(kù)')

到此這篇關(guān)于Python+MySQL隨機(jī)試卷及答案生成程序的文章就介紹到這了,更多相關(guān)Python MySQL隨機(jī)試卷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離

    使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離

    這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)計(jì)算DICOM圖像兩點(diǎn)真實(shí)距離,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-11-11
  • 詳解Python sys.argv使用方法

    詳解Python sys.argv使用方法

    在本文中我們給大家詳細(xì)講解了關(guān)于Python sys.argv使用方法以及注意事項(xiàng),有此需要的讀者們跟著學(xué)習(xí)下。
    2019-05-05
  • pandas如何使用列表和字典創(chuàng)建?Series

    pandas如何使用列表和字典創(chuàng)建?Series

    這篇文章主要介紹了pandas如何使用列表和字典創(chuàng)建?Series,pandas 是基于NumPy的一種工具,該工具是為解決數(shù)據(jù)分析任務(wù)而創(chuàng)建的,下文我們就來(lái)看看文章是怎樣介紹pandas,需要的朋友也可以參考一下
    2021-12-12
  • 在cmd中查看python的安裝路徑方法

    在cmd中查看python的安裝路徑方法

    在本篇文章里小編給大家整理的是關(guān)于怎樣在cmd中查看python的安裝路徑的相關(guān)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • Python3.9.1中使用split()的處理方法(推薦)

    Python3.9.1中使用split()的處理方法(推薦)

    這篇文章主要介紹了Python3.9.1中使用split()的處理方法(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 使用pygame實(shí)現(xiàn)垃圾分類小游戲功能(已獲校級(jí)二等獎(jiǎng))

    使用pygame實(shí)現(xiàn)垃圾分類小游戲功能(已獲校級(jí)二等獎(jiǎng))

    這篇文章主要介紹了使用pygame實(shí)現(xiàn)垃圾分類小游戲功能(已獲校級(jí)二等獎(jiǎng)),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • pycharm的python_stubs問(wèn)題

    pycharm的python_stubs問(wèn)題

    這篇文章主要介紹了pycharm的python_stubs問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • Python實(shí)現(xiàn)的彈球小游戲示例

    Python實(shí)現(xiàn)的彈球小游戲示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的彈球小游戲,可實(shí)現(xiàn)類似乒乓球游戲的鍵盤(pán)控制底部擋板移動(dòng)碰撞小球的游戲功能,需要的朋友可以參考下
    2017-08-08
  • python 多線程將大文件分開(kāi)下載后在合并的實(shí)例

    python 多線程將大文件分開(kāi)下載后在合并的實(shí)例

    今天小編就為大家分享一篇python 多線程將大文件分開(kāi)下載后在合并的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • 淺談numpy庫(kù)的常用基本操作方法

    淺談numpy庫(kù)的常用基本操作方法

    下面小編就為大家分享一篇淺談numpy庫(kù)的常用基本操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論

昭觉县| 莲花县| 盐池县| 和龙市| 陕西省| 东阳市| 偏关县| 陆良县| 玛沁县| 朔州市| 德化县| 柳河县| 乐昌市| 沙河市| 清远市| 鄂伦春自治旗| 鲜城| 临漳县| 九江市| 仁寿县| 柳河县| 瑞金市| 扎赉特旗| 武冈市| 宜君县| 揭东县| 荣成市| 巴东县| 烟台市| 德钦县| 缙云县| 松滋市| 桐庐县| 江山市| 宁陕县| 基隆市| 兴宁市| 彩票| 韶关市| 阳曲县| 朝阳市|