-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImportSS.py
363 lines (334 loc) · 13.5 KB
/
ImportSS.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
ImportExcel.py
A Python module to read Excel files into Salstat. Does not include xlsx format
(at least, not just yet)
"""
from __future__ import unicode_literals
import os, os.path
import wx, wx.aui, xlrd
import wx.grid as gridlib
from xlrd import open_workbook
from xlwt import Workbook
import ezodf
################################################
# Save in xlsx format
def SaveToExcel(filename, grid):
book = Workbook()
book = Workbook(encoding='utf-8')
datasheet = book.add_sheet('Data')
metasheet = book.add_sheet('Meta')
metasheet.write(0,0,"Name")
metasheet.write(1,0,"Alignment")
metasheet.write(2,0,"Label")
metasheet.write(3,0,"Measure")
metasheet.write(4,0,"Variable type")
metasheet.write(5,0,"Decimal places")
metasheet.write(6,0,"Missing values")
numcols = grid.GetNumberCols()
numrows = grid.GetNumberRows()
for idxx in range(numcols):
for idxy in range(numrows):
val = grid.GetCellValue(idxy, idxx)
datasheet.write(idxy + 1, idxx, val)
label = grid.GetColLabelValue(idxx)
meta = grid.meta[idxx] # was [label]
datasheet.write(0, idxx, meta['name'])
metasheet.write(0, idxx + 1, meta['name'])
metasheet.write(1, idxx + 1, meta['align'])
metasheet.write(2, idxx + 1, meta['label'])
metasheet.write(3, idxx + 1, meta['measure'])
metasheet.write(4, idxx + 1, meta['ivdv'])
metasheet.write(5, idxx + 1, meta['decplaces'])
metasheet.write(6, idxx + 1, meta['missingvalues'])
book.save(filename)
################################################
# Save in xlsx format
def SaveToLibre(filename, grid):
numcols = grid.GetNumberCols()
numrows = grid.GetNumberRows()
book = ezodf.newdoc(doctype="ods", filename=filename)
book.sheets += ezodf.Sheet('Data', size=(numrows + 10, numcols + 10))
book.sheets += ezodf.Sheet('Meta', size=(20, numcols + 10))
datasheet = book.sheets['Data']
metasheet = book.sheets['Meta']
metasheet[(0,0)].set_value("Name")
metasheet[(1,0)].set_value("Alignment")
metasheet[(2,0)].set_value("Label")
metasheet[(3,0)].set_value("Measure")
metasheet[(4,0)].set_value("Variable type")
metasheet[(5,0)].set_value("Decimal places")
metasheet[(6,0)].set_value("Missing values")
for idxx in range(numcols):
for idxy in range(numrows):
val = grid.GetCellValue(idxy, idxx)
datasheet[(idxy + 1, idxx)].set_value(val)
label = grid.GetColLabelValue(idxx)
meta = grid.meta[idxx] # was [label]
datasheet[(0, idxx)].set_value(meta['name'])
metasheet[(0, idxx + 1)].set_value(meta['name'])
metasheet[(1, idxx + 1)].set_value(meta['align'])
metasheet[(2, idxx + 1)].set_value(meta['label'])
metasheet[(3, idxx + 1)].set_value(meta['measure'])
metasheet[(4, idxx + 1)].set_value(meta['ivdv'])
metasheet[(5, idxx + 1)].set_value(meta[str('decplaces')])
metasheet[(6, idxx + 1)].set_value(meta['missingvalues'])
book.save()
################################################
# Custom grid class for previewing data
class Grid(gridlib.Grid):
def __init__(self, parent, pos):
gridlib.Grid.__init__(self, parent, size=(560,200), pos=(0,0))
def ResizeGrid(self, nCols, nRows, spare=10):
# check that data is saved before clearing!
self.ClearGrid()
# resize grid to accommodate data
actual_cols = self.GetNumberCols()
num_cols_to_append = nCols + spare
self.DeleteCols(pos=0, numCols=actual_cols)
self.AppendCols(numCols=num_cols_to_append)
actual_rows = self.GetNumberRows()
num_rows_to_append = nRows + spare
self.DeleteRows(pos=0, numRows=actual_rows)
self.AppendRows(numRows=num_rows_to_append)
def GetFileSizes(filename):
try:
workbook = open_workbook(filename)
except: # what error?
pass # seriously, what?
nsheets = workbook.nsheets
sheet_names = workbook.sheet_names()
for idx_sheet in range(nsheets):
sheet = workbook.sheet_by_index(idx_sheet)
def GetCells(workbook, sheet_number):
worksheet = workbook.sheet_by_index(sheet_number)
nrows = worksheet.nrows
ncols = worksheet.ncols
# resize grid to accomodate data size
for idx_row in range(nrows):
for idx_col in range(ncols):
val = worksheet.cell(idx_row, idx_col)
# set cell of grid to 'val'
################################################
# Dialog to retrieve file name. Needed before we can do anything
class GetFilename(object):
def __init__(self, parent, startDir):
dlg = wx.FileDialog(parent, message="Open a spreadsheet file", defaultDir=startDir, \
wildcard="Excel (newer) (*.xlsx)|*.xlsx|Excel (older) (*.xls)|*.xls|\
LibreOffice Calc (*.ods)|*.ods")
ico = wx.Icon('icons/PurpleIcon05_32.png',wx.BITMAP_TYPE_PNG)
dlg.SetIcon(ico)
if dlg.ShowModal() == wx.ID_OK:
self.fileName = dlg.GetPath()
if os.path.exists(self.fileName):
self.fileName
else:
self.fileName = None
else:
self.fileName = None
################################################
# Panel for spreadsheets
class PanelSpreadsheet(wx.Panel):
def __init__(self, parent, fileName, pos, size):
wx.Panel.__init__(self, parent, -1, pos=pos, size=size)
self.parent = parent
help_text = "Salstat can load files from Excel and LibreOffice/OpenOffice Calc. "
t1 = wx.StaticText(self, -1, label=help_text, pos=(10,10))
t1.Wrap(545)
################################################
# Main class that binds it all together
class ImportDialog(wx.Dialog):
def __init__(self, FileName):
wx.Dialog.__init__(self, None, title="Importing a spreadsheet file",\
size=(600,380))
self.FileName = FileName
self.excel = ['.xls','.xlsx']
self.loo = ['.ods']
extension = os.path.splitext(self.FileName.fileName)[1]
if extension.lower() in self.excel:
self.ftype = "excel"
elif extension.lower() in self.loo:
self.ftype = "libre"
else:
self.ftype = None
if not self.ftype:
self.CancelButton(None)
help_text = "Salstat can load files from Excel and LibreOffice/OpenOffice. "
t1 = wx.StaticText(self, -1, label=help_text, pos=(20,10))
t1.Wrap(545)
t2 = wx.StaticText(self,-1,label="Import from row:",pos=(20,50))
self.headerRow = wx.CheckBox(self, 760, " Header on first row", \
pos=(210,50))
self.dataRow = wx.SpinCtrl(self,766,min=1,max=100, initial=1, \
pos=(140,49),size=(50,-1))
self.dataRow.SetValue(1)
self.buttonImport = wx.Button(self,763,"Import", \
pos=(500,310),size=(70,-1))
self.buttonImport.SetDefault()
self.buttonCancel = wx.Button(self,764,"Cancel", \
pos=(405,310),size=(70,-1))
self.worksheets = wx.aui.AuiNotebook(self, -1, pos=(20,90), \
size=(560,210),style=wx.NB_TOP|wx.aui.AUI_NB_WINDOWLIST_BUTTON)
self.MaxRows = 100
self.MaxCols = 40
self.gridFont = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL)
if self.ftype == "excel":
self.workbook = xlrd.open_workbook(self.FileName.fileName)
sheets = self.workbook.sheets()
elif self.ftype == "libre":
self.workbook = ezodf.opendoc(self.FileName.fileName)
sheets = self.workbook.sheets
self.grids = []
for idx, sheet in enumerate(sheets):
self.grids.append(Grid(self.worksheets, -1))
self.grids[idx].HideRowLabels()
self.grids[idx].SetColLabelSize(16)
self.grids[idx].SetRowLabelSize(40)
self.grids[idx].SetDefaultColSize(60)
self.grids[idx].CreateGrid(self.MaxRows,self.MaxCols)
for i in range(self.MaxCols):
self.grids[idx].SetColLabelValue(i, " ")
for i in range(self.MaxRows):
self.grids[idx].SetRowSize(i,16)
self.grids[idx].SetDefaultCellFont(self.gridFont)
self.worksheets.AddPage(self.grids[idx],sheet.name)
if self.ftype == 'excel':
nrows = sheet.nrows
ncols = sheet.ncols
elif self.ftype == 'libre':
nrows = sheet.nrows()
ncols = sheet.ncols()
self.grids[idx].ResizeGrid(ncols, nrows)
for idx_row in range(nrows):
for idx_col in range(ncols):
if self.ftype == 'excel':
val = sheet.cell(idx_row, idx_col).value
elif self.ftype == 'libre':
val = sheet[idx_row, idx_col].value
self.grids[idx].SetCellValue(idx_row, idx_col, str(val))
# It seems odd but it seems better for all events to be routed to the same method
self.Bind(wx.EVT_CHECKBOX, self.AdjustGrid, id=760)
self.Bind(wx.EVT_SPINCTRL, self.AdjustGrid, id=766)
self.Bind(wx.EVT_BUTTON, self.CancelButton, id=764)
self.Bind(wx.EVT_BUTTON, self.ImportButton, id=763)
def AttemptPreview(self, event=None):
beginRow = self.dataRow.GetValue()
if self.headerRow.IsChecked():
pass
def CancelButton(self, event):
# let's get out!
self.FileName.fileName = None
self.Close()
def AdjustGrid(self, event):
if self.ftype == "excel":
sheets = self.workbook.sheets()
elif self.ftype == "libre":
sheets = self.workbook.sheets
sheetIdx = self.worksheets.GetSelection()
sheet = sheets[sheetIdx]
self.grids[sheetIdx].ClearGrid()
# beginData = # get row where data begins
if self.ftype == 'excel':
nrows = sheet.nrows
ncols = sheet.ncols
elif self.ftype == 'libre':
nrows = sheet.nrows()
ncols = sheet.ncols()
self.grids[sheetIdx].ClearGrid()
# get headers
beginRow = self.dataRow.GetValue()-1
if self.headerRow.IsChecked():
for idxCol in range(ncols):
if self.ftype == 'excel':
val = sheet.cell(beginRow, idxCol).value
elif self.ftype == 'libre':
val = sheet[beginRow, idxCol].value
self.grids[sheetIdx].SetColLabelValue(idxCol, str(val))
bonus = 1
else:
self.headers = []
bonus = 0
for idx_row in range(beginRow+bonus,nrows):
for idx_col in range(ncols):
if self.ftype == 'excel':
val = sheet.cell(idx_row, idx_col).value
elif self.ftype == 'libre':
val = sheet[idx_row, idx_col].value
self.grids[sheetIdx].SetCellValue(idx_row-beginRow-bonus, idx_col, str(val))
def ImportButton(self, event):
if self.ftype == "excel":
sheets = self.workbook.sheets()
elif self.ftype == "libre":
sheets = self.workbook.sheets
self.varnames = []
self.gridData = []
sheetIdx = self.worksheets.GetSelection()
sheet = sheets[sheetIdx]
# beginData = # get row where data begins
if self.ftype == 'excel':
nrows = sheet.nrows
ncols = sheet.ncols
elif self.ftype == 'libre':
nrows = sheet.nrows()
ncols = sheet.ncols()
# get headers
beginRow = self.dataRow.GetValue()-1
if self.headerRow.IsChecked():
self.headers = []
for idxCol in range(ncols):
if self.ftype == 'excel':
val = sheet.cell(beginRow, idxCol).value
elif self.ftype == 'libre':
val = sheet[beginRow, idxCol].value
self.headers.append(val)
bonus = 1
else:
self.headers = []
bonus = 0
for idx_row in range(beginRow+bonus, nrows):
line = []
for idx_col in range(ncols):
if self.ftype == 'excel':
val = sheet.cell(idx_row, idx_col).value
elif self.ftype == 'libre':
val = sheet[idx_row, idx_col].value
line.append(val)
self.gridData.append(line)
self.Close()
################################################
# Function to control all this
def ImportSS(frame, startDir):
"""
Controls all the module.
Parameters: startDir: where to start the file dialog
Returns:
filename: Path + File name selected by user
headers: Variable name (None if none selected)
data: data as a list of lists
"""
FileName = GetFilename(frame, startDir)
if FileName.fileName == None:
return None
dlg = ImportDialog(FileName)
if dlg.ShowModal():
if dlg.FileName.fileName == None:
dlg.Destroy()
return None
else:
fileName = dlg.FileName.fileName
variableNames = dlg.headers
gridData = dlg.data
dlg.Destroy()
return fileName, variableNames, gridData
else:
dlg.Destroy()
return None
################################################
# Main routine for testing only!
if __name__ == '__main__':
startDir = os.path.expanduser("~")
app = wx.App(False)
x = ImportCSV(startDir)
#x.Close()
#frame = ImportDialog(startDir)
#frame.Show()
app.MainLoop()