Column / Row styles not applied to existing cells in 1.9

Issue #259 closed
Adam Morris
created an issue

1.9 supports setting style on a column / row, but it doesn't work at the moment for existing cells because excel assigns the column style to both the cell and the column, and openpyxl doesn't (yet).

from openpyxl import Workbook
from openpyxl.styles import Style, Border
wb = Workbook()
ws =
ws['A1'].value = 'Cell A1'
ws['A2'].value = 'Cell A2'
ws['B1'].value = 'Cell B1'

default_style = Style(static=True)
default_style.borders.left.border_style = Border.BORDER_THIN

# This works for blank cells, but not for existing cells:
ws._styles['A'] = default_style  # Set style for Column A
ws._styles[1] = default_style  # Set style for Row 1

# Maybe we can add something like:
ws.setStyle('A', default_style)   # Set for both column and existing cells

In this case a function like ws.setStyle could set the style to the column/row, and to all the cells that were in a column/row.

The only issue here, is that when adding a new cell:

  1. If the style is modified, it should copy an existing column/row style, but this could be an expensive if added to worksheet.get_style(), so I suggest we ignore this.

  2. When writing styles, if a cell style is not set, but a column / row style is, it should be written. This could happen by adding a column style and then adding a cell. This is less expensive.

Comments (6)

  1. Eric Gazoni

    To me, now we have immutable styles, it shouldn't be complicated at all. But yes, this would imply creating empty cells (that we now support well in 1.9) with just some styling (which is what happens in Excel anyway from my experience. This comes with the cost of some spilled XML and thus a workbook size increase but nothing extravagant.

  2. CharlieC

    @Adam Morris this is actually possible using the ColumnDimension and RowDimension objects which have their own style attributes. I'm not sure quite how much we really support (we don't check them when looking at the format of cells) but they are read and should be writeable.

  3. Log in to comment