00001
00002 """
00003 Created on Fri Nov 11 16:49:46 2011
00004
00005 @author: Sat Kumar Tomer
00006 @website: www.ambhas.com
00007 @email: satkumartomer@gmail.com
00008 """
00009
00010 import xlrd, xlwt
00011 import numpy as np
00012
00013 class xlsread():
00014 """
00015 A class to read data from xls file
00016 based on the 'xlrd'
00017
00018 Example:
00019 fname = '/home/tomer/rain_projection/raw_data/a2_0.5.xls'
00020 foo = xlsread(fname)
00021 var = foo.get_cells('a3:a5', 'Sheet1')
00022 """
00023
00024 def __init__(self, fname):
00025 self.fname = fname
00026 book = xlrd.open_workbook(self.fname)
00027 self.sheet_names = book.sheet_names()
00028 self.book = book
00029
00030 def get_cells(self, cell_range, sheet):
00031 """
00032 cell_range: a single cell i.e. 'a2'
00033 range of cells i.e. 'a2:f5'
00034 sheet: name of the sheet, must be string
00035 """
00036 book = self.book
00037 sheet = book.sheet_by_name(sheet)
00038
00039 if ':' not in cell_range:
00040 foo1 = cell_range
00041 row,col = self.__cell2ind__(foo1)
00042 data = sheet.cell_value(row,col)
00043 else:
00044 foo1, foo2 = cell_range.split(':')
00045 row1,col1 = self.__cell2ind__(foo1)
00046 row2,col2 = self.__cell2ind__(foo2)
00047
00048 if row2<row1:
00049 raise Exception('row_start should be <= row_end')
00050 if col2<col1:
00051 raise Exception('col_start should be <= col_end')
00052
00053 data = []
00054 for i in range(row1,row2+1):
00055 data_row = []
00056 for j in range(col1,col2+1):
00057 if sheet.cell_value(i,j):
00058 data_row.append(sheet.cell_value(i,j))
00059 else:
00060 if sheet.cell_value(i,j) == 0:
00061
00062 data_row.append(sheet.cell_value(i,j))
00063 else:
00064
00065 data_row.append(np.nan)
00066
00067 data.append(data_row)
00068
00069 return np.array(data)
00070
00071 def __cell2ind__(self,foo):
00072 """
00073 given the cell number i.e. (AA100)
00074 returns the row and column of cell in indices format i.e. 0, 10
00075 """
00076
00077
00078 foo_str = foo[0].lower()
00079 col = ord(foo_str)-ord('a')
00080
00081
00082 try:
00083 row = int(foo[1:])-1
00084 except:
00085 row = int(foo[2:])-1
00086 foo_str = foo[1].lower()
00087 col = (col+1)*26+ ord(foo_str)-ord('a')
00088
00089 return row, col
00090
00091
00092 class xlswrite():
00093 """
00094 This saves the array in xls format
00095
00096 Example:
00097 var = np.array([[5,10,12],[2,5,6]])
00098 xls_out_file = xlswrite(var, 'f10', 'Sheet1')
00099 fname = '/home/tomer/data.xls'
00100 foo1.save(fname)
00101 """
00102
00103 def __init__(self, data, cell_start, sheet):
00104 self.data = data
00105 self.cell_start = cell_start
00106
00107
00108 book = xlwt.Workbook()
00109 sheet = book.add_sheet(sheet)
00110
00111
00112 row, col = self.__cell2ind__(cell_start)
00113
00114 if isinstance(data, str) or isinstance(data, float) or isinstance(data,int):
00115 sheet.write(row,col,data)
00116
00117 if data.ndim == 1:
00118 for i in range(data.shape[0]):
00119 sheet.write(row+i,col, data[i])
00120
00121 else:
00122 for i in range(data.shape[0]):
00123 for j in range(data.shape[1]):
00124 sheet.write(row+i, col+j, data[i,j])
00125
00126 self.book = book
00127
00128
00129 def save(self, fname):
00130 self.book.save(fname)
00131
00132
00133 def __cell2ind__(self, foo):
00134 """
00135 given the cell number i.e. (AA100)
00136 returns the row and column of cell in indices format i.e. 0,10
00137 """
00138
00139
00140 foo_str = foo[0].lower()
00141 col = ord(foo_str)-ord('a')
00142
00143
00144 try:
00145 row = int(foo[1:])-1
00146 except:
00147 row = int(foo[2:])-1
00148 foo_str = foo[1].lower()
00149 col = (col+1)*26+ ord(foo_str)-ord('a')
00150
00151 return row,col
00152
00153
00154 class xlswrite2(xlswrite):
00155 """
00156 This saves the array in xls format
00157
00158 Example:
00159 var = np.array([[5,10,12],[2,5,6]])
00160 fname = '/home/tomer/data.xls'
00161 xls_out_file = xlswrite(fname)
00162 xls_out_file.write(var, 'f10', 'Sheet1')
00163 xls_out_file.save()
00164 """
00165
00166 def __init__(self, fname):
00167 self.fname = fname
00168
00169
00170 self.book = xlwt.Workbook()
00171
00172 def write(self, data, cell_start, sheet):
00173 sheet = book.add_sheet(sheet)
00174
00175
00176 row, col = self.__cell2ind__(cell_start)
00177
00178 if isinstance(data, str) or isinstance(data, float) or isinstance(data,int):
00179 sheet.write(row,col,data)
00180
00181 if data.ndim == 1:
00182 for i in range(data.shape[0]):
00183 sheet.write(row+i,col, data[i])
00184
00185 else:
00186 for i in range(data.shape[0]):
00187 for j in range(data.shape[1]):
00188 sheet.write(row+i, col+j, data[i,j])
00189
00190
00191 def save(self):
00192 self.book.save(self.fname)
00193
00194
00195 if __name__ == "__main__":
00196
00197
00198 fname = '/home/tomer/rain_projection/raw_data/a2_0.5.xls'
00199
00200 foo = xlsread(fname)
00201 var = foo.get_cells('a3:a5', 'Sheet1')
00202
00203 book = xlrd.open_workbook(fname)
00204 sheet = book.sheet_by_name('Sheet1')
00205 sheet.cell_value(0,0)
00206
00207 print var
00208
00209
00210 var = np.array([[5,10,12],[2,5,6]])
00211 foo1 = xlswrite(var, 'f10', 'Sheet1')
00212 fname = '/home/tomer/data.xls'
00213 foo1.save(fname)
00214
00215
00216