def load_cells(self, macrosheet, xls_sheet): cells = {} try: self._excel.Application.ScreenUpdating = False col_offset = xls_sheet.UsedRange.Column row_offset = xls_sheet.UsedRange.Row formulas = xls_sheet.UsedRange.Formula if formulas is not None: for row_no, row in enumerate(formulas): for col_no, col in enumerate(row): if col: cell = Cell() cell.sheet = macrosheet if len(col) > 1 and col.startswith('='): cell.formula = col else: cell.value = col row_addr = row_offset + row_no col_addr = col_offset + col_no cell.row = row_addr cell.column = Cell.convert_to_column_name(col_addr) cells[(col_addr, row_addr)] = cell self._excel.Application.ScreenUpdating = True except pywintypes.com_error as error: print('CELL(Formula): ' + str(error.args[2])) try: values = xls_sheet.UsedRange.Value if values is not None: for row_no, row in enumerate(values): for col_no, col in enumerate(row): if col: row_addr = row_offset + row_no col_addr = col_offset + col_no if (col_addr, row_addr) in cells: cell = cells[(col_addr, row_addr)] cell.value = col else: cell = Cell() cell.sheet = macrosheet cell.value = col cell.row = row_addr cell.column = Cell.convert_to_column_name( col_addr) cells[(col_addr, row_addr)] = cell except pywintypes.com_error as error: print('CELL(Constant): ' + str(error.args[2])) for cell in cells: macrosheet.add_cell(cells[cell])
def load_cells(self, macrosheet, xls_sheet): try: for xls_cell in xls_sheet.get_used_cells(): cell = Cell() cell.sheet = macrosheet if xls_cell.formula is not None and len(xls_cell.formula) > 0: cell.formula = '=' + xls_cell.formula cell.value = xls_cell.value cell.row = xls_cell.row + 1 cell.column = Cell.convert_to_column_name(xls_cell.column + 1) if cell.value is not None or cell.formula is not None: macrosheet.add_cell(cell) except Exception as error: print('CELL(Formula): ' + str(error.args[2]))
def xlref(self, row, column, zero_indexed=True): if zero_indexed: row += 1 column += 1 return '$' + Cell.convert_to_column_name(column) + '$' + str(row)