Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def showable_cell_value(celltype, cellvalue, datemode):
if celltype == xlrd.XL_CELL_DATE:
try:
showval = xlrd.xldate_as_tuple(cellvalue, datemode)
except xlrd.XLDateError:
e1, e2 = sys.exc_info()[:2]
showval = "%s:%s" % (e1.__name__, e2)
elif celltype == xlrd.XL_CELL_ERROR:
showval = xlrd.error_text_from_code.get(
cellvalue, '' % cellvalue)
else:
showval = cellvalue
return showval
import datetime
import os
from io import BytesIO
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%"),
}
cell_contents.second,
cell_contents.microsecond)
else:
# Use the xlrd <= 0.9.2 date handling.
try:
dt = xldate.xldate_as_tuple(cell_contents, epoch1904)
except xldate.XLDateTooLarge:
return cell_contents
if dt[0] < MINYEAR:
cell_contents = time(*dt[3:])
else:
cell_contents = datetime(*dt)
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 suprising
val = int(cell_contents)
if val == cell_contents:
cell_contents = val
return cell_contents
def parse_cell_value(sheet, cell):
if cell.ctype == xlrd.XL_CELL_DATE:
year, month, day, hour, minute, second = xldate_as_tuple(cell.value, sheet.book.datemode)
return ['date', year, month, day, hour, minute, second]
elif cell.ctype == xlrd.XL_CELL_ERROR:
return ['error', error_text_from_code[cell.value]]
elif cell.ctype == xlrd.XL_CELL_BOOLEAN:
return False if cell.value == 0 else True
elif cell.ctype == xlrd.XL_CELL_EMPTY:
return None
return cell.value
:license: GPL v3
"""
import datetime
import xlrd
from xlwt import Workbook, XFStyle
from .._compact import OrderedDict
XLS_FORMAT_CONVERSION = {
xlrd.XL_CELL_TEXT: str,
xlrd.XL_CELL_EMPTY: None,
xlrd.XL_CELL_DATE: datetime.datetime,
xlrd.XL_CELL_NUMBER: float,
xlrd.XL_CELL_BOOLEAN: int,
xlrd.XL_CELL_BLANK: None,
xlrd.XL_CELL_ERROR: None
}
def xldate_to_python_date(value):
"""
convert xl date to python date
"""
date_tuple = xlrd.xldate_as_tuple(value, 0)
ret = None
if date_tuple == (0, 0, 0, 0, 0, 0):
ret = datetime.datetime(1900, 1, 1, 0, 0, 0)
elif date_tuple[0:3] == (0, 0, 0):
ret = datetime.time(date_tuple[3],
date_tuple[4],
date_tuple[5])
elif date_tuple[3:6] == (0, 0, 0):
def showable_cell_value(celltype, cellvalue, datemode):
if celltype == xlrd.XL_CELL_DATE:
try:
showval = xlrd.xldate_as_tuple(cellvalue, datemode)
except xlrd.XLDateError as e:
showval = "%s:%s" % (type(e).__name__, e)
elif celltype == xlrd.XL_CELL_ERROR:
showval = xlrd.error_text_from_code.get(
cellvalue, '' % cellvalue)
else:
showval = cellvalue
return showval
# the last record is written to the file.
# (3) If you write a data record for a cell
# followed by a blank record for the same cell,
# Excel will display a blank but OOo Calc and
# Gnumeric will display the data :-(
return
wtrow = self.wtsheet.row(wtrowx)
if cty == xlrd.XL_CELL_TEXT:
wtrow.set_cell_text(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_NUMBER or cty == xlrd.XL_CELL_DATE:
wtrow.set_cell_number(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_BLANK:
wtrow.set_cell_blank(wtcolx, style)
elif cty == xlrd.XL_CELL_BOOLEAN:
wtrow.set_cell_boolean(wtcolx, cell.value, style)
elif cty == xlrd.XL_CELL_ERROR:
wtrow.set_cell_error(wtcolx, cell.value, style)
else:
raise Exception(
"Unknown xlrd cell type %r with value %r at (sheet=%r,rowx=%r,colx=%r)" \
% (cty, cell.value, self.rdsheet.name, rdrowx, rdcolx)
)
def cell_value(cell, datemode):
"""Get cell value from excel."""
# This should be always returning text format if the excel is generated
# by the get_field_info command
ctype = cell.ctype
value = cell.value
if ctype == xlrd.XL_CELL_ERROR: # pragma: no cover
raise ValueError(repr(cell), 'cell error')
elif ctype == xlrd.XL_CELL_BOOLEAN:
return str(value).upper().strip()
elif ctype == xlrd.XL_CELL_NUMBER:
if value.is_integer():
value = int(value)
return str(value).strip()
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: # pragma: no cover
return datetime.datetime(*value).isoformat()
elif ctype in (xlrd.XL_CELL_TEXT, xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK):
return value.strip()
raise ValueError(repr(cell), 'unknown cell type') # pragma: no cover
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()
ctys = sh.row_types(rowx)
cvals = sh.row_values(rowx)
for colx in colrange:
cty = ctys[colx]
cval = cvals[colx]
if bk.formatting_info:
cxfx = str(sh.cell_xf_index(rowx, colx))
else:
cxfx = ''
if cty == xlrd.XL_CELL_DATE:
try:
showval = xlrd.xldate_as_tuple(cval, dmode)
except xlrd.XLDateError:
e1, e2 = sys.exc_info()[:2]
showval = "%s:%s" % (e1.__name__, e2)
cty = xlrd.XL_CELL_ERROR
elif cty == xlrd.XL_CELL_ERROR:
showval = xlrd.error_text_from_code.get(cval, '' % cval)
else:
showval = cval
result.append((colx, cty, showval, cxfx))
return result