import os,re
import win32clipboard as wc
import win32con

# 获取剪切板内容
def getCopy():
    wc.OpenClipboard()
    t = wc.GetClipboardData(win32con.CF_UNICODETEXT)
    wc.CloseClipboard()
    return t

# 写入剪切板内容
def setCopy(str):
    wc.OpenClipboard()
    wc.EmptyClipboard()
    wc.SetClipboardData(win32con.CF_UNICODETEXT, str)
    wc.CloseClipboard()

def choicetoblankfilling(data):
    choiceslist = re.findall(r"(\\(?:two|four|one)ch[\s\S]*?)(?:\n[\s]*\\item|$)",data)
    for rawchoice in choiceslist:
        leftcount = 0
        rightcount = 0
        choicestring = rawchoice
        contentlist = [""]
        while len(choicestring) > 0:
            char = choicestring[0]
            if char == "{":
                leftcount += 1
            if char == "}":
                rightcount += 1
            if leftcount > rightcount:
                contentlist[-1]+=char
            if leftcount == rightcount and contentlist[-1] != "":
                contentlist[-1] += "}"
                contentlist.append("")
            choicestring = choicestring[1:]
        contentlist = [c[1:-1] for c in contentlist if c.strip()!=""]
        repstring = ""
        for t in range(len(contentlist)):
            repstring += r"\textcircled{"+str(t+1)+"} "+contentlist[t]+"; "
        repstring= repstring[:-2]+"."
        data = data.replace(rawchoice,repstring)
    data = re.sub(r"bracket\{\d*?\}\.*",r"blank{50}.\\\\",data)
    return data

data = getCopy()
modified_data = choicetoblankfilling(data)
setCopy(modified_data)

with open("临时文件/outputfile.txt","w",encoding = "utf8") as f:
    f.write(modified_data)