Sure! Below is a Python program that provides a graphical user interface (GUI) allowing you to select multiple text files to concatenate, with the ability to assign a header to each file. The program uses Tkinter, Python's standard GUI library.
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
# List to store filename and header entry widget tuples
file_entries = []
def add_files():
# Open file dialog to select multiple text files
filenames = filedialog.askopenfilenames(
title='Select Text Files',
filetypes=[('Text files', '*.txt'), ('All files', '*.*')]
)
for filename in filenames:
# Create a frame for each file to hold its label and header entry
file_frame = ttk.Frame(files_frame)
file_frame.pack(fill='x', pady=2)
# Label to display the filename
filename_label = ttk.Label(file_frame, text=filename)
filename_label.pack(side='left', padx=5)
# Entry widget for the header
header_entry = ttk.Entry(file_frame)
header_entry.pack(side='right', fill='x', expand=True, padx=5)
# Store the filename and its associated header entry widget
file_entries.append((filename, header_entry))
def concatenate_files():
if not file_entries:
messagebox.showwarning("Warning", "No files selected.")
return
content_list = []
for filename, header_entry in file_entries:
# Retrieve the header text from the entry widget
header = header_entry.get()
# Read the content of the file
try:
with open(filename, 'r') as f:
file_content = f.read()
except Exception as e:
messagebox.showerror("Error", f"Could not read file {filename}:\n{e}")
return
# Append header and file content to the list
content_list.append(header + '\n' + file_content + '\n')
# Ask the user where to save the concatenated file
output_filename = filedialog.asksaveasfilename(
title='Save Concatenated File As',
defaultextension='.txt',
filetypes=[('Text files', '*.txt'), ('All files', '*.*')]
)
if output_filename:
try:
# Write the concatenated content to the output file
with open(output_filename, 'w') as outfile:
outfile.writelines(content_list)
messagebox.showinfo("Success", f"Files concatenated successfully into:\n{output_filename}")
except Exception as e:
messagebox.showerror("Error", f"Could not write to file {output_filename}:\n{e}")
else:
# User canceled the save operation
messagebox.showinfo("Info", "Concatenation canceled.")
# Create the main application window
root = tk.Tk()
root.title("Text File Concatenator")
# Button to add files
add_files_button = ttk.Button(root, text="Add Files", command=add_files)
add_files_button.pack(pady=5)
# Frame to hold the list of files and headers
files_frame = ttk.Frame(root)
files_frame.pack(fill='both', expand=True)
# Button to concatenate files
concatenate_button = ttk.Button(root, text='Concatenate', command=concatenate_files)
concatenate_button.pack(pady=5)
# Start the GUI event loop
root.mainloop()
How to Use the Program:
-
Add Files:
- Click the "Add Files" button.
- In the file dialog that appears, select the text files you want to concatenate. You can select multiple files at once.
- For each file selected, a row will appear in the GUI displaying the filename and an entry field where you can input the header for that file.
-
Assign Headers:
- In the text entry next to each filename, type the header you want to precede that file's content in the concatenated output.
-
Concatenate Files:
- After adding all desired files and headers, click the "Concatenate" button.
- A save dialog will appear. Choose the location and name for the concatenated output file.
- The program will create the output file, concatenating the contents of the selected files, each preceded by its respective header.
Notes:
- The program uses the
tkinter library for the GUI, which is included with most Python installations.
- The headers and file contents are concatenated with a newline character (
'\n') separating headers and contents for readability.
- If you need to remove a file or change the order, you'll need to modify the code to add that functionality or restart the program and select the files again.
- This program assumes all selected files are readable text files and does not handle binary files.
Example:
If you select file1.txt, file2.txt, and file3.txt, and assign headers "Header 1", "Header 2", and "Header 3" respectively, the output file will look like:
Header 1
<contents of file1.txt>
Header 2
<contents of file2.txt>
Header 3
<contents of file3.txt>
Feel free to customize and expand upon this code to fit your specific needs!