VelocityFileConverter Python GUI
👤 Sharing: AI
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
import os
import json
import csv
import xml.etree.ElementTree as ET
class VelocityFileConverterApp:
def __init__(self, master):
self.master = master
master.title("Velocity File Converter")
# --- UI Elements ---
self.input_file_label = ttk.Label(master, text="Input File:")
self.input_file_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
self.input_file_entry = ttk.Entry(master, width=50)
self.input_file_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we")
self.browse_button = ttk.Button(master, text="Browse", command=self.browse_file)
self.browse_button.grid(row=0, column=2, padx=5, pady=5, sticky="e")
self.output_file_label = ttk.Label(master, text="Output File:")
self.output_file_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
self.output_file_entry = ttk.Entry(master, width=50)
self.output_file_entry.grid(row=1, column=1, padx=5, pady=5, sticky="we")
self.output_browse_button = ttk.Button(master, text="Browse", command=self.browse_output_file)
self.output_browse_button.grid(row=1, column=2, padx=5, pady=5, sticky="e")
self.input_type_label = ttk.Label(master, text="Input File Type:")
self.input_type_label.grid(row=2, column=0, padx=5, pady=5, sticky="w")
self.input_type_combo = ttk.Combobox(master, values=["JSON", "CSV", "XML"], state="readonly")
self.input_type_combo.grid(row=2, column=1, padx=5, pady=5, sticky="we")
self.input_type_combo.set("JSON") # Default value
self.output_type_label = ttk.Label(master, text="Output File Type:")
self.output_type_label.grid(row=3, column=0, padx=5, pady=5, sticky="w")
self.output_type_combo = ttk.Combobox(master, values=["JSON", "CSV", "XML"], state="readonly")
self.output_type_combo.grid(row=3, column=1, padx=5, pady=5, sticky="we")
self.output_type_combo.set("CSV") # Default value
self.convert_button = ttk.Button(master, text="Convert", command=self.convert_file)
self.convert_button.grid(row=4, column=1, padx=5, pady=10, sticky="e")
self.detail_button = ttk.Button(master, text="Details", command=self.show_details)
self.detail_button.grid(row=5, column=1, padx=5, pady=10, sticky="w")
# --- Layout Configuration ---
master.columnconfigure(1, weight=1) # Make column 1 expand to fill space
master.rowconfigure(6, weight=1) # Add an empty row to push content to the top
def browse_file(self):
filename = filedialog.askopenfilename()
self.input_file_entry.delete(0, tk.END)
self.input_file_entry.insert(0, filename)
def browse_output_file(self):
filename = filedialog.asksaveasfilename(defaultextension=".txt")
self.output_file_entry.delete(0, tk.END)
self.output_file_entry.insert(0, filename)
def convert_file(self):
input_file = self.input_file_entry.get()
output_file = self.output_file_entry.get()
input_type = self.input_type_combo.get().lower()
output_type = self.output_type_combo.get().lower()
if not input_file or not output_file:
messagebox.showerror("Error", "Please select both input and output files.")
return
try:
if input_type == "json":
with open(input_file, 'r') as f:
data = json.load(f)
elif input_type == "csv":
data = []
with open(input_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
data.append(row)
elif input_type == "xml":
tree = ET.parse(input_file)
root = tree.getroot()
data = self.xml_to_dict(root)
else:
raise ValueError("Unsupported input type")
if output_type == "json":
with open(output_file, 'w') as f:
json.dump(data, f, indent=4)
elif output_type == "csv":
if isinstance(data, list) and data:
keys = data[0].keys()
with open(output_file, 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=keys)
writer.writeheader()
writer.writerows(data)
else:
messagebox.showerror("Error", "Cannot convert to CSV. Input data is not a list of dictionaries.")
return
elif output_type == "xml":
root = self.dict_to_xml(data)
tree = ET.ElementTree(root)
tree.write(output_file, encoding="utf-8", xml_declaration=True)
else:
raise ValueError("Unsupported output type")
messagebox.showinfo("Success", "File converted successfully!")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
def xml_to_dict(self, element):
d = {}
for child in element:
if len(child):
d[child.tag] = self.xml_to_dict(child)
else:
d[child.tag] = child.text
return d
def dict_to_xml(self, data, root_name='root'):
root = ET.Element(root_name)
if isinstance(data, dict):
for key, value in data.items():
element = ET.SubElement(root, key)
if isinstance(value, dict):
self.dict_to_xml(value, element)
else:
element.text = str(value)
elif isinstance(data, list):
for item in data:
if isinstance(item, dict):
sub_element = ET.SubElement(root, 'item')
self.dict_to_xml(item, sub_element)
else:
element = ET.SubElement(root, 'item')
element.text = str(item)
else:
root.text = str(data)
return root
def show_details(self):
details_window = tk.Toplevel(self.master)
details_window.title("Program Details")
details_text = tk.Text(details_window, wrap=tk.WORD, height=20, width=60)
details_text.pack(padx=10, pady=10)
details_text.insert(tk.END, """
This program, 'Velocity File Converter', is a versatile tool designed to convert files between different formats such as JSON, CSV, and XML. It provides a user-friendly graphical interface built with Tkinter, allowing users to easily select input and output files and specify the desired conversion types. Key features include:
* **File Selection:** Browse buttons for selecting input and output files.
* **Format Selection:** Dropdown menus to specify the input and output file formats (JSON, CSV, XML).
* **Conversion Engine:** Robust conversion logic to handle various data structures and potential errors.
* **Error Handling:** Clear error messages to guide users in case of issues.
The program leverages standard Python libraries such as `json`, `csv`, and `xml.etree.ElementTree` to ensure efficient and reliable file processing. It supports:
* **JSON:** Reading and writing JSON files with proper indentation.
* **CSV:** Reading and writing CSV files with headers automatically inferred from the data.
* **XML:** Converting between XML and other formats using a recursive function to handle nested structures.
The conversion process includes error handling to manage invalid file selections, incorrect formats, and data conversion problems. The output file is written with appropriate formatting (indentation for JSON, headers for CSV, and XML declaration for XML).
The overall design focuses on providing a seamless experience for users who need to quickly convert files between commonly used formats.
""")
details_text.config(state=tk.DISABLED) # Make the text read-only
root = tk.Tk()
app = VelocityFileConverterApp(root)
root.mainloop()
👁️ Viewed: 5
Comments