55from typing import TYPE_CHECKING , Iterator
66
77from pptx .dml .fill import FillFormat
8+ from pptx .dml .line import LineFormat
89from pptx .oxml .table import TcRange
910from pptx .shapes import Subshape
1011from pptx .text .text import TextFrame
@@ -165,6 +166,62 @@ def vert_banding(self, value: bool):
165166 self ._tbl .bandCol = value
166167
167168
169+ class _BorderEdge :
170+ """Adapter providing a `LineFormat`-compatible interface for one edge of a cell border.
171+
172+ `LineFormat` requires a parent with `.ln` and `.get_or_add_ln()`. This adapter delegates those
173+ to the appropriate border element (`a:lnL`, `a:lnR`, `a:lnT`, `a:lnB`) on `a:tcPr`.
174+ """
175+
176+ def __init__ (self , tc : CT_TableCell , edge_attr : str ):
177+ self ._tc = tc
178+ self ._edge_attr = edge_attr # e.g. "lnL", "lnR", "lnT", "lnB"
179+
180+ @property
181+ def ln (self ):
182+ """Return the `a:lnX` element or None."""
183+ tcPr = self ._tc .tcPr
184+ if tcPr is None :
185+ return None
186+ return getattr (tcPr , self ._edge_attr )
187+
188+ def get_or_add_ln (self ):
189+ """Return the `a:lnX` element, creating `a:tcPr` and the element if not present."""
190+ tcPr = self ._tc .get_or_add_tcPr ()
191+ return getattr (tcPr , f"get_or_add_{ self ._edge_attr } " )()
192+
193+
194+ class _CellBorders :
195+ """Provides access to border line formatting for each edge of a table cell.
196+
197+ Accessed via `cell.borders`. Each edge (`.left`, `.right`, `.top`, `.bottom`) returns a
198+ |LineFormat| object that controls the border's color, width, and dash style.
199+ """
200+
201+ def __init__ (self , tc : CT_TableCell ):
202+ self ._tc = tc
203+
204+ @lazyproperty
205+ def bottom (self ) -> LineFormat :
206+ """|LineFormat| for the bottom border of this cell."""
207+ return LineFormat (_BorderEdge (self ._tc , "lnB" ))
208+
209+ @lazyproperty
210+ def left (self ) -> LineFormat :
211+ """|LineFormat| for the left border of this cell."""
212+ return LineFormat (_BorderEdge (self ._tc , "lnL" ))
213+
214+ @lazyproperty
215+ def right (self ) -> LineFormat :
216+ """|LineFormat| for the right border of this cell."""
217+ return LineFormat (_BorderEdge (self ._tc , "lnR" ))
218+
219+ @lazyproperty
220+ def top (self ) -> LineFormat :
221+ """|LineFormat| for the top border of this cell."""
222+ return LineFormat (_BorderEdge (self ._tc , "lnT" ))
223+
224+
168225class _Cell (Subshape ):
169226 """Table cell"""
170227
@@ -187,6 +244,21 @@ def __ne__(self, other: object) -> bool:
187244 return True
188245 return self ._tc is not other ._tc
189246
247+ @lazyproperty
248+ def borders (self ) -> _CellBorders :
249+ """|_CellBorders| instance for this cell.
250+
251+ Provides access to the line formatting for each border edge. Each edge (`.left`, `.right`,
252+ `.top`, `.bottom`) is a |LineFormat| object.
253+
254+ Example::
255+
256+ cell.borders.top.width = Pt(2)
257+ cell.borders.top.color.rgb = RGBColor(0xFF, 0x00, 0x00)
258+ cell.borders.bottom.dash_style = MSO_LINE.DASH
259+ """
260+ return _CellBorders (self ._tc )
261+
190262 @lazyproperty
191263 def fill (self ) -> FillFormat :
192264 """|FillFormat| instance for this cell.
0 commit comments