Skip to content

Commit eb64379

Browse files
committed
refactor: rename to_rgb_color function to to_color for clarity
1 parent 02af520 commit eb64379

File tree

14 files changed

+317
-90
lines changed

14 files changed

+317
-90
lines changed

src/tppt/pptx/converter.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pptx.util import Pt as PptxPt
2020

2121
from tppt.types._angle import Angle, Degrees, LiteralAngle
22-
from tppt.types._color import Color, LiteralColor, to_rgb_color
22+
from tppt.types._color import Color, LiteralColor, to_color
2323
from tppt.types._length import (
2424
CentiMeters,
2525
EnglishMetricUnits,
@@ -49,14 +49,18 @@ def from_pptx(cls, pptx_obj: PT) -> Self:
4949

5050

5151
@overload
52-
def to_pptx_length(length: Length | LiteralLength) -> PptxLength: ...
52+
def to_pptx_length(length: Length | LiteralLength | PptxLength) -> PptxLength: ...
5353

5454

5555
@overload
56-
def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None: ...
56+
def to_pptx_length(
57+
length: Length | LiteralLength | PptxLength | None,
58+
) -> PptxLength | None: ...
5759

5860

59-
def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None:
61+
def to_pptx_length(
62+
length: Length | LiteralLength | PptxLength | None,
63+
) -> PptxLength | None:
6064
if isinstance(length, tuple):
6165
length = to_length(length)
6266

@@ -71,6 +75,8 @@ def to_pptx_length(length: Length | LiteralLength | None) -> PptxLength | None:
7175
return PptxMm(length.value)
7276
case EnglishMetricUnits():
7377
return PptxEmu(length.value)
78+
case PptxLength():
79+
return length
7480
case None:
7581
return None
7682
case _:
@@ -113,7 +119,7 @@ def to_pptx_rgb_color(
113119
if color is None:
114120
return None
115121

116-
color = to_rgb_color(color)
122+
color = to_color(color)
117123

118124
return PptxRGBColor(color.r, color.g, color.b), color.a
119125

src/tppt/pptx/dml/color.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from typing import Self, cast
22

3+
from lxml.etree import _Element
34
from pptx.dml.color import ColorFormat as PptxColorFormat
45
from pptx.dml.color import RGBColor as PptxRGBColor
56
from pptx.enum.dml import MSO_THEME_COLOR
7+
from pptx.oxml.xmlchemy import OxmlElement
68

79
from tppt.pptx.converter import PptxConvertible, to_pptx_rgb_color, to_tppt_rgb_color
810
from tppt.types import Color, LiteralColor
@@ -27,12 +29,25 @@ def brightness(self, value: float) -> None:
2729

2830
@property
2931
def rgb(self) -> Color:
30-
return to_tppt_rgb_color(cast(PptxRGBColor, self._pptx.rgb), alpha=None)
32+
solid_fill = cast(
33+
_Element, self._pptx._xFill.solidFill.get_or_change_to_srgbClr()
34+
)
35+
alpha = solid_fill.find("a:alpha", None)
36+
if alpha is not None:
37+
alpha = alpha.attrib["val"]
38+
return to_tppt_rgb_color(cast(PptxRGBColor, self._pptx.rgb), alpha=alpha)
3139

3240
@rgb.setter
3341
def rgb(self, color: Color | LiteralColor):
34-
pptx_color, _ = to_pptx_rgb_color(color)
42+
pptx_color, alpha = to_pptx_rgb_color(color)
3543
self._pptx.rgb = pptx_color
44+
if alpha:
45+
solid_fill = cast(
46+
_Element, self._pptx._xFill.solidFill.get_or_change_to_srgbClr()
47+
)
48+
element = OxmlElement("a:alpha")
49+
element.attrib["val"] = str(alpha)
50+
solid_fill.append(element)
3651

3752
@property
3853
def theme_color(self) -> MSO_THEME_COLOR:

src/tppt/pptx/presentation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pptx.presentation import Presentation as PptxPresentation
77

8-
from tppt.pptx.tree import ppt2dict
8+
from tppt.pptx.tree import ppt2tree
99
from tppt.template.default import DefaultSlideMaster
1010
from tppt.template.slide_layout import SlideLayout, SlideLayoutProxy
1111
from tppt.template.slide_master import (
@@ -72,7 +72,7 @@ def slide_height(self, value: Length | LiteralLength) -> None:
7272
@property
7373
def tree(self) -> dict[str, Any]:
7474
"""Get the node tree of the presentation."""
75-
return ppt2dict(self._pptx)
75+
return ppt2tree(self._pptx)
7676

7777
@overload
7878
@classmethod

src/tppt/pptx/shape/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING, Self, TypedDict
44

5+
from pptx.opc.package import XmlPart
6+
from pptx.shapes import Subshape as PptxSubshape
57
from pptx.shapes.autoshape import Shape as PptxShape
68
from pptx.shapes.base import BaseShape as PptxBaseShape
79
from typing_extensions import TypeVar
@@ -56,6 +58,22 @@ def text_frame(self) -> "TextFrame":
5658
return TextFrame(self._pptx.text_frame)
5759

5860

61+
class SubShape(PptxConvertible[PptxSubshape]):
62+
def __init__(self, pptx_shape: PptxSubshape) -> None:
63+
self._pptx: PptxSubshape = pptx_shape
64+
65+
@property
66+
def part(self) -> XmlPart:
67+
return self._pptx.part
68+
69+
def to_pptx(self) -> PptxSubshape:
70+
return self._pptx
71+
72+
@classmethod
73+
def from_pptx(cls, pptx_obj: PptxSubshape) -> Self:
74+
return cls(pptx_obj)
75+
76+
5977
class RangeProps(TypedDict):
6078
"""Range properties."""
6179

src/tppt/pptx/shape/text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE, PP_ALIGN
44
from pptx.shapes.autoshape import Shape as PptxShape
55

6-
from tppt.pptx.converter import to_pptx_length, to_pptx_rgb_color
6+
from tppt.pptx.converter import to_pptx_length
77
from tppt.pptx.text.text_frame import TextFrame
8-
from tppt.types._color import Color, LiteralColor
8+
from tppt.types._color import Color, LiteralColor, to_color
99
from tppt.types._length import Length, LiteralLength
1010

1111
from . import RangeProps, Shape
@@ -52,7 +52,7 @@ def __init__(self, pptx_obj: PptxShape, data: TextData | None = None, /) -> None
5252
if (italic := data.get("italic")) is not None:
5353
font.italic = italic
5454
if (color := data.get("color")) is not None:
55-
font.color.rgb = to_pptx_rgb_color(color)
55+
font.color.rgb = to_color(color)
5656
if (margin_bottom := data.get("margin_bottom")) is not None:
5757
p.space_after = to_pptx_length(margin_bottom)
5858
if (margin_left := data.get("margin_left")) is not None:

src/tppt/pptx/text/paragraph.py

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,108 @@
11
from typing import Self
22

3+
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
34
from pptx.text.text import _Paragraph as PptxParagraph
5+
from pptx.util import Length as PptxLength
46

5-
from tppt.pptx.converter import PptxConvertible
7+
from tppt.pptx.converter import PptxConvertible, to_pptx_length
8+
from tppt.pptx.text.font import Font
69
from tppt.pptx.text.run import Run
10+
from tppt.types._length import Length, to_length
711

812

913
class Paragraph(PptxConvertible[PptxParagraph]):
1014
def __init__(self, pptx_obj: PptxParagraph) -> None:
1115
self._pptx = pptx_obj
1216

13-
@property
14-
def runs(self) -> list[Run]:
15-
return [Run(run) for run in self._pptx.runs]
17+
def add_line_break(self) -> None:
18+
self._pptx.add_line_break()
1619

1720
def add_run(self) -> Run:
1821
return Run(self._pptx.add_run())
1922

23+
@property
24+
def alignment(self) -> PP_PARAGRAPH_ALIGNMENT | None:
25+
return self._pptx.alignment
26+
27+
@alignment.setter
28+
def alignment(self, value: PP_PARAGRAPH_ALIGNMENT | None) -> None:
29+
self._pptx.alignment = value
30+
31+
def clear(self) -> None:
32+
self._pptx.clear()
33+
34+
@property
35+
def font(self) -> Font:
36+
return Font(self._pptx.font)
37+
38+
@property
39+
def level(self) -> int:
40+
return self._pptx.level
41+
42+
@level.setter
43+
def level(self, value: int) -> None:
44+
self._pptx.level = value
45+
46+
@property
47+
def line_spacing(self) -> int | float | Length | None:
48+
match self._pptx.line_spacing:
49+
case PptxLength():
50+
return to_length(self._pptx.line_spacing)
51+
case _:
52+
return self._pptx.line_spacing
53+
54+
@line_spacing.setter
55+
def line_spacing(self, value: int | float | Length | PptxLength | None) -> None:
56+
match value:
57+
case int() | float() | PptxLength() | None:
58+
self._pptx.line_spacing = value
59+
case _:
60+
self._pptx.line_spacing = to_pptx_length(value)
61+
62+
@property
63+
def runs(self) -> tuple[Run, ...]:
64+
return tuple(Run(run) for run in self._pptx.runs)
65+
66+
@property
67+
def space_after(self) -> Length | None:
68+
match self._pptx.space_after:
69+
case PptxLength():
70+
return to_length(self._pptx.space_after)
71+
case _:
72+
return self._pptx.space_after
73+
74+
@space_after.setter
75+
def space_after(self, value: Length | PptxLength | None) -> None:
76+
match value:
77+
case PptxLength() | None:
78+
self._pptx.space_after = value
79+
case _:
80+
self._pptx.space_after = to_pptx_length(value)
81+
82+
@property
83+
def space_before(self) -> Length | None:
84+
match self._pptx.space_before:
85+
case PptxLength():
86+
return to_length(self._pptx.space_before)
87+
case _:
88+
return self._pptx.space_before
89+
90+
@space_before.setter
91+
def space_before(self, value: Length | PptxLength | None) -> None:
92+
match value:
93+
case PptxLength() | None:
94+
self._pptx.space_before = value
95+
case _:
96+
self._pptx.space_before = to_pptx_length(value)
97+
98+
@property
99+
def text(self) -> str:
100+
return self._pptx.text
101+
102+
@text.setter
103+
def text(self, value: str) -> None:
104+
self._pptx.text = value
105+
20106
def to_pptx(self) -> PptxParagraph:
21107
return self._pptx
22108

src/tppt/pptx/text/run.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ class Run(PptxConvertible[PptxRun]):
1111
def __init__(self, pptx_obj: PptxRun) -> None:
1212
self._pptx = pptx_obj
1313

14-
@property
15-
def text(self) -> str:
16-
return self._pptx.text
17-
18-
@text.setter
19-
def text(self, text: str) -> None:
20-
self._pptx.text = text
21-
2214
@property
2315
def font(self) -> Font:
2416
return Font(self._pptx.font)
@@ -27,6 +19,14 @@ def font(self) -> Font:
2719
def hyperlink(self) -> Hyperlink:
2820
return Hyperlink(self._pptx.hyperlink)
2921

22+
@property
23+
def text(self) -> str:
24+
return self._pptx.text
25+
26+
@text.setter
27+
def text(self, text: str) -> None:
28+
self._pptx.text = text
29+
3030
def to_pptx(self) -> PptxRun:
3131
return self._pptx
3232

0 commit comments

Comments
 (0)