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

Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷

 更新時間:2021年01月14日 14:43:44   作者:瘋狂的機器人  
這篇文章主要介紹了Python如何從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一、背景

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

二、準備工作

1.安裝Python3

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

2.安裝庫

pip install python-docx==0.8.10

pip install PyMySQL==1.0.2

3.試題庫.xlsx

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

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

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

三、代碼

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) # 連接數(shù)據(jù)庫
    self.cursor = self.db.cursor() # 獲取操作游標

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

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

  # 增加、刪除、修改
  def shell_sql(self, sql):
    "執(zhí)行sql語句"
    print(sql)
    count = 0
    try:
      self.connet()
      count = self.cursor.execute(sql) # 執(zhí)行sql語句
      self.db.commit() # 提交
    except Exception as e:
      print('事務提交失敗:%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)絡通信方向試題及答案"
  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對應文檔中的“節(jié)”
  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('軟件測試(網(wǎng)絡通信)方向試題(%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('軟件測試(網(wǎng)絡通信)方向試題答案(%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范圍內(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('三、簡答題')
  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, '網(wǎng)絡通信試題(%s).docx' % name))       #保存試題
  docx2.save(os.path.join(paper_path, '網(wǎng)絡通信試題答案(%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對應文檔中的“節(jié)”
  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)                            #創(chuàng)建試卷文件夾
  my = SunckSql(ip,name,passwd,db_name)                     #連接數(shù)據(jù)庫
  choice1 = my.get_all("select * from %s" % '計算機基礎選擇題')           #查詢數(shù)據(jù)庫中的試題
  choice2 = my.get_all("select * from %s" % '測試基礎選擇題')
  choice3 = my.get_all("select * from %s" % '網(wǎng)絡通信選擇題')
  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" % '網(wǎng)絡通信綜合題')
  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='數(shù)據(jù)庫ip地址', name='mysql賬號', passwd='mysql密碼', db_name='軟件測試試題庫')

以上就是Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷的詳細內(nèi)容,更多關于python MySQL抽取題目生成試卷的資料請關注腳本之家其它相關文章!

相關文章

  • Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析

    Python實現(xiàn)微信好友數(shù)據(jù)爬取及分析

    這篇文章會基于Python對微信好友進行數(shù)據(jù)分析,這里選擇的維度主要有:性別、頭像、簽名、位置,主要采用圖表和詞云兩種形式來呈現(xiàn)結果,其中,對文本類信息會采用詞頻分析和情感分析兩種方法,感興趣的小伙伴可以了解一下
    2021-12-12
  • 純numpy數(shù)值微分法實現(xiàn)手寫數(shù)字識別

    純numpy數(shù)值微分法實現(xiàn)手寫數(shù)字識別

    本文主要介紹了純numpy數(shù)值微分法實現(xiàn)手寫數(shù)字識別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • pycharm調(diào)試時顯示圖片問題的解決

    pycharm調(diào)試時顯示圖片問題的解決

    這篇文章主要介紹了pycharm調(diào)試時顯示圖片問題的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • 基于PyQt5完成pdf轉(zhuǎn)word功能

    基于PyQt5完成pdf轉(zhuǎn)word功能

    本文介紹的pdf轉(zhuǎn)word功能還有一些待完善地方,例如可增加預覽功能,實現(xiàn)每頁預覽,當然我們可以在后續(xù)階段逐漸完善,對基于PyQt5完成的pdf轉(zhuǎn)word功能感興趣的朋友一起看看吧
    2022-06-06
  • Python算法練習之二分查找算法的實現(xiàn)

    Python算法練習之二分查找算法的實現(xiàn)

    二分查找也稱折半查找(Binary Search),它是一種效率較高的查找方法。本文將介紹python如何實現(xiàn)二分查找算法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2022-06-06
  • python爬蟲selenium和phantomJs使用方法解析

    python爬蟲selenium和phantomJs使用方法解析

    這篇文章主要介紹了python爬蟲selenium和phantomJs使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • Python異步執(zhí)行CMD命令的具體實現(xiàn)

    Python異步執(zhí)行CMD命令的具體實現(xiàn)

    異步執(zhí)行CMD命令是提高Python程序性能的有效方法,本文就來介紹一下Python異步執(zhí)行CMD命令的具體實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • 對python中的argv和argc使用詳解

    對python中的argv和argc使用詳解

    今天小編就為大家分享一篇對python中的argv和argc使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python input函數(shù)使用實例解析

    Python input函數(shù)使用實例解析

    這篇文章主要介紹了Python input函數(shù)使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • 一文學會Python列表list的使用

    一文學會Python列表list的使用

    這篇文章主要為大家介紹了Python列表list的使用全面解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06

最新評論

潼南县| 岳阳市| 墨玉县| 富裕县| 玛曲县| 肇庆市| 阿鲁科尔沁旗| 桂东县| 永定县| 台山市| 郴州市| 塘沽区| 公安县| 府谷县| 澳门| 鄂州市| 漳平市| 西畴县| 布尔津县| 佛学| 祥云县| 玉树县| 丰原市| 林州市| 定兴县| 井研县| 湖州市| 伊川县| 昌宁县| 平顶山市| 信宜市| 兴山县| 梧州市| 高陵县| 奇台县| 建宁县| 象州县| 莱西市| 崇阳县| 大兴区| 都昌县|