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 write_format(worksheet): attrs = {'defaultRowHeight': '15', 'baseColWidth': '10'} dimensions_outline = [dim.outline_level for dim in itervalues(worksheet.column_dimensions)] if dimensions_outline: outline_level = max(dimensions_outline) if outline_level: attrs['outlineLevelCol'] = str(outline_level) return Element('sheetFormatPr', attrs)
def create_string_table(workbook): """Compile the string table for a workbook.""" strings = set() for sheet in workbook.worksheets: for cell in itervalues(sheet._cells): if cell.data_type == cell.TYPE_STRING and cell._value is not None: strings.add(cell.value) return IndexedList(sorted(strings))
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 test_itervalues(dictionary): from openpyxl.compat import itervalues d = dictionary assert set(itervalues(d)) == set([1, "b", "d"])
def test_itervalues(dictionary): from openpyxl.compat import itervalues d = dictionary assert set(itervalues(d)) == set([1, 'b', 'd'])