From aa9578f1b05be7f3d81f86d176456f1cd34bcca8 Mon Sep 17 00:00:00 2001 From: "weiye.wang" Date: Mon, 5 Feb 2024 22:05:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20=E7=B3=BB=E5=88=97?= =?UTF-8?q?=E8=AE=B2=E4=B9=89=E7=94=9F=E6=88=90=EF=BC=88=E6=9B=BF=E4=BB=A3?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E7=9A=84=E8=AE=B2=E4=B9=89=E7=94=9F=E6=88=90?= =?UTF-8?q?=EF=BC=89=E5=8A=9F=E8=83=BD,=20=E5=8F=8A=E8=BE=85=E5=8A=A9?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=BA=9Bjson=E5=92=8Cdatabase=5Ftools?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 工具v2/database_tools.py | 88 +++++++++++++++++++++++++++++++++++++ 工具v2/文本文件/config.json | 17 +++++++ 工具v2/系列讲义生成.py | 56 +++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 工具v2/系列讲义生成.py diff --git a/工具v2/database_tools.py b/工具v2/database_tools.py index 76800b03..f8b7a1b8 100644 --- a/工具v2/database_tools.py +++ b/工具v2/database_tools.py @@ -887,6 +887,53 @@ def XeLaTeXCompile(filedir,filename): #在filedir目录中用XeLaTeX编译filena flagsuc = False return flagsuc # 若第二次编译成功则返回True, 否则返回False + +def GenerateSectionBodyString2024(problems,sectiontitles,pro_dict,obj_dict,bk_dict,misc,consecutivenumbering= True): #生成学生版的.tex文件的主体内容 + bodystring = "" + count = 0 + for i in range(len(problems)): + if problems[i][0] in "0123456789": + idlist = generate_number_set(problems[i],pro_dict) + sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\\setcounter{{enumi}}{{{count if consecutivenumbering else 0}}}\n\n" + for id in idlist: + count += 1 + sectionstring += generateLaTeXBodyContent(id,pro_dict,obj_dict,misc) + sectionstring += "\\end{enumerate}\n\n" + bodystring += sectionstring + if problems[i][0] == "K": + idlist = problems[i].split(",") + sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\n" + for objid in idlist: + sectionstring += f"\\item {objid} \\ {(obj_dict[objid]['content'] if objid in obj_dict else '目标编号有误')}\n\n" + sectionstring += "\\end{enumerate}\n\n" + bodystring += sectionstring + if problems[i][0] == "B": + idlist = problems[i].split(",") + sectionstring = f"\\section{{{sectiontitles[i]}}}\n\\begin{{enumerate}}\n\n" + for bkid in idlist: + if bkid in bk_dict: + bk = bk_dict[bkid]["content"] + else: + bk = bkid + " \\ 基础知识编号有误" + sectionstring += f"\\item {(bk)}\n\n" + sectionstring += "\\end{enumerate}\n\n" + bodystring += sectionstring + return bodystring #返回主题内容字符串 + +# if not objlist == []: +# output += "\\section{课时目标}\n\n" +# output += "\\begin{enumerate}\n\n" +# for objid in objlist: +# output += "\\item %s \\ %s \n\n"%(objid,objdict[objid]["content"]) +# output += "\\end{enumerate}\n\n" #生成学习目标 +# if not bnlist == []: +# output += "\\section{双基梳理}\n\n" +# basic_body = "" +# for bnid in bnlist: +# basic_body += "\\item %s\n\n"%basicknowledgedict[bnid]["content"] +# output += "\\begin{enumerate}\n\n %s\n\n\\end{enumerate}\n\n"%basic_body #生成基础知识梳理 + + def GenerateStudentBodyString(problems,sectiontitles,pro_dict,consecutivenumbering= True,answered= True,spaceflag=True): #生成学生版的.tex文件的主体内容 bodystring = "" if len(problems) == len(sectiontitles): @@ -1499,6 +1546,47 @@ def select_grade_from_pro_dict(prodict,grades): adict[id]["usages"] = new_usages.copy() return adict +def GenerateSingleLessonNote2024(id,notesdict,pro_dict,obj_dict,bk_dict,templatepath,outputfilepath,misc,consecutivenumbering = False): #20231215版讲义生成 + notetitle = id + r" \ " + notesdict["notes"][id]["name"] + structure = notesdict["structures"][id[0].upper()]["structure"] + note_contents = notesdict["notes"][id] + output = "" + sections_list = [] + problems_list = [] + for key in structure: + if not len(note_contents[key]) == 0: + sections_list.append(key) + problems_list.append(",".join(note_contents[key])) + rawoutput = GenerateSectionBodyString2024(problems=problems_list,sectiontitles=sections_list,pro_dict=pro_dict,obj_dict=obj_dict,bk_dict=bk_dict,misc=misc,consecutivenumbering= consecutivenumbering) + # rawoutput = GenerateStudentBodyString(problems=problems_list,sectiontitles=sections_list,pro_dict=metadict,consecutivenumbering= consecutivenumbering, answered= answered, spaceflag = True) + paragraphs = [p for p in rawoutput.split("\\section") if not p.strip() == ""] + for item in paragraphs: + sectionkey, content = re.findall(r"\{([\S]*)\}\n([\S\s]*)$",item)[0] + if not len(paragraphs) == 1: + output += "\\section{" + structure[sectionkey]["name"] + "}\n\n" + # if not structure[sectionkey]["spaceflag"] or answered: + # content = re.sub(r"\\vspace[\*]?\{[\S]*\}","\n",content) + output += content + "\n\n" + + latex_raw = ReadTextFile(templatepath) + 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) + + latex_data = StringSubstitute(r"<<[\s\S]*?待替换[\s\S]*?>>",latex_raw,(notetitle,output)) #替换标题和bodystring + SaveTextFile(latex_data,outputfilepath) #保存.tex文件 + + outputdir,filename = os.path.split(outputfilepath) + print(f"{filename}编译中...") + if XeLaTeXCompile(outputdir,filename): + print("编译成功") + return latex_data # 返回0 + else: + print("编译失败") + return latex_data # 返回有错误的latex源代码 + def GenerateSingleLessonNote(id,notesdict,metadict,templatepath,outputfilepath,consecutivenumbering = False, answered = False): #20231215版讲义生成 notetitle = id + r" \ " + notesdict["notes"][id]["name"] structure = notesdict["structures"][id[0].upper()]["structure"] diff --git a/工具v2/文本文件/config.json b/工具v2/文本文件/config.json index 5b84b7f3..4317d110 100644 --- a/工具v2/文本文件/config.json +++ b/工具v2/文本文件/config.json @@ -32,5 +32,22 @@ "备注": true, "届别": [] } + }, + "系列讲义生成.py": { + "讲义标题格式": "[EWGVI]202601\\d{2}", + "输出路径": "临时文件", + "教师版": true, + "字段显示设置": { + "题后空间": true, + "课时目标": true, + "题目标签": true, + "答案": true, + "解答与提示": true, + "使用记录": [3,-1], + "使用记录说明": "[a,b]表示显示最好的a个和最差b个, 有-2表示不显示, 无-2但有-1表示全部显示", + "来源": true, + "备注": true, + "届别": [] + } } } \ No newline at end of file diff --git a/工具v2/系列讲义生成.py b/工具v2/系列讲义生成.py new file mode 100644 index 00000000..8c4325ad --- /dev/null +++ b/工具v2/系列讲义生成.py @@ -0,0 +1,56 @@ +# lessonpattern = r"U20240501" # 正则表达式, 数据库中讲义的编号([A-Z][0-9]{4}[(01)|(02)][[0-9]{2}), 字母表示类型, 四位数字表示届别, 2位数字表示学期及其他, 2位数字表示序号 +# outputdir = "d:/temp/26届材料" # 输出文件夹, 不建议修改 +# answered = True # 设置是否编译答案 + +from database_tools import * + +configjson = load_dict("文本文件/config.json")["系列讲义生成.py"] + +lessonpattern = configjson["讲义标题格式"] # 正则表达式, 数据库中讲义的编号([A-Z][0-9]{4}[(01)|(02)][[0-9]{2}), 字母表示类型, 四位数字表示届别, 2位数字表示学期及其他, 2位数字表示序号 +outputdir = configjson["输出路径"] # 输出文件夹, 不建议修改 +# answered = configjson["提供答案"] # 设置是否编译答案 + + +jsonpath = "../备课组" #有json文件的根目录, 文件名需为"校本材料.json" +jsondicts = [] +for loc,dirs,files in os.walk(jsonpath): + if "校本材料.json" in files: + jsondicts.append(load_dict(os.path.join(loc,"校本材料.json"))) + + +pro_dict = load_dict("../题库0.3/problems.json") +obj_dict = load_dict("../题库0.3/LessonObj.json") +basicknowledge_dict = load_dict("../题库0.3/BasicKnowledge.json") + +dictionaries = {} #合并字典 +for t in (obj_dict,basicknowledge_dict,pro_dict): + dictionaries.update(t) + +papernames = [] +multitexdata = [] + +for notes_dict in jsondicts: + for lessonid in notes_dict["notes"]: + if re.findall(lessonpattern,lessonid) != []: + filename = notes_dict["notes"][lessonid]["id"]+notes_dict["notes"][lessonid]["filename"]+".tex" + papertype = lessonid[0] + consecutivenumbering = notes_dict["structures"][papertype]["consecutivenumbering"] + texdata = GenerateSingleLessonNote2024(id = lessonid,notesdict=notes_dict,pro_dict=pro_dict,obj_dict=obj_dict,bk_dict=basicknowledge_dict,templatepath=".\模板文件\讲义模板.txt",outputfilepath = os.path.join(outputdir,filename),misc=configjson,consecutivenumbering = consecutivenumbering) + # texdata = GenerateSingleLessonNote(id = lessonid,notesdict = notes_dict, metadict = dictionaries, templatepath = ".\模板文件\讲义模板.txt", outputfilepath = os.path.join(outputdir,filename),consecutivenumbering=consecutivenumbering,answered=answered) + papernames.append(notes_dict["notes"][lessonid]["id"]+" \\ "+notes_dict["notes"][lessonid]["name"]) + multitexdata.append(re.findall(r"\\begin{center}\n{\\bf\\large \\papername}\n\\end{center}([\s\S]*\\end\{enumerate\})",texdata)[0]) + + +merged = "" +for i in range(len(papernames)): + merged += "\n\n\\chapter{"+papernames[i]+"}\n\n\n" + merged += multitexdata[i] + +mergedtext = StringSubstitute(r"<<待替换[\d]+>>",ReadTextFile("./模板文件/合集模板.txt"),[merged]) + +SaveTextFile(mergedtext,os.path.join(outputdir,f"合集{GetDate()}.tex")) + +tocompile = input("需要编译合集吗?(Y/[N]):") +if tocompile[0].upper() == "Y": + XeLaTeXCompile(outputdir,f"合集{GetDate()}.tex") +