duplicate properties of workbook?

Issue #816 resolved
semigod
created an issue

It seems property named_styles and property style_names are duplicate as they both returns names of named styles. Meanwhile, there's no way to access named styles to inspect details of style settings.For example, I may want to create a copy of an existing named style and do some modifications to the clone.

Comments (9)

  1. CharlieC

    Is this a question then please submit it to the mailing list.

    If it is a proposal for the library then it needs to be more detailed and specific.

  2. semigod reporter

    Hi Charlie, This is a defect instead of question. I think property '_named_styles' of workbook should return objects of named styles instead of names of styles.

    >>> wb = Workbook()
    >>> create_predefined_styles(wb) # My function to create some styles in workbook
    >>> wb.named_styles
    ['Normal', 'header', 'field', 'footer', 'alert', 'warning']
    >>> wb.style_names
    ['Normal', 'header', 'field', 'footer', 'alert', 'warning']
    >>> wb._named_styles
    [<openpyxl.styles.named_styles.NamedStyle object>
    Parameters:
    hidden=False, name='Normal', builtinId=0, number_format='General', alignment=<openpyxl.styles.alignment.Alignment object>
    Parameters:
    justifyLastLine=None, horizontal=None, textRotation=0, vertical=None, relativeIndent=0.0, wrapText=None, indent=0.0, shrinkToFit=None, readingOrder=0.0, border=<openpyxl.styles.borders.Border object>
    Parameters:
    diagonalUp=False, outline=True, diagonalDown=False, start=None, end=None, left=<openpyxl.styles.borders.Side object>
    Parameters:
    style=None, color=None, right=<openpyxl.styles.borders.Side object>
    Parameters:
    style=None, color=None, top=<openpyxl.styles.borders.Side object>
    Parameters:
    style=None, color=None, bottom=<openpyxl.styles.borders.Side object>
    Parameters:
    style=None, color=None, diagonal=<openpyxl.styles.borders.Side object>
    Parameters:
    style=None, color=None, vertical=None, horizontal=None, fill=<openpyxl.styles.fills.PatternFill object>
    Parameters:
    patternType=None, fgColor=<openpyxl.styles.colors.Color object>
    Parameters:
    tint=0.0, auto=None, theme=None, indexed=None, rgb='00000000', type='rgb', bgColor=<openpyxl.styles.colors.Color object>
    Parameters:
    tint=0.0, auto=None, theme=None, indexed=None, rgb='00000000', type='rgb', font=<openpyxl.styles.fonts.Font object>
    Parameters:
    name='Calibri', charset=None, family=2.0, b=False, i=False, strike=None, outline=None, shadow=None, condense=None, color=<openpyxl.styles.colors.Color object>
    
  3. CharlieC

    I'm not against improvements in the API here but it's a bit pity simply due to the way NamedStyles are built. This is why I kept the container private and exposed only the names and a method for adding new styles. wb._named_styles[key] will let you work with the NamedStyle directly.

    For any changes I'd like to see a detailed use case that also considers non-sophisticated uses. Having a redundant property isn't much of a problem, I think.

  4. semigod reporter

    Here is the use case: We are trying to convert <table> from HTML into Excel. By default, we use HTML attribute 'class' to match predefined named styles in Excel. In case attributes 'class' and 'color' are all applied to a single cell, we would like to let 'color' take priority over color defined by class. In this case, we can not simply modify existing named styles as they are shared by whole workbook. So, we would like to find out the actual named style, clone it and apply new color. Then we can apply cloned named style to the cell. I am not sure if there's any better way to achieve same.

  5. CharlieC

    It would be nice to have a complete example but I would tend to do it without too many additional new named styles. If you have a cell with a named style you can overwrite formats on the cell (like using the style attribute in HTML) because changes to the named style only affect cells are subsequently assigned the named style. I decided that it was way too much overhead to try and check which cells were using a particular named style.

  6. Log in to comment