def write_rows(xf, worksheet, string_table, style_table=None): """Write worksheet data to xml.""" # Ensure a blank cell exists if it has a style for styleCoord in iterkeys(worksheet._styles): if isinstance(styleCoord, str) and COORD_RE.search(styleCoord): worksheet.cell(styleCoord) # create rows of cells cells_by_row = {} for cell in itervalues(worksheet._cells): cells_by_row.setdefault(cell.row, []).append(cell) with xf.element("sheetData"): for row_idx in sorted(cells_by_row): # row meta data row_dimension = worksheet.row_dimensions[row_idx] row_dimension.style = worksheet._styles.get(row_idx) attrs = { 'r': '%d' % row_idx, 'spans': '1:%d' % worksheet.max_column } attrs.update(dict(row_dimension)) with xf.element("row", attrs): row_cells = cells_by_row[row_idx] for cell in sorted(row_cells, key=row_sort): write_cell(xf, worksheet, cell, string_table)
def get_rows_to_write(worksheet): """Return all rows, and any cells that they contain""" # Ensure a blank cell exists if it has a style for styleCoord in iterkeys(worksheet._styles): if isinstance(styleCoord, str) and COORD_RE.search(styleCoord): worksheet.cell(styleCoord) # create rows of cells cells_by_row = {} for cell in itervalues(worksheet._cells): cells_by_row.setdefault(cell.row, []).append(cell) # make sure rows that only have a height set are returned for row_idx in worksheet.row_dimensions: if row_idx not in cells_by_row: cells_by_row[row_idx] = [] return cells_by_row
def write_worksheet_data(doc, worksheet, string_table, style_table): """Write worksheet data to xml.""" start_tag(doc, 'sheetData') max_column = worksheet.get_highest_column() style_id_by_hash = style_table cells_by_row = {} for styleCoord in iterkeys(worksheet._styles): # Ensure a blank cell exists if it has a style if isinstance(styleCoord, str) and COORD_RE.search(styleCoord): worksheet.cell(styleCoord) for cell in worksheet.get_cell_collection(): cells_by_row.setdefault(cell.row, []).append(cell) for row_idx in sorted(cells_by_row): row_dimension = worksheet.row_dimensions[row_idx] attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % max_column} if not row_dimension.visible: attrs['hidden'] = '1' if row_dimension.height > 0: attrs['ht'] = str(row_dimension.height) attrs['customHeight'] = '1' if row_idx in worksheet._styles: attrs['s'] = str(style_table[hash(worksheet.get_style(row_idx))]) attrs['customFormat'] = '1' start_tag(doc, 'row', attrs) row_cells = cells_by_row[row_idx] sorted_cells = sorted(row_cells, key=row_sort) for cell in sorted_cells: value = cell._value coordinate = cell.coordinate attributes = {'r': coordinate} if cell.data_type != cell.TYPE_FORMULA: attributes['t'] = cell.data_type if coordinate in worksheet._styles: attributes['s'] = '%d' % style_id_by_hash[ hash(worksheet._styles[coordinate])] if value in ('', None): tag(doc, 'c', attributes) else: start_tag(doc, 'c', attributes) if cell.data_type == cell.TYPE_STRING: tag(doc, 'v', body='%s' % string_table[value]) elif cell.data_type == cell.TYPE_FORMULA: if coordinate in worksheet.formula_attributes: attr = worksheet.formula_attributes[coordinate] if 't' in attr and attr['t'] == 'shared' and 'ref' not in attr: # Don't write body for shared formula tag(doc, 'f', attr=attr) else: tag(doc, 'f', attr=attr, body='%s' % value[1:]) else: tag(doc, 'f', body='%s' % value[1:]) tag(doc, 'v') elif cell.data_type == cell.TYPE_NUMERIC: if isinstance(value, (long, decimal.Decimal)): func = str else: func = repr tag(doc, 'v', body=func(value)) elif cell.data_type == cell.TYPE_BOOL: tag(doc, 'v', body='%d' % value) else: tag(doc, 'v', body='%s' % value) end_tag(doc, 'c') end_tag(doc, 'row') end_tag(doc, 'sheetData')
def write_worksheet_data(doc, worksheet, string_table, style_table): """Write worksheet data to xml.""" start_tag(doc, 'sheetData') max_column = worksheet.get_highest_column() style_id_by_hash = style_table cells_by_row = {} for styleCoord in iterkeys(worksheet._styles): # Ensure a blank cell exists if it has a style if isinstance(styleCoord, str) and COORD_RE.search(styleCoord): worksheet.cell(styleCoord) for cell in worksheet.get_cell_collection(): cells_by_row.setdefault(cell.row, []).append(cell) for row_idx in sorted(cells_by_row): row_dimension = worksheet.row_dimensions[row_idx] attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % max_column} if not row_dimension.visible: attrs['hidden'] = '1' if row_dimension.height > 0: attrs['ht'] = str(row_dimension.height) attrs['customHeight'] = '1' if row_idx in worksheet._styles: attrs['s'] = str(style_table[hash(worksheet.get_style(row_idx))]) attrs['customFormat'] = '1' start_tag(doc, 'row', attrs) row_cells = cells_by_row[row_idx] sorted_cells = sorted(row_cells, key=row_sort) for cell in sorted_cells: value = cell._value coordinate = cell.coordinate attributes = {'r': coordinate} if cell.data_type != cell.TYPE_FORMULA: attributes['t'] = cell.data_type if coordinate in worksheet._styles: attributes['s'] = '%d' % style_id_by_hash[hash( worksheet._styles[coordinate])] if value in ('', None): tag(doc, 'c', attributes) else: start_tag(doc, 'c', attributes) if cell.data_type == cell.TYPE_STRING: tag(doc, 'v', body='%s' % string_table[value]) elif cell.data_type == cell.TYPE_FORMULA: if coordinate in worksheet.formula_attributes: attr = worksheet.formula_attributes[coordinate] if 't' in attr and attr[ 't'] == 'shared' and 'ref' not in attr: # Don't write body for shared formula tag(doc, 'f', attr=attr) else: tag(doc, 'f', attr=attr, body='%s' % value[1:]) else: tag(doc, 'f', body='%s' % value[1:]) tag(doc, 'v') elif cell.data_type == cell.TYPE_NUMERIC: if isinstance(value, (long, decimal.Decimal)): func = str else: func = repr tag(doc, 'v', body=func(value)) elif cell.data_type == cell.TYPE_BOOL: tag(doc, 'v', body='%d' % value) else: tag(doc, 'v', body='%s' % value) end_tag(doc, 'c') end_tag(doc, 'row') end_tag(doc, 'sheetData')