92 lines
2.9 KiB
Python
92 lines
2.9 KiB
Python
import os,re,json,time,sys
|
|
|
|
|
|
|
|
"""---设置目标及题块编号---"""
|
|
|
|
lessonid = "K0103"
|
|
title = "集合的关系"
|
|
problems = "15:30"
|
|
|
|
"""---设置结束---"""
|
|
|
|
|
|
template_file = "模板文件/备课草稿模板.txt" #设置模板文件名
|
|
exec_list = [("标题替换",lessonid+title)] #设置讲义标题
|
|
destination_file = "临时文件/"+lessonid+title+"草稿" # 设置输出文件名
|
|
|
|
|
|
|
|
#生成数码列表, 逗号分隔每个区块, 区块内部用:表示整数闭区间
|
|
def generate_number_set(string):
|
|
string = re.sub(r"[\n\s]","",string)
|
|
string_list = string.split(",")
|
|
numbers_list = []
|
|
for s in string_list:
|
|
if not ":" in s:
|
|
numbers_list.append(s.zfill(6))
|
|
else:
|
|
start,end = s.split(":")
|
|
for ind in range(int(start),int(end)+1):
|
|
numbers_list.append(str(ind).zfill(6))
|
|
return numbers_list
|
|
|
|
|
|
#读取题库json文件并转化为字典
|
|
with open(r"../题库0.3/Problems.json","r",encoding = "utf8") as f:
|
|
database = f.read()
|
|
pro_dict = json.loads(database)
|
|
|
|
#读取目标数据库json并转化为字典
|
|
with open(r"../题库0.3/LessonObj.json","r",encoding = "utf8") as f:
|
|
database = f.read()
|
|
obj_dict = json.loads(database)
|
|
|
|
#读取系统日期
|
|
current_time = time.localtime()
|
|
time_string = "_"+str(current_time.tm_year).zfill(4)+str(current_time.tm_mon).zfill(2)+str(current_time.tm_mday).zfill(2)
|
|
|
|
#生成目标文件名和目标文件目录
|
|
latex_file = destination_file + time_string + ".tex"
|
|
d = re.search("/[^/]*$",destination_file).span()[0]
|
|
destination_dir = destination_file[:d]
|
|
|
|
#读取模板
|
|
with open(template_file,"r",encoding="utf8") as f:
|
|
latex_raw = f.read()
|
|
|
|
#识别操作系统
|
|
if sys.platform != "win32":
|
|
latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw)
|
|
latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw)
|
|
|
|
#预处理
|
|
for command in exec_list:
|
|
latex_raw = re.sub(command[0],command[1],latex_raw)
|
|
|
|
data_output = latex_raw
|
|
|
|
obj_data = ""
|
|
for id in obj_dict:
|
|
if lessonid in id:
|
|
obj_data += "\n\\item %s: %s\n"%(id,obj_dict[id]["content"])
|
|
|
|
problems_data = ""
|
|
for id in generate_number_set(problems):
|
|
if id in pro_dict:
|
|
problems_data += "\n\\item (%s) %s\\\\\n来源: %s\\\\\n参考答案: %s\\\\\n关联目标: %s\\\\\n"%(id,pro_dict[id]["content"],pro_dict[id]["origin"],pro_dict[id]["ans"],",".join(pro_dict[id]["objs"]))
|
|
|
|
|
|
|
|
data_output = data_output.replace("目标待替换",obj_data)
|
|
print("目标生成完毕.")
|
|
data_output = data_output.replace("题目待替换",problems_data)
|
|
print("题目生成完毕.")
|
|
|
|
#保存和编译latex文件
|
|
with open(latex_file,"w",encoding = "utf8") as f:
|
|
f.write(data_output)
|
|
print("开始编译pdf文件: ", latex_file)
|
|
os.system("xelatex -interaction=batchmode -output-directory=" + destination_dir + " "+ latex_file)
|
|
print(os.system("xelatex -interaction=batchmode -output-directory=" + destination_dir + " "+ latex_file))
|