-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcsv_editor.py
More file actions
270 lines (216 loc) · 8.55 KB
/
csv_editor.py
File metadata and controls
270 lines (216 loc) · 8.55 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# pylint: disable=C0103,C0111,W0614,W0401,C0200,C0325
from Tkinter import *
import tkMessageBox
import tkFileDialog
import tkFont
import csv
##
# CSV GUI Editor written in python using tkinter
# - A lightweight csv editor
# - (c) 2017 Sebastian Safari ssebs@ymail.com
##
##
# TODO: Add + / - buttons to create/remove rows & coloumns
# TODO: Add resizing of cells
##
class Application(Frame):
cellList = []
currentCells = []
currentCell = None
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.createDefaultWidgets()
def focus_tab(self, event):
event.widget.tk_focusNext().focus()
return "break"
def focus_sh_tab(self, event):
event.widget.tk_focusPrev().focus()
return "break"
def focus_right(self, event):
#event.widget.tk_focusNext().focus()
widget = event.widget.focus_get()
for i in range(len(self.currentCells)):
for j in range(len(self.currentCells[0])):
if widget == self.currentCells[i][j]:
if(j >= len(self.currentCells[0]) - 1 ):
j = -1
self.currentCells[i][j+1].focus()
return "break"
def focus_left(self, event):
#event.widget.tk_focusNext().focus()
widget = event.widget.focus_get()
for i in range(len(self.currentCells)):
for j in range(len(self.currentCells[0])):
if widget == self.currentCells[i][j]:
if(j == 0):
j = len(self.currentCells[0])
self.currentCells[i][j-1].focus()
return "break"
def focus_up(self, event):
#event.widget.tk_focusNext().focus()
widget = event.widget.focus_get()
for i in range(len(self.currentCells)):
for j in range(len(self.currentCells[0])):
if widget == self.currentCells[i][j]:
if(i < 0):
i = len(self.currentCells)
self.currentCells[i-1][j].focus()
return "break"
def focus_down(self, event):
#event.widget.tk_focusNext().focus()
widget = event.widget.focus_get()
for i in range(len(self.currentCells)):
for j in range(len(self.currentCells[0])):
if widget == self.currentCells[i][j]:
if( i >= len(self.currentCells) - 1):
i = -1
self.currentCells[i+1][j].focus()
return "break"
def selectall(self, event):
event.widget.tag_add("sel", "1.0", "end")
event.widget.mark_set(INSERT, "1.0")
event.widget.see(INSERT)
return "break"
def saveFile(self, event):
self.saveCells()
# TODO: Create bind for arrow keys and enter
def createDefaultWidgets(self):
w, h = 7, 1
self.sizeX = 4
self.sizeY = 6
self.defaultCells = []
for i in range(self.sizeY):
self.defaultCells.append([])
for j in range(self.sizeX):
self.defaultCells[i].append([])
for i in range(self.sizeY):
for j in range(self.sizeX):
tmp = Text(self, width=w, height=h)
tmp.bind("<Tab>", self.focus_tab)
tmp.bind("<Shift-Tab>", self.focus_sh_tab)
tmp.bind("<Return>", self.focus_down)
tmp.bind("<Shift-Return>", self.focus_up)
tmp.bind("<Right>", self.focus_right)
tmp.bind("<Left>", self.focus_left)
tmp.bind("<Up>", self.focus_up)
tmp.bind("<Down>", self.focus_down)
tmp.bind("<Control-a>", self.selectall)
tmp.bind("<Control-s>", self.saveFile)
#TODO: Add resize check on column when changing focus
tmp.insert(END, "")
tmp.grid(padx=0, pady=0, column=j, row=i)
self.defaultCells[i][j] = tmp
self.cellList.append(tmp)
self.defaultCells[0][0].focus_force()
self.currentCells = self.defaultCells
self.currentCell = self.currentCells[0][0]
# TODO: Add buttons to create new rows/columns
def newCells(self):
self.removeCells()
self.createDefaultWidgets()
def removeCells(self):
while(len(self.cellList) > 0):
for cell in self.cellList:
# print str(i) + str(j)
cell.destroy()
self.cellList.remove(cell)
def loadCells(self):
filename = tkFileDialog.askopenfilename(initialdir=".", title="Select file",
filetypes=(("csv files", "*.csv"), ("all files", "*.*")))
ary = []
col = -1
rows = []
# get array size & get contents of rows
with open(filename, "rb") as csvfile:
rd = csv.reader(csvfile, delimiter=",", quotechar='"')
for row in rd:
ary.append([])
col = len(row)
rows.append(row)
# create the array
for i in range(len(ary)):
for j in range(col):
ary[i].append([])
# fill the array
for i in range(len(ary)):
for j in range(col):
# print rows[i][j]
ary[i][j] = rows[i][j]
self.removeCells()
# get the max width of the cells
mx = 0
for i in range(len(ary)):
for j in range(len(ary[0])):
if(len(ary[i][j]) >= mx):
mx = len(ary[i][j])
w = mx
loadCells = []
for i in range(len(ary)):
loadCells.append([])
for j in range(len(ary[0])):
loadCells[i].append([])
# create the new cells
for i in range(len(ary)):
for j in range(len(ary[0])):
tmp = Text(self, width=w, height=1)
tmp.bind("<Tab>", self.focus_tab)
tmp.bind("<Shift-Tab>", self.focus_sh_tab)
tmp.bind("<Return>", self.focus_down)
tmp.bind("<Shift-Return>", self.focus_up)
tmp.bind("<Right>", self.focus_right)
tmp.bind("<Left>", self.focus_left)
tmp.bind("<Up>", self.focus_up)
tmp.bind("<Down>", self.focus_down)
tmp.bind("<Control-a>", self.selectall)
tmp.bind("<Control-s>", self.saveFile)
tmp.insert(END, ary[i][j])
if(i == 0):
tmp.config(font=("Helvetica", 10, tkFont.BOLD))
tmp.config(relief=FLAT, bg=app.master.cget('bg'))
loadCells[i][j] = tmp
tmp.focus_force()
self.cellList.append(tmp)
tmp.grid(padx=0, pady=0, column=j, row=i)
self.currentCells = loadCells
self.currentCell = self.currentCells[0][0]
def saveCells(self):
filename = tkFileDialog.asksaveasfilename(initialdir=".", title="Save File", filetypes=(
("csv files", "*.csv"), ("all files", "*.*")), defaultextension=".csv")
vals = []
for i in range(len(self.currentCells)):
for j in range(len(self.currentCells[0])):
vals.append(self.currentCells[i][j].get(1.0, END).strip())
with open(filename, "wb") as csvfile:
for rw in range(len(self.currentCells)):
row = ""
for i in range(len(self.currentCells[0])):
x = rw * len(self.currentCells[0])
if(i != len(self.currentCells[0]) - 1):
row += vals[x + i] + ","
else:
row += vals[x + i]
csvfile.write(row + "\n")
tkMessageBox.showinfo("", "Saved!")
# End Application Class #
# Begin functions #
def hello():
tkMessageBox.showinfo("", "Hello!")
# End functions #
### CODE ENTRY ###
app = Application()
menubar = Menu(app)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=app.newCells) # add save dialog
# add save dialog
filemenu.add_command(label="Open", command=app.loadCells)
filemenu.add_command(label="Save as", command=app.saveCells)
filemenu.add_command(label="Exit", command=app.quit)
menubar.add_cascade(label="File", menu=filemenu)
menubar.add_command(label="Exit", command=app.quit)
app.master.title('CSV Editor')
app.master.config(menu=menubar)
default_font = tkFont.nametofont("TkTextFont")
default_font.configure(family="Helvetica")
app.option_add("*Font", default_font)
app.mainloop()