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