import os,re
"---此处输入文件夹列表---"
directories = [
r"C:\Users\wangweiye\Documents\wwy sync\23届\暑假概率初步续",   
r"C:\Users\wangweiye\Documents\wwy sync\23届\上学期测验卷",
r"C:\Users\wangweiye\Documents\wwy sync\23届\上学期周末卷",
r"C:\Users\wangweiye\Documents\wwy sync\23届\第一轮复习讲义",
r"C:\Users\wangweiye\Documents\wwy sync\23届\赋能",
r"C:\Users\wangweiye\Documents\wwy sync\23届\一模后春考前试卷备选",
r"C:\Users\wangweiye\Documents\wwy sync\23届\正态分布及成对数据新课",
r"C:\Users\wangweiye\Documents\wwy sync\23届\寒假作业",
r"C:\Users\wangweiye\Documents\wwy sync\23届\下学期测验卷",
r"C:\Users\wangweiye\Documents\wwy sync\23届\下学期周末卷",
r"C:\Users\wangweiye\Documents\wwy sync\23届\第二轮复习讲义",
r"C:\Users\wangweiye\Documents\wwy sync\23届\四月错题重做",
r"C:\Users\wangweiye\Documents\wwy sync\23届\简单题冲刺",
r"C:\Users\wangweiye\Documents\wwy sync\23届\中档题冲刺",
r"C:\Users\wangweiye\Documents\wwy sync\23届\双基冲刺卷",
r"C:\Users\wangweiye\Documents\wwy sync\23届\第三轮复习讲义"
]
"---文件夹列表输入结束---"

for directory in directories:
    filelist = [filename for filename in os.listdir(directory) if ".tex" in filename]

    output = ""
    outputv2 = ""

    for filename in filelist:
        print(os.path.join(directory,filename))
        output += filename + "\n"
        outputv2 += filename + "\n"
        with open(os.path.join(directory,filename),"r",encoding = "u8") as f:
            try:
                data = re.findall(r"\\begin{document}([\s\S]*?)\\end{document}",f.read())[0]
                data = data.replace(r"\section","endsecbeginsec") + "endsec"
                if not "beginsec" in data: 
                    data = "beginsec" + data
                sectionlist = re.findall(r"beginsec([\s\S]*?)endsec",data)
                for sec in sectionlist:
                    secname = re.findall(r"{([\S]*)}",sec)[0]
                    output += secname + "\n"
                    # print(secname)
                    v2idlist = []
                    for id in re.findall(r"\(\d{6}\)",sec):
                        # print(id)
                        v2idlist.append(id[1:-1])
                        output += id + "\n"
                    outputv2 += '"'+",".join(v2idlist)+'",'
            except:
                pass    
        output += "\n\n"
        outputv2 = outputv2[:-1] + "\n\n"
        # print("\n\n")

    with open(os.path.join(directory,"题号清点.txt"),"w",encoding = "u8") as f:
        f.write(output + "\n\n\n以下题号不含括号\n\n\n" + output.replace("(","").replace(")","")+outputv2)

                


                
            
