texfile = r"D:\mathdeptv2\工具\临时文件\高一区统考_学生用_20230421.tex"
excelfile = r"C:\Users\weiye\Documents\wwy sync\临时工作区\高一区统考.xlsx"
date = "20230418"
grade = "2025届高一"

sheetname = "难度统计"
max_classnum = 12

import re
import pandas as pd

df = pd.read_excel(excelfile,sheet_name = sheetname)
mark_cols = [i for i in range(2,31)]
with open(texfile,"r",encoding="u8") as f:
    data = f.read()
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 = ""

#生成题号(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(i) == str(col)[:len(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:
        if 1<=int(row)<=12:
            classrows_dict[list(df.iloc[:,0]).index(row)] = str(row).zfill(2) + "班"

for cl in classrows_dict:
    classname = grade + classrows_dict[cl]
    output += "[BEGIN]\n"
    output += "## "+date+"\n"
    output += "** "+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])
        output += id+"\t"+"\t".join(results) + "\n"
    output += "[END]\n\n\n"

with open("文本文件/手动统计结果.txt","w",encoding = "u8") as f:
    f.write(output)