Skip to content

Commit c05237a

Browse files
committed
refactor: add functional_text utility and enhance formatted_text example with new text styling options
1 parent 70dea2b commit c05237a

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

examples/formatted_text.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,74 @@
99

1010

1111
def main():
12-
def formatted_text(text: tppt.pptx.shape.text.Text) -> tppt.pptx.shape.text.Text:
12+
def formatted_text(text: tppt.pptx.shape.text.Text, /) -> tppt.pptx.shape.text.Text:
1313
run = text.text_frame().add_paragraph().add_run()
1414
run.text = "Hello, world!"
1515
font = run.font
16-
font.color.rgb = "#0000FF"
16+
font.color.rgb = "#00FFFF"
1717
font.italic = True
1818

1919
return text
2020

21+
def functional_text(
22+
text_obj: tppt.pptx.shape.text.Text,
23+
/,
24+
*,
25+
text: str,
26+
bold: bool = False,
27+
italic: bool = False,
28+
color: tppt.types.Color | tppt.types.LiteralColor | None = None,
29+
) -> tppt.pptx.shape.text.Text:
30+
run = text_obj.text_frame().add_paragraph().add_run()
31+
run.text = text
32+
font = run.font
33+
34+
if color is not None:
35+
font.color.rgb = color
36+
if bold:
37+
font.bold = True
38+
if italic:
39+
font.italic = True
40+
41+
return text_obj
42+
2143
"""Run the example."""
2244
# Create a presentation using the builder pattern
2345
presentation = (
2446
tppt.Presentation.builder()
2547
.slide(
2648
lambda slide: slide.BlankLayout()
2749
.builder()
50+
.text(
51+
# Pattern build-in options
52+
"This is a text",
53+
left=(50, "pt"),
54+
top=(50, "pt"),
55+
width=(400, "pt"),
56+
height=(50, "pt"),
57+
bold=True,
58+
italic=True,
59+
color="#0000FF",
60+
)
2861
.text(
2962
# Pattern custom function
3063
formatted_text,
3164
left=(50, "pt"),
32-
top=(50, "pt"),
65+
top=(100, "pt"),
66+
width=(400, "pt"),
67+
height=(50, "pt"),
68+
)
69+
.text(
70+
# Pattern use partial apply function
71+
tppt.apply(
72+
functional_text,
73+
text="Hello, world!",
74+
bold=True,
75+
italic=True,
76+
color="#00FF00",
77+
),
78+
left=(50, "pt"),
79+
top=(150, "pt"),
3380
width=(400, "pt"),
3481
height=(50, "pt"),
3582
)

src/tppt/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
"""Typed Python PowerPoint Tool"""
22

3+
from typing import Callable, Concatenate, ParamSpec, TypeVar
4+
35
from tppt.pptx.presentation import Presentation
46
from tppt.template.slide_layout import Placeholder, SlideLayout
57
from tppt.template.slide_master import Layout, SlideMaster, slide_master
68

79
from . import types as types
810

11+
T = TypeVar("T")
12+
P = ParamSpec("P")
13+
14+
15+
def apply(
16+
func: Callable[Concatenate[T, P], T], *args: P.args, **kwargs: P.kwargs
17+
) -> Callable[[T], T]:
18+
"""
19+
Partially applies the given function `func` with arguments `args` and keyword arguments `kwargs`,
20+
returning a new function that takes only the first argument.
21+
22+
Args:
23+
func: A function that takes a first argument of type `T` and variable arguments `P`, returning `T`
24+
*args: Variable positional arguments to partially apply to `func`
25+
**kwargs: Variable keyword arguments to partially apply to `func`
26+
27+
Returns:
28+
A function that takes a single argument `x` and calls `func(x, *args, **kwargs)`
29+
"""
30+
31+
def wrapper(x: T) -> T:
32+
return func(x, *args, **kwargs)
33+
34+
return wrapper
35+
36+
937
__all__ = [
1038
"Presentation",
1139
"SlideLayout",
@@ -14,4 +42,5 @@
1442
"SlideMaster",
1543
"Layout",
1644
"slide_master",
45+
"apply",
1746
]

0 commit comments

Comments
 (0)