Skip to content

Commit 781b588

Browse files
committed
refactor: enhance text handling by adding italic support and improving text registration in SlideBuilder
1 parent d3546e2 commit 781b588

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

examples/color_text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ def main():
2222
lambda run: run.builder()
2323
.text("Hello, world!")
2424
.font(
25-
lambda font: font.builder().color(
26-
lambda color: color.builder().rgb("#0000FF")
27-
)
25+
lambda font: font.builder()
26+
.color(lambda color: color.builder().rgb("#0000FF"))
27+
.italic(True)
2828
)
2929
)
3030
),

src/tppt/pptx/shape/text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Text(Shape[PptxShape]):
3939
"""Text data class."""
4040

4141
def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None:
42-
if data:
42+
if data and data["text"] != "":
4343
text_frame = pptx_obj.text_frame
4444
p = text_frame.paragraphs[0]
4545
run = p.add_run()

src/tppt/pptx/slide.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,17 @@ def text(
104104
text: str | Callable[[Text], Text | TextBuilder],
105105
**kwargs: Unpack[TextProps],
106106
) -> Self:
107-
data = TextData(
108-
type="text",
109-
text=text if isinstance(text, str) else "",
110-
top=kwargs["top"],
111-
left=kwargs["left"],
112-
width=kwargs["width"],
113-
height=kwargs["height"],
114-
)
107+
def _register(slide: Slide) -> Text:
108+
data = TextData(
109+
type="text",
110+
text=text if isinstance(text, str) else "",
111+
top=kwargs["top"],
112+
left=kwargs["left"],
113+
width=kwargs["width"],
114+
height=kwargs["height"],
115+
)
115116

116-
self._shape_registry.append(
117-
lambda slide: Text(
117+
text_obj = Text(
118118
slide.to_pptx().shapes.add_textbox(
119119
to_pptx_length(data["left"]),
120120
to_pptx_length(data["top"]),
@@ -123,7 +123,15 @@ def text(
123123
),
124124
data,
125125
)
126-
)
126+
if isinstance(text, Callable):
127+
text_builder = text(text_obj)
128+
if isinstance(text_builder, TextBuilder):
129+
text_builder = text_builder._build()
130+
return text_builder
131+
else:
132+
return text_obj
133+
134+
self._shape_registry.append(_register)
127135

128136
return self
129137

0 commit comments

Comments
 (0)