[Python] 纯文本查看 复制代码
from pyperclip import copy, paste
import PySimpleGUI as sg
def data_source_dic(shujuyuan, fenge='\t'):
data_source = shujuyuan.strip().split('\n')
if fenge not in data_source[0]:
fenge = ' '
data_dic = {}
try:
for i in data_source:
if i := i.strip():
k, v = i.split(fenge)
if k not in data_dic:
data_dic[k.strip()] = v.strip()
return data_dic
except:
return
def pipei(shujuyuan, daipipei):
try:
pipei_list = daipipei.split('\n')
return [shujuyuan.get(i.strip(), '') for i in pipei_list]
except:
return
def gui():
sg.theme('GrayGrayGray') # 设置当前主题
layout = [[
sg.Frame(
'数据源',
layout=[
[
sg.Multiline('', key='数据源', size=(26, 18)),
],
[
sg.Button(' 粘 贴 ', key='粘数据源', pad=(8)),
],
],
element_justification='center',
pad=(5),
),
sg.Frame(
'待匹配',
layout=[
[
sg.Multiline('', key='待匹配', size=(18, 18)),
],
[
sg.Button(' 粘 贴 ', key='粘待匹配', pad=(8)),
],
],
element_justification='center',
pad=(5),
),
sg.Frame(
'结果',
layout=[
[
sg.Multiline('',
key='结果',
size=(18, 18),
background_color='#fffff7'),
],
[
sg.Button(' 匹 配 ', key='执行匹配', pad=(8)),
sg.Button(' 复 制 ',
key='复制结果',
pad=(8),
button_color='#ffa631'),
],
],
element_justification='center',
pad=(5),
),
], [sg.Output(key='执行结果', size=(645, 6))]]
# 创建展示窗口8
window = sg.Window('匹配字符',
layout,
font=('微软雅黑', 10),
size=(650, 574),
element_justification='center').finalize()
window['执行结果'].TKOut.output.config(wrap='word')
# 事件循环并获取输入值
while True:
event, values = window.read()
if not event:
break
jtb = '' if paste() else ',但是剪贴板为空!'
if event == '粘数据源':
window['数据源'].update(paste())
print(f'粘贴数据源{jtb}')
if event == '粘待匹配':
window['待匹配'].update(paste())
print(f'粘贴待匹配的数据{jtb}')
if event == '执行匹配':
print('执行匹配')
if values['数据源'] and values['待匹配']:
shujuyuan = data_source_dic(values['数据源'])
pipei_list = pipei(shujuyuan, values['待匹配'])
try:
if isinstance(pipei_list, list) and pipei_list:
window['结果'].update('\n'.join(pipei_list))
except:
pass
elif event == '复制结果':
res = values['结果']
if res.strip():
len_res = len(res.split('\n'))
copy(res)
print(f'✔ 已复制 {len_res} 条数据')
else:
print('结果为空,没复制任何内容')
window.close()
if __name__ == '__main__':
gui()