AttributeError - Cannot import Color.RED

Issue #327 resolved
Mitch LeBlanc created an issue

According to the docs, this should work:

from openpyxl.workbook import Workbook
from openpyxl.styles import Style, Color, Font
wb = Workbook()
ws = wb.active
ws['A1'] = 'This is red'
ws['A1'].style = Style(font=Font(color=Color(Color.RED)))

However, Color.RED causes an error:

AttributeError: Type object 'Color' has no attribute 'RED'

I can set the index manually, e.g. '00FF0000' and it works, but I think your init.py import of Color class is messing with the module constants somehow?

I'm on Python 3.4.0 on win32, with the very newest 2.0.3 openpyxl.

Comments (5)

  1. CharlieC

    These are now module constants. I think the spelling colors.RED is cleaner and it allows a direct import.

    Actually, the docs are correct. No idea why 2.0.2, which is what you linked to, is wrong.

  2. Mitch LeBlanc reporter

    Ok, I tried the newer version, but I found that you need to also import colors module specifically.

    Or you can only import colors but then you have to specify colors.Color(index=colors.RED)

    i.e. docs should read:

    from openpyxl.styles import Style, Color, colors, Font
    
  3. CharlieC

    Charlie Clark sent you a message on Bitbucket: Yeah, "latest" works but I don't remember making that change just for 2.0.3 I'd try and avoid colors.Color probably best to

    from openpyxl.styles import Color

    and then either

    from openpyxl.styles.colors import RED, BLUE, YELLOW

    or just

    from openpyxl.styles import colors

    Depending on how you're using the constants in your code and whether prefixing them with colors. is helpful or a chore.

  4. Log in to comment