|
| 1 | +import pandas as pd |
| 2 | +from tkinter import * |
| 3 | +from tkinter import filedialog |
| 4 | +from tkinter import messagebox as msg |
| 5 | +from pandastable import Table |
| 6 | +import openpyxl |
| 7 | + |
| 8 | + |
| 9 | +class csv_to_excel: |
| 10 | + |
| 11 | + def __init__(self, root): |
| 12 | + |
| 13 | + self.root = root |
| 14 | + self.file_name = '' |
| 15 | + self.f = Frame(self.root, |
| 16 | + height = 200, |
| 17 | + width = 300) |
| 18 | + |
| 19 | + # Place the frame on root window |
| 20 | + self.f.pack() |
| 21 | + |
| 22 | + # Creating label widgets |
| 23 | + self.message_label = Label(self.f, |
| 24 | + text = 'FILE CONVERTER', |
| 25 | + font = ('Arial', 19,'underline'), |
| 26 | + fg = 'Green') |
| 27 | + self.message_label2 = Label(self.f, |
| 28 | + text = 'Converter of CSV to any file', |
| 29 | + font = ('Arial', 14,'underline'), |
| 30 | + fg = 'Red') |
| 31 | + |
| 32 | + # Buttons |
| 33 | + self.convert_button = Button(self.f, |
| 34 | + text = 'Convert', |
| 35 | + font = ('Arial', 14), |
| 36 | + bg = 'Orange', |
| 37 | + fg = 'Black', |
| 38 | + command = self.convert_csv_to_xls) |
| 39 | + self.display_button = Button(self.f, |
| 40 | + text = 'Display', |
| 41 | + font = ('Arial', 14), |
| 42 | + bg = 'Green', |
| 43 | + fg = 'Black', |
| 44 | + command = self.display_xls_file) |
| 45 | + self.exit_button = Button(self.f, |
| 46 | + text = 'Exit', |
| 47 | + font = ('Arial', 14), |
| 48 | + bg = 'Red', |
| 49 | + fg = 'Black', |
| 50 | + command = root.destroy) |
| 51 | + |
| 52 | + # Placing the widgets using grid manager |
| 53 | + self.message_label.grid(row = 1, column = 1) |
| 54 | + self.message_label2.grid(row = 2, column = 1) |
| 55 | + self.convert_button.grid(row = 3, column = 0, |
| 56 | + padx = 0, pady = 15) |
| 57 | + self.display_button.grid(row = 3, column = 1, |
| 58 | + padx = 10, pady = 15) |
| 59 | + self.exit_button.grid(row = 3, column = 2, |
| 60 | + padx = 10, pady = 15) |
| 61 | + |
| 62 | + def convert_csv_to_xls(self): |
| 63 | + try: |
| 64 | + self.file_name = filedialog.askopenfilename(initialdir = '/Desktop', |
| 65 | + title = 'Select a CSV file', |
| 66 | + filetypes = (('csv file','*.csv'), |
| 67 | + ('csv file','*.csv'))) |
| 68 | + |
| 69 | + df = pd.read_csv(self.file_name) |
| 70 | + |
| 71 | + # Next - Pandas DF to Excel file on disk |
| 72 | + if(len(df) == 0): |
| 73 | + msg.showinfo('No Rows Selected', 'CSV has no rows') |
| 74 | + else: |
| 75 | + |
| 76 | + # saves in the current directory |
| 77 | + with pd.ExcelWriter('excel_file_projects.xls') as writer: |
| 78 | + df.to_excel(writer,'GFGSheet') |
| 79 | + writer.save() |
| 80 | + msg.showinfo('Excel file created', 'Excel File created') |
| 81 | + |
| 82 | + except FileNotFoundError as e: |
| 83 | + msg.showerror('Error in opening file', e) |
| 84 | + |
| 85 | + def display_xls_file(self): |
| 86 | + try: |
| 87 | + self.file_name = filedialog.askopenfilename(initialdir = '/Desktop', |
| 88 | + title = 'Select a excel file', |
| 89 | + filetypes = (('excel file','*.xls'), |
| 90 | + ('excel file','*.xls'))) |
| 91 | + df = pd.read_excel(self.file_name) |
| 92 | + |
| 93 | + if (len(df)== 0): |
| 94 | + msg.showinfo('No records', 'No records') |
| 95 | + else: |
| 96 | + pass |
| 97 | + |
| 98 | + # Now display the DF in 'Table' object |
| 99 | + # under'pandastable' module |
| 100 | + self.f2 = Frame(self.root, height=200, width=300) |
| 101 | + self.f2.pack(fill=BOTH,expand=1) |
| 102 | + self.table = Table(self.f2, dataframe=df,read_only=True) |
| 103 | + self.table.show() |
| 104 | + |
| 105 | + except FileNotFoundError as e: |
| 106 | + print(e) |
| 107 | + msg.showerror('Error in opening file',e) |
| 108 | + |
| 109 | +# Driver Code |
| 110 | +root = Tk() |
| 111 | +root.title('---Convert CSV to any File----') |
| 112 | + |
| 113 | +obj = csv_to_excel(root) |
| 114 | +root.geometry('800x600') |
| 115 | +root.mainloop() |
0 commit comments