[Python] 纯文本查看 复制代码
# -*- coding:utf-8 -*-
import wx
import os
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='TXT文本处理软件', size=(467, 426),name='frame',style=541072384)
self.qdck = wx.Panel(self)
self.SetTransparent(230)
self.qdck.SetOwnBackgroundColour((119, 202, 160, 255))
self.Centre()
self.bjk1 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 40),value='',name='text',style=0)
self.bjk2 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 40),value='',name='text',style=0)
self.bq1 = wx.StaticText(self.qdck,size=(180, 20),pos=(30, 10),label='被替换内容',name='staticText',style=2321)
self.bq2 = wx.StaticText(self.qdck,size=(180, 20),pos=(230, 10),label='替换内容',name='staticText',style=2321)
bq1_font = wx.Font(12, 74, 90, 400, False, 'Microsoft YaHei UI', 28)
self.bq1.SetFont(bq1_font)
self.bq2.SetFont(bq1_font)
self.bjk3 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 90),value='',name='text',style=0)
self.bjk4 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 90),value='',name='text',style=0)
self.bjk5 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 140),value='',name='text',style=0)
self.bjk6 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 140),value='',name='text',style=0)
self.bjk7 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(30, 190),value='',name='text',style=0)
self.bjk8 = wx.TextCtrl(self.qdck,size=(180, 40),pos=(230, 190),value='',name='text',style=0)
self.an1 = wx.Button(self.qdck,size=(377, 47),pos=(30, 280),label='执行替换',name='button')
self.an1.Bind(wx.EVT_BUTTON,self.an1_anbdj)
self.dxk1 = wx.RadioButton(self.qdck,size=(180, 39),pos=(30, 240),name='radioButton',label='修改文件名和TXT内容')
self.dxk2 = wx.RadioButton(self.qdck,size=(180, 39),pos=(230, 240),name='radioButton',label='仅修改TXT内容')
self.dxk2.SetValue(True)
def an1_anbdj(self,event):
dir_path = wx.DirSelector(message='选择需要处理的文件目录', style=0, pos=(57, 322), parent=self.qdck)
file_list = list(os.scandir(dir_path))
print(file_list)
text01 = self.bjk1.GetValue()
text02 = self.bjk3.GetValue()
text03 = self.bjk5.GetValue()
text04 = self.bjk7.GetValue()
retext01 = self.bjk2.GetValue()
retext02 = self.bjk4.GetValue()
retext03 = self.bjk6.GetValue()
retext04 = self.bjk8.GetValue()
text_list = [text01, text02, text03, text04]
retext_list = [retext01, retext02, retext03, retext04]
dict01 = dict(zip(text_list,retext_list))
for file in list(file_list):
if '.txt' in os.path.basename(file):
file_path = os.path.abspath(file)
file_name_basic = os.path.basename(file)
file_name = file_name_basic
print(file_name)
if self.dxk1.GetValue() == True:
print('-----------修改文件名和TXT内容')
for x,y in dict01.items():
print(x, y)
try:
file_name = file_name.replace(x, y)
print(file_name)
except:
print(x,y,'修改名称失败')
print(file_name)
new_file_path = file_path.replace(file_name_basic, f'[已替换]{file_name}')
print(new_file_path)
with open(file, 'r', encoding='utf-8') as f, open(new_file_path, 'w', encoding='utf-8') as w:
text = f.read()
for x, y in dict01.items():
print(x, y)
text = text.replace(x, y)
w.write(text)
else:
print('-----------修改TXT内容')
new_file_path = file_path.replace(file_name, f'[已替换]{file_name}')
with open(file, 'r', encoding='utf-8') as f, open(new_file_path, 'w', encoding='utf-8') as w:
text = f.read()
for x, y in dict01.items():
text = text.replace(x, y)
else:
continue
os.startfile(dir_path)
print('an1,按钮被单击')
class myApp(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
return True
if __name__ == '__main__':
app = myApp()
app.MainLoop()