def create_dataset(self, in_stream): """ Create dataset from first sheet. """ assert XLS_IMPORT xls_book = xlrd.open_workbook(file_contents=in_stream) dataset = tablib.Dataset() sheet = xls_book.sheets()[0] dataset.headers = sheet.row_values(0) for i in moves.range(1, sheet.nrows): row = [] for c in xrange(sheet.ncols): cell = sheet.cell(i, c) cell_value = cell.value if cell.ctype == xlrd.XL_CELL_NUMBER and int(cell_value) == cell_value: cell_value = int(cell_value) elif cell.ctype == xlrd.XL_CELL_DATE: dt_tuple = xlrd.xldate_as_tuple(cell_value, xls_book.datemode) # Create datetime object from this tuple. cell_value = datetime.datetime( dt_tuple[0], dt_tuple[1], dt_tuple[2], dt_tuple[3], dt_tuple[4], dt_tuple[5] ) row.append(cell_value) dataset.append(row) return dataset
def create_dataset(self, in_stream): """ Create dataset from first sheet. """ assert XLS_IMPORT xls_book = xlrd.open_workbook(file_contents=in_stream) dataset = tablib.Dataset() sheet = xls_book.sheets()[0] dataset.headers = sheet.row_values(0) for i in moves.range(1, sheet.nrows): dataset.append(sheet.row_values(i)) return dataset
def import_set(dset, in_stream, headers=True): """Returns databook from XLS stream.""" dset.wipe() xls_book = xlrd.open_workbook(file_contents=in_stream) sheet = xls_book.sheet_by_index(0) dset.title = sheet.name for i in xrange(sheet.nrows): if (i == 0) and (headers): dset.headers = sheet.row_values(0) else: dset.append(sheet.row_values(i))
def import_book(dbook, in_stream, headers=True): """Returns databook from XLS stream.""" dbook.wipe() xls_book = xlrd.open_workbook(file_contents=in_stream) for sheetname in xls_book.sheet_names(): data = tablib.Dataset() data.title = sheetname sheet = xls_book.sheet_by_name(sheetname) for row in range(sheet.nrows): if (row == 0) and (headers): data.headers = sheet.row_values(row) else: data.append(sheet.row_values(row)) dbook.add_sheet(data)
def create_dataset(self, in_stream): """ Create dataset from first sheet. """ assert XLS_IMPORT xls_book = xlrd.open_workbook(file_contents=in_stream) formatter = lambda(t, v): formatcell(xls_book, t, v, False) dataset = tablib.Dataset() sheet = xls_book.sheets()[0] for i in xrange(sheet.nrows): if i == 0: dataset.headers = sheet.row_values(0) else: (types, values) = (sheet.row_types(i), sheet.row_values(i)) line = map(formatter, zip(types, values)) dataset.append(line) return dataset
def detect(stream): """Returns True if given stream is a readable excel file.""" try: xlrd.open_workbook(file_contents=stream) return True except (TypeError, XLRDError): pass try: xlrd.open_workbook(file_contents=stream.read()) return True except (AttributeError, XLRDError): pass try: xlrd.open_workbook(filename=stream) return True except: return False
def create_dataset(self, in_stream): """ Create dataset from first sheet. """ assert XLS_IMPORT xls_book = xlrd.open_workbook(file_contents=in_stream) dataset = tablib.Dataset() sheet = xls_book.sheets()[0] ncols = sheet.ncols for i in xrange(sheet.nrows): if i == 0: dataset.headers = sheet.row_values(0) else: row_data = [] for j in range(0, ncols): value = sheet.cell_value(i, j) t = sheet.cell_type(i, j) if t == xlrd.XL_CELL_DATE: value = xlrd.xldate_as_tuple(value, 0) value = datetime(*value) row_data.append(value) dataset.append(row_data) return dataset