Example #1
0
 def _cast_datetime(self, value):
     """Convert Python datetime to Excel and set formatting"""
     if isinstance(value, datetime.date):
         value = to_excel(value, self.base_date)
         self.number_format = numbers.FORMAT_DATE_YYYYMMDD2
     elif isinstance(value, datetime.time):
         value = time_to_days(value)
         self.number_format = numbers.FORMAT_DATE_TIME6
     elif isinstance(value, datetime.timedelta):
         value = timedelta_to_days(value)
         self.number_format = numbers.FORMAT_DATE_TIMEDELTA
     return value
Example #2
0
 def _cast_datetime(self, value):
     """Convert Python datetime to Excel and set formatting"""
     if isinstance(value, datetime.date):
         value = to_excel(value, self.base_date)
         self.number_format = numbers.FORMAT_DATE_YYYYMMDD2
     elif isinstance(value, datetime.time):
         value = time_to_days(value)
         self.number_format = numbers.FORMAT_DATE_TIME6
     elif isinstance(value, datetime.timedelta):
         value = timedelta_to_days(value)
         self.number_format = numbers.FORMAT_DATE_TIMEDELTA
     return value
Example #3
0
 def _cast_datetime(self, value):
     if isinstance(value, datetime.date):
         value = to_excel(value, self.base_date)
         self.number_format = NumberFormat.FORMAT_DATE_YYYYMMDD2
     elif isinstance(value, datetime.time):
         value = time_to_days(value)
         self.number_format = NumberFormat.FORMAT_DATE_TIME6
     elif isinstance(value, datetime.timedelta):
         value = timedelta_to_days(value)
         self.number_format = NumberFormat.FORMAT_DATE_TIMEDELTA
     self.set_explicit_value(value, self.TYPE_NUMERIC)
     return True
Example #4
0
 def _cast_datetime(self, value):
     if isinstance(value, datetime.date):
         value = to_excel(value, self.base_date)
         self.number_format = NumberFormat.FORMAT_DATE_YYYYMMDD2
     elif isinstance(value, datetime.time):
         value = time_to_days(value)
         self.number_format = NumberFormat.FORMAT_DATE_TIME6
     elif isinstance(value, datetime.timedelta):
         value = timedelta_to_days(value)
         self.number_format = NumberFormat.FORMAT_DATE_TIMEDELTA
     self.set_explicit_value(value, self.TYPE_NUMERIC)
     return True
    def append(self, row):
        """
        :param row: iterable containing values to append
        :type row: iterable
        """
        doc = self._get_content_generator()
        self._max_row += 1
        span = len(row)
        self._max_col = max(self._max_col, span)
        row_idx = self._max_row
        attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % span}
        start_tag(doc, 'row', attrs)

        for col_idx, cell in enumerate(row):
            style = None
            comment = None
            if cell is None:
                continue
            elif isinstance(cell, dict):
                dct = cell
                cell = dct.get('value')
                if cell is None:
                    continue
                style = dct.get('style')
                comment = dct.get('comment')
                for ob, attr, cls in ((style, 'style', Style),
                                      (comment, 'comment', Comment)):
                    if ob is not None and not isinstance(ob, cls):
                        raise TypeError('%s should be a %s not a %s' %
                                        (attr, cls.__class__.__name__,
                                         ob.__class__.__name__))

            column = get_column_letter(col_idx + 1)
            coordinate = '%s%d' % (column, row_idx)
            attributes = {'r': coordinate}
            if comment is not None:
                comment._parent = CommentParentCell(coordinate, row_idx,
                                                    column)
                self._comments.append(comment)
                self._comment_count += 1

            if isinstance(cell, bool):
                dtype = 'boolean'
            elif isinstance(cell, NUMERIC_TYPES):
                dtype = 'numeric'
            elif isinstance(cell, TIME_TYPES):
                dtype = 'datetime'
                if isinstance(cell, datetime.date):
                    cell = to_excel(cell)
                elif isinstance(cell, datetime.time):
                    cell = time_to_days(cell)
                elif isinstance(cell, datetime.timedelta):
                    cell = timedelta_to_days(cell)
                if style is None:
                    # allow user-defined style if needed
                    style = STYLES[dtype]['style']
            elif cell and cell[0] == '=':
                dtype = 'formula'
            else:
                dtype = 'string'
                cell = self._strings.add(unicode(cell))

            if style is not None:
                attributes['s'] = '%d' % self._styles.add(style)

            if dtype != 'formula':
                attributes['t'] = STYLES[dtype]['type']
            start_tag(doc, 'c', attributes)

            if dtype == 'formula':
                tag(doc, 'f', body='%s' % cell[1:])
                tag(doc, 'v')
            elif dtype == 'boolean':
                tag(doc, 'v', body='%d' % cell)
            else:
                tag(doc, 'v', body='%s' % cell)
            end_tag(doc, 'c')
        end_tag(doc, 'row')