import tkinter as tk # Tkinter for GUI
from tkinter import filedialog, messagebox # File dialog and message boxes
import pandas as pd # Pandas for data manipulation
df = None # Global variable to store the Pandas DataFrame
def load_excel():
"""Load an Excel file and convert it to a Pandas DataFrame."""
global df
file_path = filedialog.askopenfilename(
filetypes=[("Excel files", "*.xlsx;*.xls")]
)
if file_path: # Check if a file was selected
try:
df = pd.read_excel(file_path) # Load the Excel file into DataFrame
messagebox.showinfo("Success", "Excel file loaded successfully!")
except Exception as e:
messagebox.showerror("Error", f"Failed to load Excel file: {e}")
def export_to_xml():
"""Export the DataFrame to an XML file."""
global df
if df is None: # Check if data is loaded
messagebox.showwarning("Warning", "No data to export. Load an Excel file first.")
return
file_path = filedialog.asksaveasfilename(
defaultextension=".xml",
filetypes=[("XML files", "*.xml")]
)
if file_path: # Check if save location is selected
try:
df.to_xml(file_path, index=False) # Export data to XML
messagebox.showinfo("Success", f"Data exported successfully to {file_path}")
except Exception as e:
messagebox.showerror("Error", f"Failed to export data: {e}")
my_w = tk.Tk() # Create the main application window
my_w.title("Excel to XML Exporter") # Set the title
my_w.geometry("400x200") # Set the dimensions
# Button to load an Excel file
btn_load_excel = tk.Button(
my_w,
text="Load Excel",
command=load_excel,
width=15,
bg="lightblue"
)
btn_load_excel.pack(pady=10)
# Button to export data to XML
btn_export_xml = tk.Button(
my_w,
text="Export to XML",
command=export_to_xml,
width=15,
bg="lightgreen"
)
btn_export_xml.pack(pady=10)
my_w.mainloop() # Start the GUI loop
# Importing required libraries
import tkinter as tk # Tkinter for GUI
from tkinter import filedialog, messagebox # For file dialog and message boxes
import pandas as pd # Pandas for data manipulation
# Global variable to store the DataFrame
df = None # This will hold the data loaded from the Excel file
# Function to load an Excel file
def load_excel():
"""Load an Excel file and convert it to a Pandas DataFrame."""
global df # Declare the global DataFrame variable
# Open a file dialog to select an Excel file
file_path = filedialog.askopenfilename(
filetypes=[("Excel files", "*.xlsx;*.xls")] # Filter to show only Excel files
)
# Check if a file was selected
if file_path:
try:
# Load the selected Excel file into a Pandas DataFrame
df = pd.read_excel(file_path)
# Show a success message to the user
messagebox.showinfo("Success", "Excel file loaded successfully!")
except Exception as e:
# If an error occurs during file loading, display an error message
messagebox.showerror("Error", f"Failed to load Excel file: {e}")
# Function to export the DataFrame to an XML file
def export_to_xml():
"""Export the DataFrame to an XML file."""
global df # Declare the global DataFrame variable
# Check if the DataFrame is empty
if df is None:
# Show a warning if no data is loaded
messagebox.showwarning("Warning", "No data to export. Load an Excel file first.")
return # Exit the function if no data is available
# Open a file dialog to specify the save location and file name for the XML file
file_path = filedialog.asksaveasfilename(
defaultextension=".xml", # Default file extension
filetypes=[("XML files", "*.xml")] # Filter to show only XML files
)
# Check if the user provided a save location
if file_path:
try:
# Convert the DataFrame to an XML file and save it to the specified location
df.to_xml(file_path, index=False) # Exclude the index in the XML file
# Show a success message once the file is saved
messagebox.showinfo("Success", f"Data exported successfully to {file_path}")
except Exception as e:
# If an error occurs during file saving, display an error message
messagebox.showerror("Error", f"Failed to export data: {e}")
# Tkinter GUI setup
my_w = tk.Tk() # Create the main Tkinter window
my_w.title("Excel to XML Exporter") # Set the title of the window
my_w.geometry("400x200") # Set the dimensions of the window
# Button to load an Excel file
btn_load_excel = tk.Button(
my_w, # Parent window
text="Load Excel", # Button label
command=load_excel, # Function to execute on button click
width=15, # Width of the button
bg="lightblue" # Background color of the button
)
btn_load_excel.pack(pady=10) # Add padding and pack the button into the window
# Button to export data to an XML file
btn_export_xml = tk.Button(
my_w, # Parent window
text="Export to XML", # Button label
command=export_to_xml, # Function to execute on button click
width=15, # Width of the button
bg="lightgreen" # Background color of the button
)
btn_export_xml.pack(pady=10) # Add padding and pack the button into the window
# Start the Tkinter event loop
my_w.mainloop()
While this tutorial focused on Excel as the data source, the same approach can be applied to other sources such as database tables, SQL queries, or even real-time data streams. Let’s explore how these sources can be integrated into the current workflow:
Instead of loading data from an Excel file, we can connect to a database (e.g., SQLite, MySQL, PostgreSQL) and fetch table data. Using libraries like SQLAlchemy or PyODBC, the data can be queried and loaded into a Pandas DataFrame for further processing.
from sqlalchemy import create_engine
import pandas as pd
# Connect to SQLite Database
engine = create_engine('sqlite:///example.db') # Replace with your database path
# Fetch data from a specific table
query = "SELECT * FROM employees" # Adjust table name and fields as needed
df = pd.read_sql(query, engine)
# Convert DataFrame to XML
df.to_xml('output.xml', index=False)
Use Case:
Custom SQL queries can be used to apply filters or retrieve specific subsets of data. For instance, fetching data from an orders table for a particular customer or date range can be integrated seamlessly.
query = "SELECT * FROM orders WHERE customer_id = 123 AND order_date >= '2023-01-01'"
df = pd.read_sql(query, engine)
# Convert filtered data to XML
df.to_xml('filtered_orders.xml', index=False)
Use Case:
Data can also come from APIs or real-time streams, such as RESTful APIs or message queues. Libraries like requests can be used to fetch JSON or CSV responses, which can then be converted to Pandas DataFrames.
import requests
import pandas as pd
# Fetch data from an API
response = requests.get('https://api.example.com/data')
data = response.json()
# Load JSON data into a Pandas DataFrame
df = pd.DataFrame(data)
# Convert DataFrame to XML
df.to_xml('api_data.xml', index=False)
Use Case:
Other file formats like CSV and JSON can also be used as sources. With Pandas’ built-in methods, these formats can be loaded and converted to XML just like Excel.
# Load JSON data into DataFrame
df = pd.read_json('data.json')
# Convert DataFrame to XML
df.to_xml('output.xml', index=False)
Use Case:
By diversifying data sources, this tool can cater to broader use cases, ranging from small file-based tasks to enterprise-level database operations. This adaptability makes it a valuable resource for developers and analysts alike.