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 dollared(string):
    flag = True
    for c in string:
        if not c in "1234567890.+-:[]()":
            flag = False
            break
    if flag:
        string = "$" + string + "$"
    return string



data = getCopy()

data1 = ""
for c in data:
    if 65296 <= ord(c) < 65306:
        data1 += str(ord(c)-65296)
    else:
        data1 += c
data = data1
data = data.replace("．",".").replace("：",":")
elements = [l for l in data.split("\n") if len(l)>0]
elements_per_line = int(elements.pop(0)) #这里需要修改
contents = "\\begin{center}\n\\begin{tabular}{|"
for i in range(elements_per_line):
    contents += "c|"
contents += "}\n"
contents += r"\hline"+"\n"
col = 1
for element in elements:
    if col != 1:
        contents += " & "
    contents += dollared(element.strip())
    if col == elements_per_line:
        contents += r" \\ \hline"+"\n"
    col += 1
    if col > elements_per_line:
        col = 1
contents += "\\end{tabular}" + "\n" + "\\end{center}"

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

setCopy(contents)