Write Direct Shape Area Pattern

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

Setup

from __future__ import annotations
import uno
from ooodev.format.writer.direct.shape.area import Pattern as ShapePattern
from ooodev.format.writer.direct.shape.area import PresetPatternKind
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:
    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 = ShapePattern.from_preset(preset=PresetPatternKind.SHINGLE)
        style.apply(rect)
        page.add(rect)

        f_style = ShapePattern.from_obj(rect)
        assert f_style

        Lo.delay(1_000)

        Lo.close_doc(doc)

    return 0

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

Add a pattern to the shape

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

from ooodev.format.writer.direct.shape.area import Pattern as ShapePattern
from ooodev.format.writer.direct.shape.area import PresetPatternKind
# ... other code

rect = Draw.draw_rectangle(slide=page, x=10, y=10, width=100, height=100)
style = ShapePattern.from_preset(preset=PresetPatternKind.SHINGLE)
style.apply(rect)

The results of the setting the shape fill pattern can be seen in Fig. 1007.

Shape with pattern

Fig. 1007 Shape with pattern

Get Shape Pattern

We can get the fill pattern of the shape by using the ShapePattern.from_obj() method.

from ooodev.format.draw.direct.area import Pattern as ShapePattern
# ... other code

# get the pattern from the shape
f_style = ShapePattern.from_obj(rec)
assert f_style