Style cells and ranges
devStyling cells introduces significant overhead and can increase execution time up to 10x.
Methods of Styling
set_cell_style(row, col, Style(...)): The fastest method. Note that subsequent calls toset_cell_styleon the same cell override previous styles rather than merging them. To combine styles, pass all properties into a singleStyleobject.ws.get_cell_style(row, col).property = value: A faster way to modify existing styles.ws[row][col].style.property = value: The most intuitive but slowest method.
Styling Ranges, Rows, and Columns
- Ranges:
ws.range("A1", "C3").style.font.bold = True - Rows:
ws.set_row_style(row_index, Style(...))orws[row_index].style.property = value - Columns:
ws.set_col_style(col_index, Style(...))
from pyexcelerate import Workbook, Color, Style, Font, Fill, Format
wb = Workbook()
ws = wb.new_sheet("sheet name")
# Combined style to avoid overrides
ws.set_cell_style(1, 1, Style(font=Font(bold=True), format=Format('mm/dd/yy')))
wb.save("output.xlsx")