Write Direct Shape Area Gradient

The ooodev.format.writer.direct.shape.area.Gradient class is used to modify the values seen in Fig. 836 of a shape.

Setup

import uno
from ooodev.format.writer.direct.shape.area import Gradient as ShapeGradient
from ooodev.format.writer.direct.shape.area import PresetGradientKind
from ooodev.gui import GUI
from ooodev.loader.lo import Lo
from ooodev.office.write import Write
from ooodev.office.draw import Draw


def main() -> int:
    """Main Entry Point"""

    with Lo.Loader(Lo.ConnectPipe()):
        doc = Write.create_doc()
        GUI.set_visible(doc=doc)
        Lo.delay(300)
        GUI.zoom(GUI.ZoomEnum.ENTIRE_PAGE)

        page = Write.get_draw_page(doc)
        rect = Draw.draw_rectangle(slide=page, x=10, y=10, width=100, height=100)
        style = ShapeGradient.from_preset(preset=PresetGradientKind.DEEP_OCEAN)
        style.apply(rect)
        page.add(rect)

        f_style = ShapeGradient.from_obj(rect)
        assert f_style.prop_inner == style.prop_inner

        Lo.delay(1_000)

        Lo.close_doc(doc)

    return 0

if __name__ == "__main__":
    raise SystemExit(main())

Fill Shape Gradient

Add a gradient to the shape

Adding a fill gradient to the shape is done by using the ShapeGradient class. The ShapeGradient class has a method from_preset() takes a preset as a parameter. The PresetGradientKind class is used to get the preset of the gradient.

from ooodev.format.writer.direct.shape.area import Color as ShapeColor
# ... other code

style = ShapeGradient.from_preset(preset=PresetGradientKind.DEEP_OCEAN)
style.apply(rect)
page.add(rect)

The results of the setting the shape fill gradient can be seen in Fig. 1004.

Shape with Gradient color

Fig. 1004 Shape with Gradient color

Get Shape Gradient

We can get the fill gradient of the shape by using the ShapeColor.from_obj() method.

from ooodev.format.writer.direct.shape.area import Color as ShapeColor
# ... other code

# get the gradient from the shape
f_style = ShapeGradient.from_obj(rect)
# assert the color is the same
assert f_style.prop_inner == style.prop_inner