Certainly! Below is a Python script that utilizes the tkinter library for the user interface. It allows you to select a .txt file, specify the number of lines per file, and select a folder where the split files will be saved.
import os
from tkinter import Tk, filedialog, messagebox, Label, Entry, Button
def split_text_file(file_path, lines_per_file, output_directory):
try:
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
total_lines = len(lines)
total_files = (total_lines + lines_per_file - 1) // lines_per_file # Ceiling division
base_name = os.path.basename(file_path).split('.')[0]
for i in range(total_files):
start = i * lines_per_file
end = start + lines_per_file
chunk = lines[start:end]
output_file_name = os.path.join(output_directory, f"{base_name}_Set{i+1:02d}.txt")
with open(output_file_name, 'w', encoding='utf-8') as output_file:
output_file.writelines(chunk)
messagebox.showinfo("Success", f"File has been split into {total_files} parts.")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
def select_file():
file_path = filedialog.askopenfilename(
filetypes=[("Text Files", "*.txt")],
title="Select a Text File"
)
entry_file_path.delete(0, 'end')
entry_file_path.insert(0, file_path)
def select_output_directory():
output_dir = filedialog.askdirectory(title="Select Output Directory")
entry_output_dir.delete(0, 'end')
entry_output_dir.insert(0, output_dir)
def on_split_button_click():
file_path = entry_file_path.get()
output_directory = entry_output_dir.get()
lines_per_file = entry_lines_per_file.get()
if not file_path:
messagebox.showerror("Error", "Please select a text file.")
return
if not output_directory:
messagebox.showerror("Error", "Please select an output directory.")
return
if not lines_per_file.isdigit() or int(lines_per_file) <= 0:
messagebox.showerror("Error", "Please provide a valid number of lines per file.")
return
lines_per_file = int(lines_per_file)
split_text_file(file_path, lines_per_file, output_directory)
# Create the UI
root = Tk()
root.title("Text File Splitter")
root.geometry("500x200")
root.resizable(False, False)
# Labels and Entry fields
Label(root, text="Select File:").grid(row=0, column=0, pady=10, padx=10, sticky='e')
entry_file_path = Entry(root, width=50)
entry_file_path.grid(row=0, column=1, pady=10, padx=10)
Button(root, text="Browse", command=select_file).grid(row=0, column=2, pady=10, padx=10)
Label(root, text="Lines per File:").grid(row=1, column=0, pady=10, padx=10, sticky='e')
entry_lines_per_file = Entry(root, width=50)
entry_lines_per_file.grid(row=1, column=1, pady=10, padx=10)
Label(root, text="Output Directory:").grid(row=2, column=0, pady=10, padx=10, sticky='e')
entry_output_dir = Entry(root, width=50)
entry_output_dir.grid(row=2, column=1, pady=10, padx=10)
Button(root, text="Browse", command=select_output_directory).grid(row=2, column=2, pady=10, padx=10)
# Split Button
Button(root, text="Split File", command=on_split_button_click, width=20).grid(row=3, column=1, pady=20)
# Run the Tkinter event loop
root.mainloop()
How It Works:
-
File Selection:
The select_file() function lets the user select a .txt file using a file dialog. It populates the entry field with the selected file path.
-
Output Directory:
The select_output_directory() function lets the user choose the directory where the split files will be saved.
-
Lines Per File:
Enter the desired number of lines per split file in the "Lines per File" input box.
-
Splitting the File:
When the user clicks the "Split File" button, it validates the inputs and then processes the selected text file in chunks, saving each subset of lines as a separate .txt file in the specified output directory.
-
Output Files:
The output files are named using the pattern [OriginalFileName]_SetXX.txt, where XX is the part number (e.g., Set01.txt, Set02.txt, etc.).
Make sure you have Python installed on your system. To execute this script:
- Save the code to a file (e.g.,
text_splitter.py).
- Run the script using
python text_splitter.py.
This script offers a simple and effective GUI-based solution for splitting text files.