This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具/剪贴板表格整理.py

63 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)