Copying cell style and applying to other cell not copying background color correctly

Issue #1263 invalid
Vitor Diniz created an issue

I am trying to copy the whole style from a cell in a worksheet and apply to a cell in another worksheet. It works for a few colors but for others when i apply it gets gray. I believe it is working only for 'standard colors' (I don't know if openpyxl treats them in a special way somehow). I am using version 2.5.12.

In the images you can see the color in the worksheet with a few cells (the one which i copy the styles) and the result in the body cells in the other worksheet. I believe it is working only with the colors defined as 'standard'.

I am copying the cell style with the code bellow:

style_dict = {}
if cell.has_style:
    style_dict['font'] = copy.copy(cell.font)
    style_dict['border'] = copy.copy(cell.border)
    style_dict['fill'] = copy.copy(cell.fill)
    style_dict['number_format'] = copy.copy(cell.number_format)
    style_dict['protection'] = copy.copy(cell.protection)
    style_dict['alignment'] = copy.copy(cell.alignment)
return style_dict

And then apply with this one:

def apply_style(cell, style_dict):
    """
    Apply styles defined in style_dict in cell or keep the current style if it's not defined in

style_dict :param cell: Cell that will be styled :param style_dict: Dict with settings about the style :return: """ cell.font = style_dict.get('font') or cell.font cell.border = style_dict.get('border') or cell.border cell.fill = style_dict.get('fill') or cell.fill cell.number_format = style_dict.get('number_format') or cell.number_format cell.protection = style_dict.get('protection') or cell.protection cell.alignment = style_dict.get('alignment') or cell.alignment

Thanks in advance

Comments (5)

  1. Log in to comment