python實(shí)現(xiàn)某考試系統(tǒng)生成word試卷
本文實(shí)例為大家分享了python實(shí)現(xiàn)某考試系統(tǒng)生成word試卷的具體代碼,供大家參考,具體內(nèi)容如下
提示:寫完文章后,目錄可以自動(dòng)生成,如何生成可參考右邊的幫助文檔
準(zhǔn)備條件
1.試題excel信息,存放在名為data.xls的excel文件中

2.安裝python依賴的模塊信息
pip install xlrd pip install python-docx
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
編碼實(shí)現(xiàn)
#!/bin/bash env python
import xlrd
import random
from docx import Document
from docx.shared import Pt, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
#打開excel
data = xlrd.open_workbook('data.xls')
#獲取工作表
sheet = data.sheet_by_index(0)
class Question:
pass
def create_question():
question_list = []
for i in range(sheet.nrows):
if i>2:
#創(chuàng)建試題類
question = Question()
question.ID = sheet.cell(i,0).value
#添加試題的題目信息
question.subject = sheet.cell(i,1).value
#添加題目類型
question.question_type = sheet.cell(i,2).value
#添加試題選項(xiàng)
question.option = []
question.option.append(sheet.cell(i, 3).value) # A
question.option.append(sheet.cell(i, 4).value) # B
question.option.append(sheet.cell(i, 5).value) # C
question.option.append(sheet.cell(i, 6).value) # D
#添加分值
question.score = sheet.cell(i,7).value
question_list.append(question)
#將試卷題目隨機(jī)打亂并且返回
random.shuffle(question_list)
return question_list
def create_papper(file_name,paper_name,question_list):
#創(chuàng)建一個(gè)文檔對(duì)象
document = Document()
#設(shè)置頁眉的位置信息
section = document.sections[0]
header = section.header
p1 = header.paragraphs[0]
p1.text = paper_name
#設(shè)置頁腳信息
footer = section.footer
p2 = footer.paragraphs[0]
p2.text = '內(nèi)部試題,禁止泄露'
#寫入試卷基本信息
titile = document.add_heading(paper_name,level=1)
#設(shè)置對(duì)齊方式
titile.alignment = WD_ALIGN_PARAGRAPH.CENTER
#添加一個(gè)段落
p3 = document.add_paragraph()
p3.add_run('姓名:____')
p3.add_run('班級(jí):____')
p3.alignment = WD_ALIGN_PARAGRAPH.CENTER
#寫入試題信息
for i,question in enumerate(question_list):
subject_paragraph = document.add_paragraph() #添加一個(gè)段落
run = subject_paragraph.add_run(str(i+1)+str(question.subject)) #添加題目信息
run.bold = True #設(shè)置加粗
subject_paragraph.add_run('【%s】分'%str(question.score))
#打亂選項(xiàng)的順序
random.shuffle(question.option)
for index,option in enumerate(question.option):
document.add_paragraph(('ABCD')[index]+str(option))
#保存試題
document.save(file_name)
return
if __name__ == '__main__':
question_list = create_question()
#循環(huán)生成100份試卷
for item in range(1,100):
create_papper('2021第'+str(item)+'套內(nèi)部考試試題.docx','2021第一季度內(nèi)部考試',question_list)
print('over')
實(shí)現(xiàn)效果


總結(jié)
該案例綜合使用了xlrd模塊和python-docx模塊的一個(gè)讀寫練習(xí)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子
今天小編就為大家分享一篇使用Matplotlib 繪制精美的數(shù)學(xué)圖形例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
cv2.imread?和?cv2.imdecode?用法及區(qū)別
對(duì)于路徑中含有中文的圖像,直接用cv2.imread讀取會(huì)報(bào)錯(cuò),上次看到有大佬使用cv2.imdecode就可以正常讀取,有點(diǎn)好奇,所以今天來記錄下二者用法和區(qū)別,感興趣的朋友跟隨小編一起看看吧2023-02-02
Python實(shí)現(xiàn)提取指定名稱的文件并批量復(fù)制到其他文件夾
本文介紹基于Python語言,讀取一個(gè)文件夾,并將其中每一個(gè)子文件夾內(nèi)符合名稱要求的文件加以篩選,并將篩選得到的文件復(fù)制到另一個(gè)目標(biāo)文件夾中的方法,需要的朋友可以參考下2023-10-10
Python集成學(xué)習(xí)之Blending算法詳解
集成學(xué)習(xí)(又稱模型融合)就是結(jié)合若干個(gè)體分類器(基學(xué)習(xí)器)進(jìn)行綜合預(yù)測(cè),各個(gè)個(gè)體學(xué)習(xí)器通常是弱學(xué)習(xí)器.集成學(xué)習(xí)相較于個(gè)體學(xué)習(xí)在預(yù)測(cè)準(zhǔn)確率以及穩(wěn)定性上都有很大的提高.文中有非常詳細(xì)的代碼示例哦,需要的朋友可以參考下2021-05-05
windows下python虛擬環(huán)境virtualenv安裝和使用詳解
這篇文章主要介紹了windows下python虛擬環(huán)境virtualenv安裝和使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-07-07
python讀取excel數(shù)據(jù)并且畫圖的實(shí)現(xiàn)示例
這篇文章主要介紹了python讀取excel數(shù)據(jù)并且畫圖的實(shí)現(xiàn)示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02

