from System.Drawing import Color def CellRangeFromDimensions(startCell, width, height): """Given a start cell and a width and height, return a cell range.""" endCell = startCell.Offset(width-1, height-1) return CellRange(startCell, endCell) def FormatCellRangeBorders(cellRange): """Given a cell range, format it with borders.""" cellRange.BorderRight = True cellRange.BorderTop = True cellRange.Cols[1].BorderLeft = True cellRange.Rows[cellRange.MaxRow].BorderBottom = True def ColorAlternateRows(cellRange, mainColor=Color.CornflowerBlue, alternateColor=None): """ Given a cell range format it with a BackColor on alternate rows. By default it sets a BackColor of "CornflowerBlue" on every other row, and the other rows have no BackColor set. You can change this by passing in different colours as the "mainColor" and "alternateColor" arguments. """ for i in range(1, cellRange.MaxRow + 1): thisRow = cellRange.Rows[i] if cellRange.HeaderRow is not None and cellRange.HeaderRow.Top == thisRow.Top: # Skip header row continue if i % 2 and alternateColor is not None: thisRow.BackColor = alternateColor elif not i % 2 and mainColor is not None: thisRow.BackColor = mainColor