outputfilepath = "临时文件/tabular.txt"

from database_tools import *

data = getCopy().strip().split("\n")

validflag = False
if "col" in data[0]:
    cols = int("".join(re.findall(r"\d",data[0])))
    data = data[1:]
    validflag = True
elif "col" in data[-1]:
    cols = int("".join(re.findall(r"\d",data[-1])))
    data = data[:-1]
    validflag = True
else:
    print("未设置列数(在首行或末行, 格式: '数字 cols'), 请重试") #分离列数cols与数据data

rows = [data[i:i+cols] for i in range(0,len(data),cols)]
for t in range(cols-len(rows[-1])):
    rows[-1].append("")

output = "\\begin{center}\n\\begin{tabular}{%s}\n\\hline\n"%("|c"*cols +"|")
output += "\\\\ \\hline\n".join([" & ".join(row) for row in rows])
output += "\\\\ \\hline\n"
output += "\\end{tabular}\n\\end{center}"

setCopy(output)
SaveTextFile(output,outputfilepath)

