Skip to content

Commit de0ed4a

Browse files
committed
refactor: enhance picture method in SlideBuilder to support callable images and improve parameter handling
1 parent 7e03de6 commit de0ed4a

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/tppt/pptx/slide.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,36 @@ def _register(slide: Slide) -> Text:
133133

134134
return self
135135

136+
@overload
137+
def picture(
138+
self, image: FilePath | IO[bytes], /, **kwargs: Unpack[PictureProps]
139+
) -> Self: ...
140+
141+
@overload
142+
def picture(
143+
self,
144+
image: Callable[[Picture], Picture],
145+
/,
146+
image_file: FilePath | IO[bytes],
147+
**kwargs: Unpack[PictureProps],
148+
) -> Self: ...
149+
136150
def picture(
137-
self, image_file: FilePath | IO[bytes], /, **kwargs: Unpack[PictureProps]
151+
self,
152+
image: FilePath | IO[bytes] | Callable[[Picture], Picture],
153+
image_file: FilePath | IO[bytes] | None = None,
154+
**kwargs: Unpack[PictureProps],
138155
) -> Self:
156+
if not isinstance(image, Callable):
157+
image_file = image
158+
159+
assert image_file
139160
if isinstance(image_file, os.PathLike):
140161
image_file = os.fspath(image_file)
141162

142-
data = PictureData(type="picture", image_file=image_file, **kwargs)
143-
144-
self._shape_registry.append(
145-
lambda slide: Picture(
163+
def _register(slide: Slide) -> Picture:
164+
data = PictureData(type="picture", image_file=image_file, **kwargs)
165+
picture_obj = Picture(
146166
slide.to_pptx().shapes.add_picture(
147167
image_file,
148168
to_pptx_length(data["left"]),
@@ -152,7 +172,12 @@ def picture(
152172
),
153173
data,
154174
)
155-
)
175+
if isinstance(image, Callable):
176+
return image(picture_obj)
177+
else:
178+
return picture_obj
179+
180+
self._shape_registry.append(_register)
156181

157182
return self
158183

0 commit comments

Comments
 (0)