Advertisement
Najeebsk

CSV-JSON-XLSX-READER.py

Mar 17th, 2024 (edited)
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.77 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk, filedialog
  3. import pandas as pd
  4.  
  5. def open_file():
  6.     file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv"), ("Excel files", "*.xlsx"), ("JSON files", "*.json")])
  7.     if file_path:
  8.         if file_path.endswith('.csv'):
  9.             df = pd.read_csv(file_path)
  10.         elif file_path.endswith('.xlsx'):
  11.             df = pd.read_excel(file_path)
  12.         elif file_path.endswith('.json'):
  13.             df = pd.read_json(file_path)
  14.         display_data(df)
  15.  
  16. def display_data(df):
  17.     # Creating a new Frame inside the root window
  18.     frame = ttk.Frame(root)
  19.     frame.pack(fill=tk.BOTH, expand=True)
  20.  
  21.     # Adding vertical scrollbar
  22.     scrollbar_y = ttk.Scrollbar(frame, orient=tk.VERTICAL)
  23.     scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
  24.  
  25.     # Adding horizontal scrollbar
  26.     scrollbar_x = ttk.Scrollbar(frame, orient=tk.HORIZONTAL)
  27.     scrollbar_x.pack(side=tk.BOTTOM, fill=tk.X)
  28.  
  29.     # Adding Text widget
  30.     text = tk.Text(frame, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set)
  31.     text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  32.  
  33.     # Configuring Scrollbars to work with Text widget
  34.     scrollbar_y.config(command=text.yview)
  35.     scrollbar_x.config(command=text.xview)
  36.  
  37.     # Inserting data into Text widget
  38.     text.insert(tk.END, df.to_string(index=False))
  39.  
  40.     # Configuring text color
  41.     text.config(fg="black", bg="white")
  42.  
  43. # Main GUI window
  44. root = tk.Tk()
  45. root.title("File Reader")
  46.  
  47. # Changing the background color of root window
  48. root.config(bg="lightgrey")
  49.  
  50. # Adding Open File button
  51. open_button = ttk.Button(root, text="Open File", command=open_file)
  52. open_button.pack(pady=10)
  53.  
  54. # Apply a new theme
  55. style = ttk.Style()
  56. style.theme_use("clam")
  57.  
  58. root.mainloop()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement