Skip to content

Commit c398a32

Browse files
committed
refactor: introduce TextFrame, Paragraph, and Run classes with builder support for improved text handling
1 parent 77c2d3d commit c398a32

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

src/tppt/pptx/shape/text.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pptx.shapes.autoshape import Shape as PptxShape
55

66
from tppt.pptx.converter import to_pptx_color, to_pptx_length
7+
from tppt.pptx.text.text_frame import TextFrame
78
from tppt.types._color import Color, LiteralColor
89
from tppt.types._length import Length, LiteralLength
910

@@ -69,6 +70,9 @@ def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None
6970

7071
self._pptx = pptx_obj
7172

73+
def text_frame(self) -> TextFrame:
74+
return TextFrame(self._pptx.text_frame)
75+
7276
def to_pptx(self) -> PptxShape:
7377
"""Convert to pptx shape."""
7478
return self._pptx

src/tppt/pptx/text/__init__.py

Whitespace-only changes.

src/tppt/pptx/text/paragraph.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import Callable, Self
2+
3+
from pptx.text.text import _Paragraph as PptxParagraph
4+
5+
from tppt.pptx.converter import PptxConvertible
6+
from tppt.pptx.text.run import Run, RunBuilder
7+
8+
9+
class Paragraph(PptxConvertible[PptxParagraph]):
10+
def __init__(self, pptx_obj: PptxParagraph) -> None:
11+
self._pptx = pptx_obj
12+
13+
def builder(self) -> "ParagraphBuilder":
14+
return ParagraphBuilder(self._pptx)
15+
16+
def to_pptx(self) -> PptxParagraph:
17+
return self._pptx
18+
19+
@classmethod
20+
def from_pptx(cls, pptx_obj: PptxParagraph) -> Self:
21+
return cls(pptx_obj)
22+
23+
24+
class ParagraphBuilder:
25+
def __init__(self, pptx_obj: PptxParagraph) -> None:
26+
self._pptx = pptx_obj
27+
28+
def text(self, text: str) -> "ParagraphBuilder":
29+
self._pptx.text = text
30+
31+
return self
32+
33+
def run(self, callable: Callable[[Run], Run | RunBuilder]) -> "ParagraphBuilder":
34+
run = callable(Run(self._pptx.add_run()))
35+
if isinstance(run, RunBuilder):
36+
run._build()
37+
38+
return self
39+
40+
def _build(self) -> Paragraph:
41+
return Paragraph(self._pptx)

src/tppt/pptx/text/run.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Self
2+
3+
from pptx.text.text import _Run as PptxRun
4+
5+
from tppt.pptx.converter import PptxConvertible
6+
7+
8+
class Run(PptxConvertible[PptxRun]):
9+
def __init__(self, pptx_obj: PptxRun) -> None:
10+
self._pptx = pptx_obj
11+
12+
def builder(self) -> "RunBuilder":
13+
return RunBuilder(self._pptx)
14+
15+
def to_pptx(self) -> PptxRun:
16+
return self._pptx
17+
18+
@classmethod
19+
def from_pptx(cls, pptx_obj: PptxRun) -> Self:
20+
return cls(pptx_obj)
21+
22+
23+
class RunBuilder:
24+
def __init__(self, pptx_obj: PptxRun) -> None:
25+
self._pptx = pptx_obj
26+
27+
def _build(self) -> Run:
28+
return Run(self._pptx)

src/tppt/pptx/text/text_frame.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Callable, Self
2+
3+
from pptx.text.text import TextFrame as PptxTextFrame
4+
5+
from tppt.pptx.converter import PptxConvertible
6+
from tppt.pptx.text.paragraph import Paragraph, ParagraphBuilder
7+
8+
9+
class TextFrame(PptxConvertible[PptxTextFrame]):
10+
def __init__(self, pptx_obj: PptxTextFrame) -> None:
11+
self._pptx = pptx_obj
12+
13+
def builder(self) -> "TextFrameBuilder":
14+
return TextFrameBuilder(self._pptx)
15+
16+
def to_pptx(self) -> PptxTextFrame:
17+
return self._pptx
18+
19+
@classmethod
20+
def from_pptx(cls, pptx_obj: PptxTextFrame) -> Self:
21+
return cls(pptx_obj)
22+
23+
24+
class TextFrameBuilder:
25+
def __init__(self, pptx_obj: PptxTextFrame) -> None:
26+
self._pptx = pptx_obj
27+
28+
def paragraph(
29+
self, callable: Callable[[Paragraph], Paragraph | ParagraphBuilder]
30+
) -> "TextFrameBuilder":
31+
paragraph = callable(Paragraph(self._pptx.add_paragraph()))
32+
if isinstance(paragraph, ParagraphBuilder):
33+
paragraph._build()
34+
35+
return self
36+
37+
def _build(self) -> TextFrame:
38+
return TextFrame(self._pptx)

0 commit comments

Comments
 (0)