Change transparency of color in chart

406 views
Skip to first unread message

Conny Söderholm

unread,
Mar 17, 2021, 4:29:14 PM3/17/21
to openpyxl-users
Hi,

I am trying to change the transparency of a Chart.
I tried to pass an ARGB to the ColorChoice, but that does not work.

I also tried to create a new class to pass the color, but I am limited by what colorchoice accepts. Any suggestions on how to solve it?


Best,
Conny

Charlie Clark

unread,
Mar 18, 2021, 6:50:18 AM3/18/21
to openpyxl-users
What does your code look like? I don't think this is possible yet
because openpyxl does not expose all of the chart structure (that's all
7 layers of hell) in a way that you work with it.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Waldlehne 23
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Conny Söderholm

unread,
Mar 18, 2021, 10:08:39 AM3/18/21
to openpyx...@googlegroups.com
Ideally, I would like to pass the Alpha to the green color for example  "4D228b22" but this gives an error upon opening.
I have tried creating a separate ColorChoice object as well but I cannot pass a modified color object to it.

This works for setting a green color. Other example after this code.

from openpyxl import Workbook
from openpyxl.chart import AreaChart, Reference

import datetime, random

output_filename = "OpenPyXL_Area_Charts.xlsx"
output_wb = Workbook()
output_ws = output_wb.active

# Create some dates
dates = [datetime.date(2020, x, 1) for x in range(1,10,2)]

# Create som values for first and seconds bars
bars1 = [random.randint(10,50) for _ in range(5)]
bars2 = [random.randint(10,50) for _ in range(5)]

# Write the values to the sheet
for row in zip(dates,bars1, bars2):
    output_ws.append(row)

#Create the References
date_series = Reference(output_ws, min_col=1, min_row=1, max_col=1, max_row=5)
area1_series = Reference(output_ws, min_col=2, min_row=1, max_col=2, max_row=5)
area2_series = Reference(output_ws, min_col=3, min_row=1, max_col=3, max_row=5)

# Add Area Chart
area_chart = AreaChart()
area_chart.add_data(area1_series)

# Set the color of area1_series to green
green_color = "228b22"

a1_series = area_chart.series[0]
a1_series.graphicalProperties.line.solidFill = green_color
a1_series.graphicalProperties.solidFill = green_color

area_chart.add_data(area2_series)
area_chart.set_categories(date_series)
area_chart.title = "Bar Chart colors"
area_chart.x_axis.title = "Dates"
area_chart.y_axis.title = "Values"

output_ws.add_chart(area_chart, "A16")
output_wb.save(output_filename)

################################

But I would like to do like this
# Create green color
green_color = Color("228b22")
green_color.rgb =  "4D228b22"

a1_series = area_chart.series[0]
a1_series.graphicalProperties.line.solidFill = ColorChoice(srgbClr=green_color)
a1_series.graphicalProperties.solidFill = ColorChoice(srgbClr=green_color)

srgbClr needs to be passed as a string, but then I cannot pass a color with Alpha channel to change transparency.


--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/5AC933D0-D94A-450F-83E7-AF0BAF60007E%40clark-consulting.eu.


--
Conny Söderholm
about.me/connysoderholm

Charlie Clark

unread,
Mar 19, 2021, 11:11:32 AM3/19/21
to openpyx...@googlegroups.com
On 18 Mar 2021, at 15:10, Conny Söderholm wrote:

> But I would like to do like this
> # Create green color
> green_color = Color("228b22")
> green_color.rgb = "4D228b22"
>
> a1_series = area_chart.series[0]
> a1_series.graphicalProperties.line.solidFill =
> ColorChoice(srgbClr=green_color)
> a1_series.graphicalProperties.solidFill =
> ColorChoice(srgbClr=green_color)
>
> srgbClr needs to be passed as a string, but then I cannot pass a color
> with
> Alpha channel to change transparency.

Hm, looks like you're diving deep into DrawingML. Support for this in
openpyxl is neither complete nor error-free. Basically, I did enough to
support charts and some other things but you can tell by the lack of
tests that this not sufficient.

I assume you're already reverse engineering, right? Well, you should be
able to create the correct object from some XML and just pass it in:
it's often much easier than creating the code imperatively and probably
one of openpyxl's most powerful hacks.
Reply all
Reply to author
Forward
0 new messages