How to use the xlrd.XL_CELL_BOOLEAN function in xlrd

To help you get started, we’ve selected a few xlrd examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github frictionlessdata / tabulator-py / tabulator / parsers / xls.py View on Github external
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
github pyexcel / pyexcel / pyexcel / io / xlbook.py View on Github external
:copyright: (c) 2014 by C. W.
    :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],
github kbase / kb_sdk / lib / biokbase / common / filetools.py View on Github external
'''Converts an xlrd excel worksheet row into a standard python format.
    Empty or blank cells -> None
    Number or text -> float or string
    Boolean -> True or False
    Date -> date string as rendered by datetime.datetime
    Raises ValueError if a cell has an error.'''
    xlrdsheet = xlrdbook.sheet_by_name(xlrdsheetname)
    ret = []
    for i, cell in enumerate(xlrdsheet.row(rownum)):
        if (cell.ctype == _xlrd.XL_CELL_EMPTY or
            cell.ctype == _xlrd.XL_CELL_BLANK):
            ret.append(None)
        elif (cell.ctype == _xlrd.XL_CELL_NUMBER or
              cell.ctype == _xlrd.XL_CELL_TEXT):
            ret.append(cell.value)
        elif cell.ctype == _xlrd.XL_CELL_BOOLEAN:
            ret.append(bool(cell.value))
        elif cell.ctype == _xlrd.XL_CELL_DATE:
            dt = _xlrd.xldate_as_tuple(cell.value, xlrdbook.datemode)
            d = str(_datetime.datetime(*dt))
            ret.append(d)
        elif cell.ctype == _xlrd.XL_CELL_ERROR:
            raise ValueError(
                ' '.join(['Cell', _xlrd.cellname(rownum, i), 'in sheet',
                xlrdsheet.name, 'has an error']))
        else:
            raise ValueError('Unknown cell type')  # error in xlrd
    return ret
github extrabacon / pyspreadsheet / python / excel_reader.py View on Github external
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
github sahana / eden / modules / s3 / s3xml.py View on Github external
                    @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
github PMA-2020 / ppp / pmix / qlang.py View on Github external
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
github ENCODE-DCC / encoded / src / encoded / xlreader.py View on Github external
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')
github turicas / rows / rows / plugins / xls.py View on Github external
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%"),
}


def _python_to_xls(field_types):
    def convert_value(field_type, value):
github XLSForm / pyxform / pyxform / xls2json_backends.py View on Github external
def xls_value_to_unicode(value, value_type, datemode):
    """
    Take a xls formatted value and try to make a unicode string
    representation.
    """
    if value_type == xlrd.XL_CELL_BOOLEAN:
        return "TRUE" if value else "FALSE"
    elif value_type == xlrd.XL_CELL_NUMBER:
        # Try to display as an int if possible.
        int_value = int(value)
        if int_value == value:
            return unicode(int_value)
        else:
            return unicode(value)
    elif value_type is xlrd.XL_CELL_DATE:
        # Warn that it is better to single quote as a string.
        # error_location = cellFormatString % (ss_row_idx, ss_col_idx)
        # raise Exception(
        #   "Cannot handle excel formatted date at " + error_location)
        datetime_or_time_only = xlrd.xldate_as_tuple(value, datemode)
        if datetime_or_time_only[:3] == (0, 0, 0):
            # must be time only
github PMA-2020 / ppp / pmix / cell.py View on Github external
def cell_value(cell, datemode=None, stripstr=True):
        """Get python object out of xlrd.Cell value.

        Args:
            cell (xlrd.Cell): The cell
            datemode (int): The date mode for the workbook
            stripstr (bool): Remove trailing / leading whitespace from text?

        Returns:
            The python object represented by this cell.
        """
        value = None
        if cell.ctype == xlrd.XL_CELL_BOOLEAN:
            value = True if cell.value == 1 else False
        elif cell.ctype == xlrd.XL_CELL_EMPTY:
            # value = None  # ... redundant
            pass
        elif cell.ctype == xlrd.XL_CELL_TEXT:
            if stripstr:
                value = cell.value.strip()
            else:
                value = cell.value
        elif cell.ctype == xlrd.XL_CELL_NUMBER:
            # Make integer what is equal to an integer
            int_val = int(cell.value)
            value = int_val if int_val == cell.value else cell.value
        elif cell.ctype == xlrd.XL_CELL_DATE:
            value = Cell.parse_datetime(cell.value, datemode)
        elif cell.ctype == xlrd.XL_CELL_ERROR: