要安装的库:pywin32, python-docx
选择题库文件word后,就能开始刷题了。
导入题库时会自动生成一个docx文件和json文件,想要重新导入就删除json文件。
"""
@author: Bre Athy
@contact: https://www.zhihu.com/people/you-yi-shi-de-hu-xi
@productware: PyCharm
@file: 刷题.py
@time: 2020/1/1 22:28
"""
# 要安装的库:pywin32, python-docx
from docx import Document
from win32com.client import Dispatch as DPT
import os, re, json, copy, randomdef main():print("导入题库前请先关闭word!")wordpath = input("请输入题库word文档的位置:")if os.path.exists(os.path.splitext(wordpath)[0]+".json"):with open(os.path.splitext(wordpath)[0] + ".json", "r") as fp:Qs = json.load(fp)else:if os.path.splitext(wordpath)[1] == ".doc":print("检测到文件格式为doc,正在转为docx")word = DPT("Word.Application")doc = word.Documents.Open(wordpath)wordpath += "x"doc.SaveAs(wordpath, 12)doc.Close()word.Quit()print("转格式成功,文件存储为",wordpath)word = Document(wordpath)i = 1Qs = []Q = {}mcq = Falseif input("是否导入多选题(Y/N)?").upper() == "y":mcq = Truefor par in word.paragraphs:# 处理answer的函数,添加到列表中def parseresult(regex, content, item, over = False, answer = False):result = re.match(regex, content)if result:Q[item] = result.group(1).strip()if answer:if len(Q[item])>1 and mcq:Qs.append(copy.deepcopy(Q))Q.clear()if over:Qs.append(copy.deepcopy(Q))# print(Q)Q.clear()if par.paragraph_format.alignment == 1:if "多选题" not in par.text:print("导入"+par.text)i = 1else:result = re.match(".*?(\d+)"+r"[\..]"+"(.*)",par.text)if result:Q={"Item": result.group(1),"Question" : result.group(2)}i += 1parseresult(".*\(A\)(.*)", par.text, "A")parseresult(".*\(B\)(.*)", par.text, "B")parseresult(".*\(C\)(.*)", par.text, "C")parseresult(".*\(D\)(.*)", par.text, "D")parseresult(".*\(B\)(.*)", par.text, "B")parseresult(".*\(A\)(.*)\(B", par.text, "A")parseresult(".*\(B\)(.*)\(C", par.text, "B")parseresult(".*\(C\)(.*)\(D", par.text, "C")parseresult(".*A\.(.*)", par.text, "A")parseresult(".*B\.(.*)", par.text, "B")parseresult(".*C\.(.*)", par.text, "C")parseresult(".*D\.(.*)", par.text, "D")parseresult(".*A\.(.*?)B\.", par.text, "A")parseresult(".*B\.(.*?)C\.", par.text, "B")parseresult(".*C\.(.*?)D\.", par.text, "C")parseresult("答.*?[::].*?(\w+).*", par.text, "Answer", answer=True)parseresult("答.*?[::].*?(\w+).*知", par.text, "Answer", answer=True)parseresult(".*?知.*:(.*)", par.text, "knowledge", over=True)with open(os.path.splitext(wordpath)[0] + ".json", "w") as fp:json.dump(Qs, fp)print("题库导入完成!")result = input("1、随机出题\n2、顺序出题\n请选择:")print("******************** 按下回车进入下一题 ********************")# 取出一道题def getQ(Q):input()def pprint(des, item):if item in Q.keys():print(des, Q[item])if len(Q["Answer"]) == 1:if ("A" in Q["Answer"]) or ("B" in Q["Answer"]) or ("C" in Q["Answer"]) or ("D" in Q["Answer"]):print("******************** 单选题 ********************")else:print("******************** 判断题 ********************")else:print("******************** 多选题 ********************")if "Item" not in Q.keys():return Noneprint(str(Q["Item"]) + ". " + Q["Question"])pprint("A.", "A")pprint("B.", "B")pprint("C.", "C")pprint("D.", "D")if input("请选择:").upper() == Q["Answer"]:print("正确!")else:pprint("错误!正确答案:", "Answer")pprint("知识点:", "knowledge")if result == "2":for Q in Qs:getQ(Q)elif result == "1":while True:getQ(random.choice(Qs))else:print("未选择,自动退出!")if __name__ == "__main__":main()
第一次导入比较慢,第二次就打开就很快了了: