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/文本处理工具/剪贴板表格整理.ipynb

117 lines
2.9 KiB
Plaintext
Raw 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.

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os,re\n",
"import win32clipboard as wc\n",
"import win32con\n",
"\n",
"# 获取剪切板内容\n",
"def getCopy():\n",
" wc.OpenClipboard()\n",
" t = wc.GetClipboardData(win32con.CF_UNICODETEXT)\n",
" wc.CloseClipboard()\n",
" return t\n",
"\n",
"# 写入剪切板内容\n",
"def setCopy(str):\n",
" wc.OpenClipboard()\n",
" wc.EmptyClipboard()\n",
" wc.SetClipboardData(win32con.CF_UNICODETEXT, str)\n",
" wc.CloseClipboard()\n",
"\n",
"def dollared(string):\n",
" flag = True\n",
" for c in string:\n",
" if not c in \"1234567890.+-:[]()\":\n",
" flag = False\n",
" break\n",
" if flag:\n",
" string = \"$\" + string + \"$\"\n",
" return string\n",
"\n",
"\n",
"\n",
"data = getCopy()\n",
"\n",
"data1 = \"\"\n",
"for c in data:\n",
" if 65296 <= ord(c) < 65306:\n",
" data1 += str(ord(c)-65296)\n",
" else:\n",
" data1 += c\n",
"data = data1\n",
"data = data.replace(\"\",\".\").replace(\"\",\":\")\n",
"elements = data.split(\"\\n\")\n",
"elements_per_line = int(elements.pop(-1)) #这里需要修改\n",
"contents = \"\\\\begin{center}\\n\\\\begin{tabular}{|\"\n",
"for i in range(elements_per_line):\n",
" contents += \"c|\"\n",
"contents += \"}\\n\"\n",
"contents += r\"\\hline\"+\"\\n\"\n",
"col = 1\n",
"for element in elements:\n",
" if col != 1:\n",
" contents += \" & \"\n",
" contents += dollared(element)\n",
" if col == elements_per_line:\n",
" contents += r\" \\\\ \\hline\"+\"\\n\"\n",
" col += 1\n",
" if col > elements_per_line:\n",
" col = 1\n",
"contents += \"\\\\end{tabular}\" + \"\\n\" + \"\\\\end{center}\"\n",
"\n",
"with open(\"临时文件/tablefile.txt\",\"w\",encoding = \"utf8\") as f:\n",
" f.write(contents)\n",
"\n",
"setCopy(contents)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pythontest",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "91219a98e0e9be72efb992f647fe78b593124968b75db0b865552d6787c8db93"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}