Openpyxl cell fill color include alpha value

60 views Asked by At

I have a codeblock using PatternFill:

import openpyxl
from openpyxl.styles import PatternFill


wb = openpyxl.load_workbook(filename=filename)
if f'Parameter 1' in wb.sheetnames:
    ws = wb['Parameter 1']
else:
    ws = wb.create_sheet(f'Parameter 1', 0)
my_fill = PatternFill(patternType='solid', fgColor='1A801AC3', bgColor='1A801AC3')
ws.cell(row=32, column=8, value='Hello').fill = my_fill

I expect to fill my cell with almost transparent color, but I have solid fill with color(801AC3) and no transparency at all, as if I do not pass any alpha in aRGB.

1

There are 1 answers

0
Ga11eaN On BEST ANSWER

I solved issue modifiying @Eijaf's method , where alpha is in percentage from 0 to 1

def color_alpha(color, alpha):
    rgb = tuple(int(color[i:i+2], 16) for i in (0, 2, 4))
    rgb_alpha = []
    for i in rgb : 
        rgb_alpha.append(int(alpha * i + (1 - alpha) * 255))
    return '{:02X}{:02X}{:02X}'.format(rgb_alpha[0], rgb_alpha[1], rgb_alpha[2])