Named styles are lost on on read

Issue #381 resolved
Bors LTD created an issue


Just saving a file with a style in a cell makes it lose the style in the output file.

Here is the test program I used:

import openpyxl

wb = openpyxl.load_workbook("test.xlsx")
#ws =
#style = ws.cell("A1").style
#ws.cell("B1").style = style
#ws.cell("B1").value = u"Output""test_output.xlsx")

Looking at the print of the style object, it does not seem to have read the 12pt font or the green background color in the first place:

Style(font=Font(name='Calibri', sz=11.0, b=False, i=False, u=None, strike=False, color=Color(indexed=Value must be type 'int', auto=Value must be type 'bool', theme=Value must be type 'int'), vertAlign=None, charset=None, outline=False, shadow=False, condense=False, extend=False, family=2.0), fill=PatternFill(patternType=None, fgColor=Color(rgb='00000000', indexed=Value must be type 'int', auto=Value must be type 'bool', theme=Value must be type 'int', tint=0.0, type='rgb'), bgColor=Color(rgb='00000000', indexed=Value must be type 'int', auto=Value must be type 'bool', theme=Value must be type 'int', tint=0.0, type='rgb')), border=Border(left=Side(style=None, color=None), right=Side(style=None, color=None), top=Side(style=None, color=None), bottom=Side(style=None, color=None), diagonal=Side(style=None, color=None), diagonal_direction=None, vertical=None, horizontal=None), alignment=Alignment(horizontal='general', vertical='bottom', textRotation=0, wrapText=False, shrinkToFit=False, indent=0.0, relativeIndent=0.0, justifyLastLine=False, readingOrder=0.0), number_format='General', protection=)

The goal is to have the cell on the right (and potentially more) having the same style as the original cell.

I attach the input and output XLSX files.

The input file was produced on Mac Excel 2010. Sorry I don't have any Windows Excel to test it on.

Testing on openpyxl 2.1.2.

Comments (11)

  1. CharlieC

    It looks like your using a named style. These are currently not supported at all. I'm hoping to add support for them in 2.2 but it requires some reworking of how styles work first.

  2. Bors LTD reporter

    Ho, nice! Indeed I used one of Excel's buil-in styles.

    When I manually set the font and background, the style is preserved and copied to cell B1.

    Thanks, this bug is basically solved for me. Feel free to close it if you don't need a ticket on named styles.

  3. Bors LTD reporter

    Yes indeed, I can even use the commented code in my example to copy style from one cell to another.

    Thank you!

  4. CharlieC

    Good to know it's working for you. Support is currently only partial – any named styles that are around will be preserved but we're currently not reliably linking individual cell styles to named ones. The goal in the future is to have style definitions that are explicitly shared by lots of cells with only the occasional need for individual cell styles. This should make working with styles a lot easier and faster: we'll just pass around a reference to a named style instead of creating all the relevant objects and seeing if they don't exist already.

  5. Log in to comment