[Python] 纯文本查看 复制代码
import random
import tkinter
from tkinter import ttk, messagebox # 有Combobox、LabelFrame 组件时需要本语句
import os
import glob
import re
import pyperclip
import win32clipboard as wc
import time
import threading
def getCopyTxet():
try:
wc.OpenClipboard()
copytxet = wc.GetClipboardData()
except:
copytxet = ''
wc.CloseClipboard()
return str(copytxet)
text = ['']
gdata=[]
MyPath = r'.\SVG'
os.chdir(MyPath)
ctrllist1 = sorted(glob.glob('*.svg'))
print(len(ctrllist1),ctrllist1)
ctrllist1.append('~~粘贴板~~~')
ctrllist1.insert(0,'~~粘贴板~~~')
endtt= len(ctrllist1)
if len(ctrllist1) < 1:
ctrllist1 = '没有文件~~'
for i in range(1,len(ctrllist1)-1):
tt =[]
with open(ctrllist1[i], "r", encoding="utf-8") as f:
tt= f.read()
text.append(tt)
# print(text)
def open():
global it,str_tp1,str_tp2,str_tp3,str_tp4,str_tp5,html,res1,endtt,isn
res1 = []
tt =ctrl_file.get()
for i in range(len(ctrllist1)-1):
if tt == ctrllist1[i]:
isn = i
break
else:
isn =False
print('isn',isn,tt)
if isn==False:
getd=getCopyTxet()
# print('g',getd)
sdata1 = re.search('<svg', getd)
# print(sdata1)
if sdata1!= None:
html = getd
else:
messagebox.showinfo('提示', '粘贴板里没有发现是SVG模板~~')
return
isn=True
else:
html = text[isn]
#读取图片1
com1 = re.compile('".*?"',re.S)
res1 = []
print('tup',res1)
html = re.sub('url\(".*?"\)', 'url("http:55667")', html)
res1 = com1.findall(html)
for i in range(len(res1)):
res1[i] = res1[i].strip('"')
print(res1[i])
#读取图片2
com2 = re.compile('\(h.*?\)',re.S)
res2 = []
res2 = com2.findall(html)
for i in range(0,len(res2)):
res2[i] = res2[i].strip('(').strip(')')
# print(len(res2),res2)
res1 = res1+res2
print(len(res1),res1)
it = len(res1)
ctrl_LabelFileName = tkinter.Label(root, text='图片1', fg='#FF0000', font=('宋体', '10', 'bold', 'underline'))
ctrl_LabelFileName.place(x=22, y=58, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=58
str_tp1 = tkinter.StringVar() # 绑定变量
ctrl_tp1 = tkinter.Entry(root, textvariable=str_tp1, font=('宋体', '9'))
ctrl_tp1.place(x=101, y=50, width=295, height=22)
ctrl_LabelFileName = tkinter.Label(root, text='图片2', fg='#FF0000', font=('宋体', '10', 'bold', 'underline'))
ctrl_LabelFileName.place(x=22, y=85, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=58
str_tp2 = tkinter.StringVar() # 绑定变量
ctrl_tp2 = tkinter.Entry(root, textvariable=str_tp2, font=('宋体', '9'))
ctrl_tp2.place(x=101, y=85, width=295, height=22)
ctrl_LabelFileName = tkinter.Label(root, text='图片3', fg='#FF0000', font=('宋体', '10', 'bold', 'underline'))
ctrl_LabelFileName.place(x=22, y=125, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=58
str_tp3 = tkinter.StringVar() # 绑定变量
ctrl_tp3 = tkinter.Entry(root, textvariable=str_tp3, font=('宋体', '9'))
ctrl_tp3.place(x=101, y=125, width=295, height=22)
ctrl_LabelFileName = tkinter.Label(root, text='图片4', fg='#FF0000', font=('宋体', '10', 'bold', 'underline'))
ctrl_LabelFileName.place(x=22, y=165, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=58
str_tp4 = tkinter.StringVar() # 绑定变量
ctrl_tp4 = tkinter.Entry(root, textvariable=str_tp4, font=('宋体', '9'))
ctrl_tp4.place(x=101, y=165, width=295, height=22)
ctrl_LabelFileName = tkinter.Label(root, text='图片5', fg='#FF0000', font=('宋体', '10', 'bold', 'underline'))
ctrl_LabelFileName.place(x=22, y=205, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=58
str_tp5 = tkinter.StringVar() # 绑定变量
ctrl_tp5 = tkinter.Entry(root, textvariable=str_tp5, font=('宋体', '9'))
ctrl_tp5.place(x=101, y=205, width=295, height=22)
pyperclip.copy('')
print('开始')
t= threading.Thread(target=dd)
t.start()
tt = threading.Thread(target=ok)
tt.start()
def ok():
global it,html,res1,gdata,isn
while True:
if len(gdata)==0:
time.sleep(.2)
else:
if len(res1)<=len(gdata):
for i in range(len(gdata)):
try:
html = re.sub(res1[i], gdata[i], html, count=1)
print(i, res1[1], gdata[i])
except:
pass
pyperclip.copy(html)
messagebox.showinfo('提示', '以复制到你的粘贴板,直接粘贴即可~')
break
# else:
# messagebox.showinfo('错误', '您没有复制到指定张数~~~~~')
# return
# print(html)
def dd():
global gdata
ss=len(res1)
gos= 0
last_data = None
gdata=[]
while True:
data = getCopyTxet()
if gos >= ss:
break
if data != last_data:
ifs = 0
last_data = data
sdata1 = re.search('http', data)
if sdata1 != None:
gdata.append(data)
gos=gos+1
if gos==1:
str_tp1.set(data)
if gos==2:
str_tp2.set(data)
if gos == 3:
str_tp3.set(data)
if gos == 4:
str_tp4.set(data)
if gos == 5:
str_tp5.set(data)
time.sleep(0.3)
root = tkinter.Tk() # 设定窗体变量
root.geometry('451x253') # 格式('宽x高+x+y')其中x、y为位置
root.title('SVG助手')
root.wm_attributes('-topmost',1)
str_file = tkinter.StringVar() # 绑定变量
ctrl_file = ttk.Combobox(root, textvariable=str_file, font=('宋体', '9'))
ctrl_file.place(x=101, y=14, width=250, height=18)
ctrl_file['value'] = (ctrllist1) # 给下拉菜单设定预设值
ctrl_file.current(0) # 设定第一个值为默认值
ctrl_LabelBlue = tkinter.Label(root, text='选择SVG文件', fg='#0000C0', font=('黑体', '9', 'bold'))
ctrl_LabelBlue.place(x=7, y=14, height=14) # 考虑到对齐问题,不列入宽度,需要时手动加入 width=79
ctrl_CommandButton4 = tkinter.Button(root, text='打开', font=('宋体', '9'),
command=open) # 可在括号内加上调用函数部分 ,command=ctrl_CommandButton4_clicked
ctrl_CommandButton4.place(x=360, y=7, width=58, height=29)
root.mainloop()