import
os
import
re
import
time
from
tkinter
import
ttk, filedialog, messagebox, INSERT, Tk, Button, Text, Scrollbar, \
HORIZONTAL, VERTICAL, IntVar, Checkbutton, Label, StringVar, Entry
import
windnd
ui_pos
=
{
"title"
:
"TxT文件处理助手V0.2-0802 By 52poje Loker"
,
"geometry"
:
"450x300"
,
}
FilePaths
=
()
def
clearAll():
ctrl_FileListBox.delete(
1.0
,
"end"
)
str_KeyWord.
set
("")
str_KeyNum.
set
("")
def
getTxtFiles():
global
FilePaths
files
=
filedialog.askopenfilenames(filetypes
=
[(
'text files'
,
'.txt'
)])
if
files:
FilePaths
=
files
for
f_name
in
files:
ctrl_FileListBox.insert(
'end'
, f_name)
ctrl_FileListBox.insert(INSERT,
'\n'
)
else
:
messagebox.showinfo(title
=
'提示'
, message
=
'没有选择任何文件!'
)
def
KeyWordScan(keys, s):
key_words
=
keys.split(
" "
)
t_f
=
False
for
key_word
in
key_words:
if
key_word
in
s:
t_f
=
True
return
t_f
def
ctrl_StartBtn_clicked():
has_key_words
=
int_CheckBox1.get()
key_words
=
str_KeyWord.get()
has_empty_line
=
int_CheckBox2.get()
has_N
=
int_CheckBox3.get()
n
=
str_KeyNum.get()
has_zz
=
int_CheckBox4.get()
zz
=
str_zz.get()
try
:
for
file
in
FilePaths:
s_file
=
open
(os.path.splitext(
file
)[
0
]
+
"_new"
+
os.path.splitext(
file
)[
1
],
'w+'
)
f_lines
=
open
(
file
, encoding
=
'utf8'
).readlines()
for
s
in
f_lines:
if
has_key_words:
if
KeyWordScan(key_words, s):
continue
if
has_empty_line:
if
len
(s.strip())
=
=
0
:
continue
if
has_N:
if
len
(s.strip()) <
int
(n):
continue
if
has_zz:
if
re.match(zz, s.strip()):
continue
s_file.write(s)
s_file.close()
except
Exception as e:
with
open
(
"log"
,
"a+"
) as f:
f.write(time.strftime(
"%Y-%m-%d, %H:%M:%S"
, time.localtime())
+
"\n"
)
f.write(
repr
(e)
+
"\n"
)
def
draggedFiles(files):
msg
=
'\n'
.join((item.decode(
'gbk'
)
for
item
in
files))
for
f_name
in
files:
ctrl_FileListBox.insert(
'end'
, f_name)
ctrl_FileListBox.insert(INSERT,
'\n'
)
print
(msg)
root
=
Tk()
root.geometry(ui_pos[
"geometry"
])
root.title(ui_pos[
"title"
])
windnd.hook_dropfiles(root, func
=
draggedFiles)
ctrl_Frame1
=
ttk.LabelFrame(root, text
=
'选项'
)
ctrl_Frame1.place(x
=
14
, y
=
72
, width
=
388
, height
=
140
)
ctrl_StartBtn
=
Button(root, text
=
'执行'
, font
=
(
'宋体'
,
'9'
),
command
=
ctrl_StartBtn_clicked)
ctrl_StartBtn.place(x
=
22
, y
=
250
, width
=
72
, height
=
29
)
ctrl_QuitBtn
=
Button(root, text
=
'清除'
, font
=
(
'宋体'
,
'9'
), command
=
clearAll)
ctrl_QuitBtn.place(x
=
108
, y
=
250
, width
=
72
, height
=
29
)
ctrl_FileListBox
=
Text(root, font
=
(
'宋体'
,
'9'
))
ctrl_FileListBox.place(x
=
14
, y
=
7
, width
=
260
, height
=
38
)
ctrl_Scrollbar1
=
Scrollbar(root, command
=
ctrl_FileListBox.xview, orient
=
HORIZONTAL)
ctrl_Scrollbar1.place(x
=
14
, y
=
46
, width
=
261
, height
=
16
)
ctrl_Scrollbar2
=
Scrollbar(root, command
=
ctrl_FileListBox.yview, orient
=
VERTICAL)
ctrl_Scrollbar2.place(x
=
275
, y
=
7
, width
=
16
, height
=
39
)
ctrl_FileListBox.config(xscrollcommand
=
ctrl_Scrollbar1.
set
, yscrollcommand
=
ctrl_Scrollbar2.
set
, wrap
=
'none'
)
int_CheckBox1
=
IntVar()
ctrl_CheckBox1
=
Checkbutton(ctrl_Frame1, text
=
'删除行含关键字或词的行'
, variable
=
int_CheckBox1, font
=
(
'宋体'
,
'9'
))
ctrl_CheckBox1.place(x
=
14
, y
=
14
, height
=
22
)
ctrl_CheckBox1.deselect()
Ctrl_Label1
=
Label(ctrl_Frame1, text
=
"关键字:"
)
Ctrl_Label1.place(x
=
180
, y
=
14
, width
=
55
, height
=
22
)
str_KeyWord
=
StringVar()
ctrl_KeyWord
=
Entry(ctrl_Frame1, textvariable
=
str_KeyWord, font
=
(
'宋体'
,
'9'
))
ctrl_KeyWord.place(x
=
230
, y
=
14
, width
=
150
, height
=
22
)
int_CheckBox2
=
IntVar()
ctrl_CheckBox2
=
Checkbutton(ctrl_Frame1, text
=
'删除空行'
, variable
=
int_CheckBox2, font
=
(
'宋体'
,
'9'
))
ctrl_CheckBox2.place(x
=
14
, y
=
36
, height
=
22
)
ctrl_CheckBox2.deselect()
int_CheckBox3
=
IntVar()
ctrl_CheckBox3
=
Checkbutton(ctrl_Frame1, text
=
'删除字符小于N的行'
, variable
=
int_CheckBox3, font
=
(
'宋体'
,
'9'
))
ctrl_CheckBox3.place(x
=
14
, y
=
58
, height
=
22
)
ctrl_CheckBox3.deselect()
Ctrl_Label
=
Label(ctrl_Frame1, text
=
"N ="
)
Ctrl_Label.place(x
=
180
, y
=
58
, width
=
55
, height
=
22
)
str_KeyNum
=
StringVar()
ctrl_KeyNum
=
Entry(ctrl_Frame1, textvariable
=
str_KeyNum, font
=
(
'宋体'
,
'9'
))
ctrl_KeyNum.place(x
=
230
, y
=
58
, width
=
30
, height
=
22
)
int_CheckBox4
=
IntVar()
ctrl_CheckBox4
=
Checkbutton(ctrl_Frame1, text
=
'删除符合正则的行'
, variable
=
int_CheckBox4, font
=
(
'宋体'
,
'9'
))
ctrl_CheckBox4.place(x
=
14
, y
=
80
, height
=
22
)
ctrl_CheckBox4.deselect()
Ctrl_Label2
=
Label(ctrl_Frame1, text
=
"正则:"
)
Ctrl_Label2.place(x
=
180
, y
=
80
, width
=
55
, height
=
22
)
str_zz
=
StringVar()
ctrl_zz
=
Entry(ctrl_Frame1, textvariable
=
str_zz, font
=
(
'宋体'
,
'9'
))
ctrl_zz.place(x
=
230
, y
=
80
, width
=
150
, height
=
22
)
ctrl_OpenFileBtn
=
Button(root, text
=
'选择文件'
,
font
=
(
'宋体'
,
'9'
),
command
=
getTxtFiles)
ctrl_OpenFileBtn.place(x
=
305
, y
=
18
, width
=
72
, height
=
29
)
root.mainloop()