Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_number_cell(self):
cell = sheet.cell(1, 1)
self.assertEqual(cell.ctype, xlrd.XL_CELL_NUMBER)
self.assertEqual(cell.value, 100)
self.assertTrue(cell.has_xf)
# 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,
cell_contents.second,
cell_contents.microsecond)
elif cell_typ == XL_CELL_ERROR:
cell_contents = np.nan
elif cell_typ == XL_CELL_BOOLEAN:
cell_contents = bool(cell_contents)
elif convert_float and cell_typ == XL_CELL_NUMBER:
# GH5394 - Excel 'numbers' are always floats
# it's a minimal perf hit and less surprising
val = int(cell_contents)
if val == cell_contents:
cell_contents = val
return cell_contents
def _make_cell_value(self, cell):
if cell.ctype == xlrd.XL_CELL_NUMBER:
# cell.value is a float, return int if it's an int
if int(cell.value) == cell.value:
return int(cell.value)
else:
return cell.value
elif cell.ctype == xlrd.XL_CELL_DATE:
datetime_tuple = xlrd.xldate_as_tuple(cell.value, self._sheet.book.datemode)
if datetime_tuple[:3] == (0, 0, 0):
return time(*datetime_tuple[3:])
elif datetime_tuple[3:] == (0, 0, 0):
return date(*datetime_tuple[:3])
else:
return datetime(*datetime_tuple)
elif cell.ctype == xlrd.XL_CELL_BOOLEAN:
return bool(cell.value)
elif cell.ctype == xlrd.XL_CELL_EMPTY:
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
return None, str(e)
except UnicodeDecodeError:
return None, 'The encoding of the file is unknown.'
if book.nsheets == 0:
return None, 'The uploaded file contains no sheets.'
sheet = book.sheet_by_index(0)
table = []
for row in xrange(sheet.nrows):
table_row = []
for col in xrange(sheet.ncols):
value = None
cell_value = sheet.cell_value(row, col)
cell_type = sheet.cell_type(row, col)
if cell_type == xlrd.XL_CELL_TEXT:
value = cell_value
elif cell_type == xlrd.XL_CELL_NUMBER:
value = str(int(cell_value))
elif cell_type == xlrd.XL_CELL_BOOLEAN:
value = 'true' if cell_value else 'false'
elif cell_type == xlrd.XL_CELL_DATE:
# TODO(ryok): support date type.
pass
table_row.append(value)
table.append(table_row)
csv_output = StringIO.StringIO()
csv_writer = csv.writer(csv_output)
csv_writer.writerows(table)
return csv_output.getvalue(), None
def f(cell):
# Can format differently?
if cell.ctype == xlrd.XL_CELL_BOOLEAN:
return 'TRUE' if cell.value == 1 else 'FALSE'
elif cell.ctype == xlrd.XL_CELL_EMPTY:
return ''
elif cell.ctype == xlrd.XL_CELL_TEXT:
s = cell.value.strip()
s = newline_space_fix(s)
return s
elif cell.ctype == xlrd.XL_CELL_NUMBER:
return cell.value
else:
m = 'Bad cell type: {}. May be DATE'.format(cell.ctype)
raise QlangError(m)
return cell.value
def decode(t, v):
"""
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 cell_value(cell, datemode):
""" Convert to a utf-8 encoded string value (csv is not unicode aware)
"""
ctype = cell.ctype
value = cell.value
if ctype == xlrd.XL_CELL_ERROR:
raise ValueError(repr(cell), 'cell error')
elif ctype == xlrd.XL_CELL_BOOLEAN:
return str(value).upper()
elif ctype == xlrd.XL_CELL_NUMBER:
if value.is_integer():
value = int(value)
return str(value)
elif ctype == xlrd.XL_CELL_DATE:
value = xlrd.xldate_as_tuple(value, datemode)
if value[3:] == (0, 0, 0):
return datetime.date(*value[:3]).isoformat()
else:
return datetime.datetime(*value).isoformat()
elif ctype in (xlrd.XL_CELL_TEXT, xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK):
return value.encode('utf-8')
raise ValueError(repr(cell), 'unknown cell type')
def _cell_value(self, cell):
"""Convert Excel cell into value of a python type
(from Swiss XlsReader.cell_to_python)"""
# 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:
# TODO: distinguish date and datetime
args = xlrd.xldate_as_tuple(cell.value, self.workbook.datemode)
try:
return datetime.date(args[0], args[1], args[2]).strftime("%d/%m/%Y")
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
import xlrd
import xlwt
import rows.fields as fields
from rows.plugins.utils import create_table, prepare_to_export
from rows.utils import Source
CELL_TYPES = {
xlrd.XL_CELL_BLANK: fields.TextField,
xlrd.XL_CELL_DATE: fields.DatetimeField,
xlrd.XL_CELL_ERROR: None,
xlrd.XL_CELL_TEXT: fields.TextField,
xlrd.XL_CELL_BOOLEAN: fields.BoolField,
xlrd.XL_CELL_EMPTY: None,
xlrd.XL_CELL_NUMBER: fields.FloatField,
}
# TODO: add more formatting styles for other types such as currency
# TODO: styles may be influenced by locale
FORMATTING_STYLES = {
fields.DateField: xlwt.easyxf(num_format_str="yyyy-mm-dd"),
fields.DatetimeField: xlwt.easyxf(num_format_str="yyyy-mm-dd hh:mm:ss"),
fields.PercentField: xlwt.easyxf(num_format_str="0.00%"),
}
def _python_to_xls(field_types):
def convert_value(field_type, value):
data = {}
if field_type in FORMATTING_STYLES: