From 803ec588cfc4b7a176a4272f386ba5ace5e032c9 Mon Sep 17 00:00:00 2001 From: "weiye.wang" Date: Mon, 5 Feb 2024 16:31:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E6=A0=B9=E6=8D=AE=E9=A2=98?= =?UTF-8?q?=E5=BA=93=E8=8E=B7=E5=BE=97=E5=90=84=E7=A7=8DLaTeX=E6=BA=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E5=86=85=E5=AE=B9=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0,=20=E6=96=B0=E5=A2=9E"=E5=A4=9A=E7=A7=8D=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E7=94=9F=E6=88=90.py"=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具v2/database_tools.py | 56 ++++++++++++++++++++++++++++++++++ 工具v2/多种题目生成.py | 60 +++++++++++++++++++++++++++++++++++++ 工具v2/文本文件/config.json | 20 +++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 工具v2/多种题目生成.py diff --git a/工具v2/database_tools.py b/工具v2/database_tools.py index c7de4008..76800b03 100644 --- a/工具v2/database_tools.py +++ b/工具v2/database_tools.py @@ -967,6 +967,62 @@ def GenerateUsageTexCode(id,prodict,topandbottomusagestuple): #根据topandbotto usagecode = re.sub("\\t([\d]\.[\d]{0,10})",GenerateValueColorCode,"\n\n".join(usages)) return usagecode #返回缩减后的使用记录列表 +def generateLaTeXBodyContent(id,adict,objdict,misc): #根据id,读取的json内容adict,和字典misc来生成讲义 +#misc 样例 +#{ +# "教师版": True, #如果设置为True则除了 题后空间 之外都进行判断并处理, 否则只处理 题后空间 和 答案 +# "字段显示设置": { +# "题后空间": True, +# "课时目标": True, +# "题目标签": True, +# "答案": True, +# "解答与提示": True, +# "使用记录": (3,-1), +# "使用记录说明": "(a,b)表示显示最好的a个和最差b个, 有-2表示不显示, 无-2但有-1表示全部显示", +# "来源": True, +# "备注": True, +# "届别": [] +# } +# } + id = str(id).zfill(6) + if misc["教师版"] == False: + output = f"\n\\item {{\\tiny ({id})}} {adict[id]['content']}" + if "答案" in misc["字段显示设置"] and misc["字段显示设置"]["答案"] == True: + ans = adict[id]["ans"] if len(adict[id]["ans"]) > 0 else "暂无答案" + output += f"\n\n答案: \\textcolor{{red}}{{{ans}}}\n\n" + if "题后空间" in misc["字段显示设置"] and misc["字段显示设置"]["题后空间"] == True: + space = f"\n\n\\vspace*{{{adict[id]['space']}}}\n\n" if len(adict[id]["space"]) > 0 else "" + output += space + else: + output = f"\n\\item ({id}) {adict[id]['content']}" + if "备注" in misc["字段显示设置"] and misc["字段显示设置"]["备注"] == True: + remark = adict[id]["remark"] if len(adict[id]["remark"]) > 0 else "暂无备注" + output += f"\n\n备注: \\textcolor[rgb]{{0,0.5,0.2}}{{{remark}}}\n\n" + if "课时目标" in misc["字段显示设置"] and misc["字段显示设置"]["课时目标"] == True: + objs = f"\n\n目标:\n\n{GenerateObjTexCode(id,adict,objdict)}\n\n" + output += objs + if "题目标签" in misc["字段显示设置"] and misc["字段显示设置"]["题目标签"] == True: + tags_raw = adict[id]['tags'] + if len(tags_raw) == 0: + tags = "暂无标签" + else: + tags = '; '.join(adict[id]['tags']) + output += f"\n\n标签: \\textcolor[rgb]{{0.5,0.6,0.8}}{{{tags}}}\n\n" + if "答案" in misc["字段显示设置"] and misc["字段显示设置"]["答案"] == True: + ans = adict[id]["ans"] if len(adict[id]["ans"]) > 0 else "暂无答案" + output += f"\n\n答案: \\textcolor{{red}}{{{ans}}}\n\n" + if "解答与提示" in misc["字段显示设置"] and misc["字段显示设置"]["解答与提示"] == True: + solution = adict[id]["solution"] if len(adict[id]["solution"]) > 0 else "暂无解答或提示" + output += f"\n\n解答或提示: \\textcolor{{magenta}}{{{solution}}}\n\n" + if "使用记录" in misc["字段显示设置"] and type(misc["字段显示设置"]["使用记录"]) in (list,tuple) and not -2 in misc["字段显示设置"]["使用记录"]: + usages = f"\n\n使用记录:\n\n{GenerateUsageTexCode(id,adict,misc['字段显示设置']['使用记录'])}\n\n" + output += usages + if "来源" in misc["字段显示设置"] and misc["字段显示设置"]["来源"] == True: + origin = adict[id]["origin"] if len(adict[id]["origin"]) > 0 else "未记录来源" + output += f"\n\n来源: {origin}\n\n" + return output + + def GenerateTeacherBodyString(problems,sectiontitles,prodict,objdict,consecutivenumbering = True,topandbottomusagestuple = (3,3),sectionname = "section", showobjs = True, showtags = True, showans = True, showsolution = True, showusages = True, showorigin = True, showremark = True, colored = False): #生成教师版的.tex文件的主体内容, 各项是否显示为可选 bodystring = "" GetAfterContent = TeachersGetAfterContentPlain if not colored else TeachersGetAfterContentColored diff --git a/工具v2/多种题目生成.py b/工具v2/多种题目生成.py new file mode 100644 index 00000000..4298e488 --- /dev/null +++ b/工具v2/多种题目生成.py @@ -0,0 +1,60 @@ +from database_tools import * +import time + + +configjson = load_dict("文本文件/config.json")["多种题目生成.py"] + + +prodictpath = "../题库0.3/problems.json" +objdictpath = "../题库0.3/lessonobj.json" +raw_pro_dict = load_dict(prodictpath) + +grades = configjson["字段显示设置"]["届别"] +pro_dict = select_grade_from_pro_dict(raw_pro_dict,grades) +obj_dict = load_dict(objdictpath) + +notetitle = configjson["pdf标题"] +outputdir = "临时文件" #输出文件的目录 +outputfilepath = os.path.join(outputdir,notetitle+".tex") +print("输出文件目录: %s\n输出文件名: %s"%(os.path.join(os.getcwd(),outputdir),notetitle+".tex")) + +latex_raw = ReadTextFile("模板文件/讲义模板.txt") +if configjson["教师版"] == True: + latex_raw = latex_raw.replace(r"学号\blank{50} \ 姓名\blank{80}","上海市控江中学") + +if sys.platform != "win32": #非win系统用默认字体 + latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw) + latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw) + +bodystring = "\\tableofcontents\n\n\\newpage\n\n" +bodylist = [] + + +problems_dict = configjson["标题与题号"] + +starttime = time.time() + +for p in problems_dict: + currentbodystring = f"\\section{{{p}}}\n\\begin{{enumerate}}\n\n" + for id in generate_number_set(problems_dict[p]): + if id in pro_dict: + currentbodystring += generateLaTeXBodyContent(id,pro_dict,obj_dict,configjson) + currentbodystring += "\\end{enumerate}" + bodylist.append(currentbodystring) +bodystring += "\n\n\\newpage\n\n".join(bodylist) + +midtime = time.time() +print(f"生成LaTeX文件所花时间: {midtime-starttime:.3f}秒") + +latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,(notetitle,bodystring)) #替换标题和bodystring +SaveTextFile(latex_data,outputfilepath) #保存.tex文件 + +if XeLaTeXCompile(outputdir,notetitle+".tex"): + print("编译成功") +else: + print("编译失败") + +endtime = time.time() +print(f"生成pdf文件所花时间: {endtime-midtime:.3f}秒") + +pass \ No newline at end of file diff --git a/工具v2/文本文件/config.json b/工具v2/文本文件/config.json index 0fa6de22..5b84b7f3 100644 --- a/工具v2/文本文件/config.json +++ b/工具v2/文本文件/config.json @@ -12,5 +12,25 @@ "讲义标题格式": "V202503\\d{2}", "输出路径": "临时文件", "提供答案": true + }, + "多种题目生成.py": { + "标题与题号": { + "第一讲": "5:25", + "第二讲": "20001:20030,70:80" + }, + "pdf标题": "测试题库", + "教师版": true, + "字段显示设置": { + "题后空间": true, + "课时目标": true, + "题目标签": true, + "答案": true, + "解答与提示": true, + "使用记录": [3,2], + "使用记录说明": "[a,b]表示显示最好的a个和最差b个, 有-2表示不显示, 无-2但有-1表示全部显示", + "来源": true, + "备注": true, + "届别": [] + } } } \ No newline at end of file