设置面积图Openpyxl的透明度(Alpha)

2022-04-07 00:00:00 python openpyxl

问题描述

我想使用Openpyxl将透明度设置为面积图的背景。我的图表代码是:

from openpyxl.drawing.fill import PatternFillProperties, ColorChoice

c2 = AreaChart()
c2.grouping = 'stacked'
c2.add_data(data, titles_from_data=True)

c2_s1 = c2.series[0]
fill = PatternFillProperties()
fill.background = ColorChoice(srgbClr="FFFFFF")

c2_s2.graphicalProperties.pattFill = fill

我已尝试使用RGBA十六进制编码。

fill.background = ColorChoice(srgbClr="80FFFFFF")

我也试过使用SystemColor,但我找不到任何如何使用它的例子,我每次尝试都不成功


解决方案

扩展颜色选项

from openpyxl.compat import unicode
from openpyxl.descriptors import Typed
from openpyxl.descriptors.serialisable import Serialisable
from openpyxl.descriptors.nested import NestedInteger
from openpyxl.xml.constants import DRAWING_NS


class MyRGBColor(Serialisable):
    tagname = 'srgbClr'
    namespace = DRAWING_NS
    val = Typed(expected_type=unicode)
    alpha = NestedInteger(allow_none=True)
    __elements__ = ('alpha', )

    def __init__(self, val, alpha=None):
        self.val = val
        self.alpha = alpha


class MyColorChoice(ColorChoice):
    srgbClr = Typed(expected_type=MyRGBColor, allow_none=True)

..和

c2_s1.graphicalProperties.solidFill = MyColorChoice(
        srgbClr=MyRGBColor('0000FF', alpha=50000))

相关文章