-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
288 lines (265 loc) · 11.7 KB
/
interface.py
File metadata and controls
288 lines (265 loc) · 11.7 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/python3
# -*- coding:utf-8 -*-
from writeTable import *
from readTable import *
import tkinter as tk
from tkinter import filedialog, dialog, messagebox
import os
file_path = ''
def openfile():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
def outputCSV():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
if file_path == '':
return ''
path, suffix = os.path.splitext(file_path)
filesuffix = suffix.strip().lower()
if filesuffix.lower() == '.csv':
try:
writeCSV(file_path[0:-4] + '.csv', readCSV(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening csv')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.csv')
print('write csv successfully')
elif filesuffix.lower() == '.xls':
try:
writeCSV(file_path[0:-4] + '.csv', read03xls(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xls')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.csv')
print('write csv successfully')
elif filesuffix.lower() == '.xlsx':
try:
writeCSV(file_path[0:-5] + '.csv', read07xlsx(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xlsx')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-5] + '.csv')
print('write csv successfully')
else:
messagebox.showerror('Something Wrong',
'Unsopported file suffix!\nOnly ".xls", ".xlsx", ".csv" are permitted')
def outputXLSX():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
if file_path == '':
return ''
path, suffix = os.path.splitext(file_path)
filesuffix = suffix.strip().lower()
if filesuffix.lower() == '.csv':
try:
write07xlsx(file_path[0:-4] + '.xlsx', readCSV(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening csv')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.xlsx')
print('write xlsx successfully')
elif filesuffix.lower() == '.xls':
try:
write07xlsx(file_path[0:-4] + '.xlsx', read03xls(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xls')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.xlsx')
print('write xlsx successfully')
elif filesuffix.lower() == '.xlsx':
try:
write07xlsx(file_path[0:-5] + '.xlsx', read07xlsx(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xlsx')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-5] + '.xlsx')
print('write xlsx successfully')
else:
messagebox.showerror('Something Wrong',
'Unsopported file suffix!\nOnly ".xls", ".xlsx", ".csv" are permitted')
def outputXLS():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
if file_path == '':
return ''
path, suffix = os.path.splitext(file_path)
filesuffix = suffix.strip().lower()
if filesuffix.lower() == '.csv':
try:
write03xls(file_path[0:-4] + '.xls', readCSV(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening csv')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.xls')
print('write xls successfully')
elif filesuffix.lower() == '.xls':
try:
write03xls(file_path[0:-4] + '.xls', read03xls(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xls')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.xls')
print('write xls successfully')
elif filesuffix.lower() == '.xlsx':
try:
write03xls(file_path[0:-5] + '.xls', read07xlsx(file_path))
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xlsx')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-5] + '.xls')
print('write xls successfully')
else:
messagebox.showerror('Something Wrong',
'Unsopported file suffix!\nOnly ".xls", ".xlsx", ".csv" are permitted')
def outputTEX():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
if file_path == '':
return ''
path, suffix = os.path.splitext(file_path)
filesuffix = suffix.strip().lower()
if filesuffix.lower() == '.csv':
try:
writeLaTeX(file_path[0:-4] + '.tex', readCSV(file_path), 1)
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening csv')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.tex')
print('write tex successfully')
elif filesuffix.lower() == '.xls':
try:
writeLaTeX(file_path[0:-4] + '.tex', read03xls(file_path), 1)
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xls')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-4] + '.tex')
print('write tex successfully')
elif filesuffix.lower() == '.xlsx':
try:
writeLaTeX(file_path[0:-5] + '.tex', read07xlsx(file_path), 1)
except Exception as e:
messagebox.showerror('Something Wrong', 'Error occurred when opening xlsx')
else:
messagebox.showinfo('Info', 'Successfully converted!\nFile saved in \n' + file_path[0:-5] + '.tex')
print('write tex successfully')
else:
messagebox.showerror('Something Wrong',
'Unsopported file suffix!\nOnly ".xls", ".xlsx", ".csv" are permitted')
def mainwindow():
def conversion():
window.destroy()
def backmain():
conversionwin.destroy()
mainwindow()
def nodelwin():
messagebox.showinfo('Tip', 'Press Back to Main to Quit')
return ''
global file_path
file_path = ''
conversionwin = tk.Tk()
conversionwin.title('File Format Conversion')
conversionwin.geometry('500x300')
# conversionwin.protocol("WM_DELETE_WINDOW", nodelwin)
banner = 'Source file should have suffix as csv, xls or xlsx'
txt = tk.Label(conversionwin, text=banner, font=('Arial', 12), width=50, height=4)
txt.pack()
print('Enter conversion')
savexlsbut = tk.Button(conversionwin, text='Convert to XLS', font=('Arial', 12), width=30, height=1,
command=outputXLS)
savexlsbut.pack()
savexlsxbut = tk.Button(conversionwin, text='Convert to XLSX', font=('Arial', 12), width=30, height=1,
command=outputXLSX)
savexlsxbut.pack()
savecsvbut = tk.Button(conversionwin, text='Convert to CSV', font=('Arial', 12), width=30, height=1,
command=outputCSV)
savecsvbut.pack()
savetexbut = tk.Button(conversionwin, text='Convert to TEX', font=('Arial', 12), width=30, height=1,
command=outputTEX)
savetexbut.pack()
backbut = tk.Button(conversionwin, text='Back to Main Menu', font=('Arial', 12), width=30, height=1,
command=backmain)
backbut.pack()
conversionwin.mainloop()
return ''
def outputint():
window.destroy()
def backmain():
outputwin.destroy()
mainwindow()
def nodelwin():
messagebox.showinfo('Tip', 'Press Back to Main to Quit')
return ''
def outputlatex():
global file_path
file_path = filedialog.askopenfilename()
print(file_path)
nonlocal text1
path, suffix = os.path.splitext(file_path)
filesuffix = suffix.strip().lower()
if filesuffix.lower() == '.csv':
try:
text1.delete('1.0', tk.END)
text1.insert('insert', writeLaTeX('./tmptex.tex', readCSV(file_path)))
except Exception as e:
text1.delete('1.0', tk.END)
text1.insert('insert', 'Error occurred when opening csv')
else:
print('read csv successfully')
elif filesuffix.lower() == '.xls':
try:
text1.delete('1.0', tk.END)
text1.insert('insert', writeLaTeX('./tmptex.tex', read03xls(file_path)))
except Exception as e:
text1.delete('1.0', tk.END)
text1.insert('insert', 'Error occurred when opening xls')
else:
print('read xls successfully')
elif filesuffix.lower() == '.xlsx':
try:
text1.delete('1.0', tk.END)
text1.insert('insert', writeLaTeX('./tmptex.tex', read07xlsx(file_path)))
except Exception as e:
text1.delete('1.0', tk.END)
text1.insert('insert', 'Error occurred when opening xlsx')
else:
print('read xlsx successfully')
else:
text1.delete('1.0', tk.END)
text1.insert('insert',
'Unsopported file suffix\nOnly ".xls", ".xlsx", ".csv" are permitted\n' + file_path)
os.remove('./tmptex.tex')
global file_path
file_path = ''
outputwin = tk.Tk()
outputwin.title('Output LaTeX Source')
outputwin.geometry('500x300')
# outputwin.protocol("WM_DELETE_WINDOW", nodelwin)
print('Enter outputfunc')
text1 = tk.Text(outputwin, width=50, height=10, bg='cyan', font=('Arial', 12))
text1.pack()
outbut = tk.Button(outputwin, text='Choose File (xls, xlsx, csv)', font=('Arial', 12), width=30, height=1,
command=outputlatex)
outbut.pack()
backbut = tk.Button(outputwin, text='Back to Main Menu', font=('Arial', 12), width=30, height=1,
command=backmain)
backbut.pack()
outputwin.mainloop()
window = tk.Tk()
window.title('LaTeXfromExcel') # set title
window.geometry('500x300') # set size
banner = 'Convert Excel Data to LaTeX Source'
txt = tk.Label(window, text=banner, font=('Arial', 12), width=30, height=6)
txt.pack()
conbut = tk.Button(window, text='File Format Conversion', font=('Arial', 12), width=30, height=1,
command=conversion)
conbut.pack()
outbut = tk.Button(window, text='Output LaTeX Source', font=('Arial', 12), width=30, height=1, command=outputint)
outbut.pack()
window.mainloop() # show window
if __name__ == '__main__':
mainwindow()