def open_file():
'''
打开文件
:return:
'''
global file_path
global file_text
global dirname
contents = text1.get(0.0,tk.END).strip()
if contents !="":
text1.delete('1.0', tk.END)
if dirname=="":
dirname=os.getcwd()
file_path = filedialog.askopenfilename(title=u'打开文件',
filetypes=[('TXT', '*.TXT'), ('All Files', '*')],
initialdir=dirname)
print('打开文件:', file_path)
if file_path is not None:
with open(file=file_path, mode='r+', encoding='UTF-8') as file:
file_text = file.read()
text1.insert('insert', file_text)
def save_file():
global file_path
global file_text
global filename1
global dirname
contents = text1.get(0.0,tk.END).strip()
#print (len(contents))
if contents=="":
tk.messagebox.showinfo('文件不存在','请打开文件操作!')
open_file()
deal_file()
filename2=os.path.basename(filename1)
file_path1 = filedialog.asksaveasfilename(title=u'保存文件',
initialfile=filename2,
filetypes=[('TXT', '*.TXT'), ('All Files', '*')],
initialdir=dirname)
print('保存文件:', file_path1)
file_text = text1.get('1.0', tk.END)
if file_path1 is not None:
with open(file=file_path1, mode='w', encoding='UTF-8') as file:
file.write(file_text)
text1.delete('1.0', tk.END)
tk.messagebox.showinfo('文件存储完毕',file_path1+'保存完成')
print('保存完成')
def deal_file():
global file_path
global file_text
global dirname
global filename1
if file_path=="":
open_file()
dirname,filename=os.path.split(file_path)
print ('处理文件名',filename)
line1=" "
with open(file_path,'r',encoding='UTF-8') as file_object:
lines = file_object.readlines()
for line in lines: #lines[0:10]:
line=line.strip()
#print(line.strip())
if len(line)==0:
pass
else:
if line[-1:]=="。" or line[-1:]=="”" :
line=line+"\n "
#print (line[-4:-1],"***",line[-4:-1],"***")
if line[0]=="”":
if line1[-4:-1]==" "[-4:-1]:
line1=line1[:-5]+"”\n "
import pyperclip # 粘贴板用
lines=pyperclip.paste()
print(lines,len(lines))
line1=""
for line in lines: #lines[0:10]:
line=line.strip()
#print(line.strip())
# print (line,len(line1))
if line=="":
if line1[-1:]=="。" or line1[-1:]=="”" or line1[-1:]==" " or line1[-1:]==" ":
line="\n"
line1=line1+line
# print(line1)
pyperclip.copy(line1)
exit()