Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def set_cell_value(self, x, y, value):
if DataTypes.get_type(value) == DataTypes.DATE:
self.get_cell_style(x, y).format = Format.Format('yyyy-mm-dd')
if x < len(self._dense_cells) and y < len(self._dense_cells[x]):
self._dense_cells[x][y] = value
else:
self._sparse_cells[x][y] = value
self._columns = max(self._columns, y)
def __set_attr(self, method, data):
if self.is_cell:
for merge in self.worksheet.merges:
if self in merge:
method(merge._start[0], merge._start[1], data)
return
method(self.x, self.y, data)
elif self.is_row and isinstance(data, Style.Style):
# Applying a row style
self.worksheet.set_row_style(self.x, data)
elif DataTypes.DataTypes.get_type(data) != DataTypes.DataTypes.ERROR:
# Attempt to apply in batch
for cell in self:
cell.__set_attr(method, data)
else:
if len(data) <= self.height:
for row in data:
if len(row) > self.width:
raise Exception(
"Row too large for range, row has %s columns, but range only has %s"
% (len(row), self.width))
for x, row in enumerate(data):
for y, value in enumerate(row):
method(x + self._start[0], y + self._start[1], value)
else:
raise Exception(
"Too many rows for range, data has %s rows, but range only has %s"
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
elif isinstance(value, bool):
return DataTypes.BOOLEAN
elif isinstance(value, DataTypes._numberTypes):
return DataTypes.NUMBER
elif HAS_NUMPY and isinstance(
value,
(np.floating, np.integer, np.complexfloating, np.unsignedinteger)):
return DataTypes.NUMBER
elif isinstance(value, (datetime, date, time)):
return DataTypes.DATE
else:
return DataTypes.ERROR
elif value.__class__ in DataTypes._numberTypes:
return DataTypes.NUMBER
# fall back to the slower isinstance
elif isinstance(value, six.string_types):
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
elif isinstance(value, bool):
return DataTypes.BOOLEAN
elif isinstance(value, DataTypes._numberTypes):
return DataTypes.NUMBER
elif HAS_NUMPY and isinstance(
value,
(np.floating, np.integer, np.complexfloating, np.unsignedinteger)):
return DataTypes.NUMBER
elif isinstance(value, (datetime, date, time)):
return DataTypes.DATE
else:
return DataTypes.ERROR
def get_type(value):
# Using value.__class__ over isinstance for speed
if value.__class__ in six.string_types:
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
# not using in (int, float, long, complex) for speed
elif value.__class__ == bool:
return DataTypes.BOOLEAN
elif value.__class__ in DataTypes._numberTypes:
return DataTypes.NUMBER
# fall back to the slower isinstance
elif isinstance(value, six.string_types):
if len(value) > 0 and value[0] == '=':
return DataTypes.FORMULA
else:
return DataTypes.INLINE_STRING
elif isinstance(value, bool):
return DataTypes.BOOLEAN
elif isinstance(value, DataTypes._numberTypes):
return DataTypes.NUMBER
elif HAS_NUMPY and isinstance(
value,
(np.floating, np.integer, np.complexfloating, np.unsignedinteger)):
return DataTypes.NUMBER
elif isinstance(value, (datetime, date, time)):
def __get_cell_data(self, cell, x, y, style):
if cell is None:
return "" # no cell data
# boolean values are treated oddly in dictionaries, manually override
type = DataTypes.get_type(cell)
if type == DataTypes.NUMBER:
if math.isnan(cell):
z = '" t="e">#NUM!'
elif math.isinf(cell):
z = '" t="e">#DIV/0!'
else:
z = '">%.15g' % (cell)
elif type == DataTypes.INLINE_STRING:
z = '" t="inlineStr">%s' % escape(
to_unicode(cell))
elif type == DataTypes.DATE:
z = '">%s' % (DataTypes.to_excel_date(cell))
elif type == DataTypes.FORMULA:
z = '">%s' % (cell[1:]) # Remove equals sign.
elif type == DataTypes.BOOLEAN:
z = '" t="b">%d' % (cell)
if style:
return "
def to_excel_date(d):
if isinstance(d, datetime):
if d.tzinfo is not None:
warnings.warn(
'Excel does not support timestamps with time zone information. Time zones will be ignored.'
)
delta = d.replace(tzinfo=None) - DataTypes.EXCEL_BASE_DATE
excel_date = delta.days + (
float(delta.seconds) + float(delta.microseconds) / 1E6) / (
60 * 60 * 24) + 1
return excel_date + (excel_date > 59)
elif isinstance(d, date):
# this is why python sucks >.<
return DataTypes.to_excel_date(datetime(*(d.timetuple()[:6])))
elif isinstance(d, time):
return DataTypes.to_excel_date(
datetime(
*(DataTypes.EXCEL_BASE_DATE.timetuple()[:3]),
hour=d.hour,
minute=d.minute,
second=d.second,
microsecond=d.microsecond)) - 1
def __get_cell_data(self, cell, x, y, style):
if cell is None:
return "" # no cell data
# boolean values are treated oddly in dictionaries, manually override
type = DataTypes.get_type(cell)
if type == DataTypes.NUMBER:
if math.isnan(cell):
z = '" t="e">#NUM!'
elif math.isinf(cell):
z = '" t="e">#DIV/0!'
else:
z = '">%.15g' % (cell)
elif type == DataTypes.INLINE_STRING:
z = '" t="inlineStr">%s' % escape(
to_unicode(cell))
elif type == DataTypes.DATE:
z = '">%s' % (DataTypes.to_excel_date(cell))
elif type == DataTypes.FORMULA:
z = '">%s' % (cell[1:]) # Remove equals sign.
elif type == DataTypes.BOOLEAN:
z = '" t="b">%d' % (cell)
def to_excel_date(d):
if isinstance(d, datetime):
if d.tzinfo is not None:
warnings.warn(
'Excel does not support timestamps with time zone information. Time zones will be ignored.'
)
delta = d.replace(tzinfo=None) - DataTypes.EXCEL_BASE_DATE
excel_date = delta.days + (
float(delta.seconds) + float(delta.microseconds) / 1E6) / (
60 * 60 * 24) + 1
return excel_date + (excel_date > 59)
elif isinstance(d, date):
# this is why python sucks >.<
return DataTypes.to_excel_date(datetime(*(d.timetuple()[:6])))
elif isinstance(d, time):
return DataTypes.to_excel_date(
datetime(
*(DataTypes.EXCEL_BASE_DATE.timetuple()[:3]),
hour=d.hour,
minute=d.minute,
second=d.second,
microsecond=d.microsecond)) - 1
if type == DataTypes.NUMBER:
if math.isnan(cell):
z = '" t="e">#NUM!'
elif math.isinf(cell):
z = '" t="e">#DIV/0!'
else:
z = '">%.15g' % (cell)
elif type == DataTypes.INLINE_STRING:
z = '" t="inlineStr">%s' % escape(
to_unicode(cell))
elif type == DataTypes.DATE:
z = '">%s' % (DataTypes.to_excel_date(cell))
elif type == DataTypes.FORMULA:
z = '">%s' % (cell[1:]) # Remove equals sign.
elif type == DataTypes.BOOLEAN:
z = '" t="b">%d' % (cell)
if style:
return "