texfile = r"D:\mathdeptv2\工具\临时文件\临时_学生用_20230622.tex" # 试卷的.tex源文件(要求每个题号都是左右带有小括号的六位数, 且没有其他这种格式的内容)
excelfile = r"C:\Users\weiye\Documents\wwy sync\临时工作区\高一期末.xlsx" # 统考难度数据所在的excel文件, 小题号为从小到大排列的字符串, 例如 1 2 3 41 42 51 52 6 71 72 73等
sheetname = "难度统计" # excel中难度数据所在的工作表名
date = "20230620" # 考试日期
grade = "2025届高一" # 考试年级
max_classnum = 12 # 年级参加考试的最大班级班号
outputfilepath = r"文本文件/metadata.txt" # 输出的用于导入的metadata.txt文件位置
checkingfilepath = r"临时文件/手动统计结果.txt" # 用于检查结构的文件所在位置

from database_tools import *

df = pd.read_excel(excelfile,sheet_name = sheetname)
data = ReadTextFile(texfile)
problems_list = re.findall(r"\((\d{6})\)",data)

#生成题号(1~n)与题库id对应
id_dict = {}
for i in range(len(problems_list)):
    id_dict[i+1] = problems_list[i]

output = "usages\n\n"
checkoutput = ""
#生成题号(1~n)与表格中数据列的对应
idcol_dict = {}
for i in id_dict:
    idcol_dict[i] = []
    mincol = -1
    for col_index in range(mincol+1,len(df.columns)):
        col = df.columns[col_index]
        if str(col).startswith(str(i)):
            idcol_dict[i].append(col_index)
            mincol = col_index
            if str(i) == str(col):
                break

#生成行号与班级的对应列表
classrows_dict = {}
for row in df.iloc[:,0]:
    if type(row) == int or type(row) == float:
        if 1<=row<=12:
            row = int(row)
            classrows_dict[list(df.iloc[:,0]).index(row)] = str(row).zfill(2) + "班"

#生成手动统计列表
for cl in classrows_dict:
    classname = grade + classrows_dict[cl]
    checkoutput += "[BEGIN]\n"
    checkoutput += "## "+date+"\n"
    checkoutput += "** "+classname+"\n"
    for i in range(len(problems_list)):
        id = id_dict[i+1]
        results_cols = idcol_dict[i+1]
        results = []
        for col in results_cols:
            results.append("%.3f"%df.iloc[cl,col])
        checkoutput += id+"\t"+"\t".join(results) + "\n"
        output += id + "\n" + "\t".join([date,classname]+results) + "\n\n"
    checkoutput += "[END]\n\n\n"

SaveTextFile(output,outputfilepath)
print("可以在 %s 文件中检查结构"%(SaveTextFile(checkoutput,checkingfilepath)))
os.system("code -w %s"%checkingfilepath)