diff --git a/src/docx/oxml/__init__.py b/src/docx/oxml/__init__.py index 37f608cef..503c0050e 100644 --- a/src/docx/oxml/__init__.py +++ b/src/docx/oxml/__init__.py @@ -155,6 +155,7 @@ CT_TblGrid, CT_TblGridCol, CT_TblLayoutType, + CT_TblLook, CT_TblPr, CT_TblPrEx, CT_TblWidth, @@ -173,6 +174,7 @@ register_element_cls("w:tbl", CT_Tbl) register_element_cls("w:tblGrid", CT_TblGrid) register_element_cls("w:tblLayout", CT_TblLayoutType) +register_element_cls("w:tblLook", CT_TblLook) register_element_cls("w:tblPr", CT_TblPr) register_element_cls("w:tblPrEx", CT_TblPrEx) register_element_cls("w:tblStyle", CT_String) diff --git a/src/docx/oxml/table.py b/src/docx/oxml/table.py index 9457da207..98675d9a1 100644 --- a/src/docx/oxml/table.py +++ b/src/docx/oxml/table.py @@ -256,6 +256,20 @@ def _tcs_xml(cls, col_count: int, col_width: Length) -> str: ) * col_count +class CT_TblLook(BaseOxmlElement): + """`w:tblLook` element, child of `w:tblPr`. + + Specifies visual formatting of the table + """ + + noHBand: int | None = OptionalAttribute( # pyright: ignore[reportAssignmentType] + "w:noHBand", XsdInt, default=1 + ) + noVBand: int | None = OptionalAttribute( # pyright: ignore[reportAssignmentType] + "w:noVBand", XsdInt, default=1 + ) + + class CT_TblGrid(BaseOxmlElement): """`w:tblGrid` element. @@ -301,6 +315,7 @@ class CT_TblPr(BaseOxmlElement): get_or_add_bidiVisual: Callable[[], CT_OnOff] get_or_add_jc: Callable[[], CT_Jc] get_or_add_tblLayout: Callable[[], CT_TblLayoutType] + get_or_add_tblLook: Callable[[], CT_TblLook] _add_tblStyle: Callable[[], CT_String] _remove_bidiVisual: Callable[[], None] _remove_jc: Callable[[], None] @@ -338,6 +353,9 @@ class CT_TblPr(BaseOxmlElement): tblLayout: CT_TblLayoutType | None = ZeroOrOne( # pyright: ignore[reportAssignmentType] "w:tblLayout", successors=_tag_seq[13:] ) + tblLook: CT_TblLook | None = ZeroOrOne( + "w:tblLook", successors=_tag_seq[15:]) # pyright: ignore[reportAssignmentType] + del _tag_seq @property diff --git a/src/docx/table.py b/src/docx/table.py index 545c46884..9491694cb 100644 --- a/src/docx/table.py +++ b/src/docx/table.py @@ -82,6 +82,34 @@ def autofit(self) -> bool: def autofit(self, value: bool): self._tblPr.autofit = value + @property + def banded_columns(self) -> bool: + """|True| if banded columns formatting is applied to this table. + + |False| if banded columns formatting is not applied. Read/write boolean. + """ + tblLook = self._tblPr.tblLook + return not tblLook.noVBand if tblLook is not None else False + + @banded_columns.setter + def banded_columns(self, value: bool): + tblLook = self._tblPr.get_or_add_tblLook() + tblLook.noVBand = int(not value) + + @property + def banded_rows(self) -> bool: + """|True| if banded rows formatting is applied to this table. + + |False| if banded rows formatting is not applied. Read/write boolean. + """ + tblLook = self._tblPr.tblLook + return not tblLook.noHBand if tblLook is not None else False + + @banded_rows.setter + def banded_rows(self, value: bool): + tblLook = self._tblPr.get_or_add_tblLook() + tblLook.noHBand = int(not value) + def cell(self, row_idx: int, col_idx: int) -> _Cell: """|_Cell| at `row_idx`, `col_idx` intersection.