目录[-]

制作一个小工具,功能为对txt文件进行切片分割,生成多个txt文本,切片方式分为按每个txt文件包含段数进行切割或者根据最终输出个数来进行切割。

Slice1

代码实现如下:

#作者:冯桂和
#功能:对txt文件进行切片分割,按每个txt文件包含段数切割或者根据最终输出个数切割

from tkinter import *
from tkinter import messagebox
import tkinter as tk
from tkinter import filedialog
import os

answer=""

#创建主窗口
win=tk.Tk()
#设置窗口标题
win.title("文本切片工具-冯桂和")
#设置窗口大小
win.geometry('600x200+800+500')

def fenge():
    global answer
    f=open(answer,'r',encoding='utf-8')
    txt=f.readlines()
    f.close()
    try:
        num=int(entry1.get())
        if len(txt)%num!=0:
            newtxtnum=int(len(txt)/num)+1
            print(newtxtnum)
        else:
            newtxtnum=int(len(txt)/num)
            print(newtxtnum)
        for i in range(newtxtnum):
            a=list(answer)
            a.insert(-4,'-{}'.format(str(i+1)))
            # print(a)
            a=''.join(a)
            ff=open(a,'w+',encoding='utf-8')
            j=i*num
            k=(i+1)*num
            txtout=''.join(str(e) for e in txt[j:k])
            ff.write(txtout)
            ff.close()
        text1.insert(END,'程序运行成功,已在同级目录下生成{}个分割文件!\n'.format(newtxtnum))
        text1.see("end")
    except ValueError:
        messagebox.showinfo(title='运行提示', message='输入错误')
def fenge2():
    global answer
    f=open(answer,'r',encoding='utf-8')
    txt=f.readlines()
    f.close()
    try:
        num=int(entry2.get())
        if len(txt)%num!=0:
            hangshu=int(len(txt)/num)+1
            geshu=int(len(txt)/hangshu)+1
        else:
            hangshu=int(len(txt)/num)
            geshu=int(len(txt)/hangshu)
        for i in range(geshu):
            a=list(answer)
            a.insert(-4,'-{}'.format(str(i+1)))
            # print(a)
            a=''.join(a)
            ff=open(a,'w+',encoding='utf-8')
            j=i*hangshu
            k=(i+1)*hangshu
            txtout=''.join(str(e) for e in txt[j:k])
            ff.write(txtout)
            ff.close()
        text1.insert(END,'程序运行成功,已在同级目录下生成{}个分割文件!\n'.format(geshu))
        text1.see("end")
    except ValueError:
        messagebox.showinfo(title='运行提示', message='输入错误')
def opentxt():
    global answer
    # 设置文件对话框会显示的文件类型
    my_filetypes = [('all files', '.*'), ('text files', '.txt')]
    answer = filedialog.askopenfilename(parent=win,
                                        initialdir=os.getcwd(),
                                        title="Please select a file:",
                                        filetypes=my_filetypes)
    fff=open(answer,'r',encoding='utf-8')
    txt=fff.readlines()
    fff.close()
    text1.insert(END,'文件打开成功,共计{}段!\n'.format(len(txt)))
    text1.see("end")
buttono= tk.Button(win,text='打开文件',width=20, height=1,command=opentxt)
buttono.grid(row=0,column=0,padx=5,pady=5)
entry1 = tk.Entry(win)
entry1.grid(row=1,column=0,padx=5,pady=5)
button1 = tk.Button(win,text='按每个文本段数分割',width=20, height=1,command=fenge)
button1.grid(row=2,column=0,padx=5,pady=5)
entry2 = tk.Entry(win)
entry2.grid(row=3,column=0,padx=5,pady=5)
button2 = tk.Button(win,text='按输出文本个数分割',width=20, height=1,command=fenge2)
button2.grid(row=4,column=0,padx=5,pady=5)
text1=tk.Text(win,width=40,height=10,font=('微软雅黑',12))
text1.place(x=170,y=5,width=420,height=180)

#显示主窗口
win.mainloop()