Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def type_value(ctype, value):
""" Detects boolean value, int value, datetime """
# Boolean
if ctype == xlrd.XL_CELL_BOOLEAN:
return bool(value)
# Excel numbers are only float
# Float with no decimals can be cast into int
if ctype == xlrd.XL_CELL_NUMBER and value == value // 1:
return int(value)
# Datetime
if ctype == xlrd.XL_CELL_DATE:
return xlrd.xldate.xldate_as_datetime(value, self.__book.datemode)
return value
def __row(self, rowx):
for colx in self.cols:
value = self.sheet.cell_value(rowx, colx)
if self.sheet.cell_type(rowx, colx) == XL_CELL_DATE:
date_parts = xldate_as_tuple(value, self.book.datemode)
# Times come out with a year of 0.
if date_parts[0]:
value = datetime(*date_parts)
else:
value = time(*date_parts[3:])
yield value
def _parse_cell(cell_contents, cell_typ):
"""converts the contents of the cell into a pandas
appropriate object"""
if cell_typ == XL_CELL_DATE:
# Use the newer xlrd datetime handling.
try:
cell_contents = xldate.xldate_as_datetime(
cell_contents, epoch1904)
except OverflowError:
return cell_contents
# Excel doesn't distinguish between dates and time,
# so we treat dates on the epoch as times only.
# Also, Excel supports 1900 and 1904 epochs.
year = (cell_contents.timetuple())[0:3]
if ((not epoch1904 and year == (1899, 12, 31)) or
(epoch1904 and year == (1904, 1, 1))):
cell_contents = time(cell_contents.hour,
cell_contents.minute,
def get_value(self, cell):
if cell.ctype == xlrd.XL_CELL_DATE:
time, date = math.modf(cell.value)
tpl = xlrd.xldate_as_tuple(cell.value, self.workbook.datemode)
if date and time:
return datetime.datetime(*tpl)
elif date:
return datetime.date(*tpl[0:3])
else:
return datetime.time(*tpl[3:6])
return cell.value
str: Formatted cell value
"""
if cell.ctype == TYPE_BOOLEAN:
if cell.value:
return 'yes'
else:
return 'no'
if cell.ctype == TYPE_ERROR:
return ''
if cell.ctype == TYPE_EMPTY:
return ''
if cell.ctype == TYPE_DATE:
dt = xldate_as_datetime(cell.value, self.datemode)
return dt.strftime(DATE_FORMAT)
return cell.value
"""
Helper method to decode the cell value by type
@param t: the cell type
@param v: the cell value
@return: text representation of the cell value
"""
text = ""
if v:
if t is None:
text = s3_unicode(v).strip()
elif t == xlrd.XL_CELL_TEXT:
text = v.strip()
elif t == xlrd.XL_CELL_NUMBER:
text = str(long(v)) if long(v) == v else str(v)
elif t == xlrd.XL_CELL_DATE:
# Convert into an ISO datetime string
text = s3_encode_iso_datetime(decode_date(v))
elif t == xlrd.XL_CELL_BOOLEAN:
text = str(v).lower()
return text
def worksheet_from_excel(excel_sheet):
worksheet = Worksheet()
for col in range(excel_sheet.ncols):
for row in range(excel_sheet.nrows):
cell = excel_sheet.cell(row, col)
if cell.ctype == XL_CELL_ERROR:
formula = '=%s' % (error_text_from_code[cell.value], )
elif cell.ctype == XL_CELL_DATE:
formula = '=DateTime(%s, %s, %s, %s, %s, %s)' % xldate_as_tuple(
cell.value, excel_sheet.book.datemode)
else:
formula = unicode(excel_sheet.cell(row, col).value)
worksheet[col + 1, row + 1].formula = formula
return worksheet
def get_cell_value(cls, workbook, tz, cell):
if cell.ctype == XL_CELL_DATE:
date = xldate_as_tuple(cell.value, workbook.datemode)
return datetime.datetime(*date, tzinfo=tz)
else:
return cls.normalize_value(str(cell.value))
def cell_value(self, row, column):
"""
Random access to the xls cells
"""
if self._keywords.get("skip_hidden_row_and_column") is True:
row, column = self._offset_hidden_indices(row, column)
cell_type = self._native_sheet.cell_type(row, column)
value = self._native_sheet.cell_value(row, column)
if cell_type == xlrd.XL_CELL_DATE:
value = xldate_to_python_date(value, self._book_date_mode)
elif cell_type == xlrd.XL_CELL_NUMBER and self.__auto_detect_int:
if has_no_digits_in_float(value):
value = int(value)
elif cell_type == xlrd.XL_CELL_ERROR:
value = DEFAULT_ERROR_VALUE
if self.__merged_cells:
merged_cell = self.__merged_cells.get("%s-%s" % (row, column))
if merged_cell:
if merged_cell.value:
value = merged_cell.value
else:
merged_cell.value = value
return value
def cell_to_python(self, cell, book):
# annoying need book argument for datemode
# info on types: http://www.lexicon.net/sjmachin/xlrd.html#xlrd.Cell-class
if cell.ctype == xlrd.XL_CELL_NUMBER:
return float(cell.value)
elif cell.ctype == xlrd.XL_CELL_DATE:
from datetime import date
# TODO: distinguish date and datetime
args = xlrd.xldate_as_tuple(cell.value, book.datemode)
try:
return date(args[0], args[1], args[2])
except Exception, inst:
print 'Error parsing excel date (%s): %s' % (args, inst)
return None
elif cell.ctype == xlrd.XL_CELL_BOOLEAN:
return bool(cell.value)
else:
return cell.value