From 6d49a690bb22fcec30614ef30156242ba0ececd5 Mon Sep 17 00:00:00 2001 From: Steve Canny Date: Sun, 28 Apr 2024 17:39:35 -0700 Subject: [PATCH] rfctr(table): reimplement CT_Tc._tr_above Use XPath rather than oxml structures. --- src/docx/oxml/table.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/docx/oxml/table.py b/src/docx/oxml/table.py index ddebee71b..963f3ebf7 100644 --- a/src/docx/oxml/table.py +++ b/src/docx/oxml/table.py @@ -748,11 +748,10 @@ def _tr_above(self) -> CT_Row: Raises |ValueError| if called on a cell in the top-most row. """ - tr_lst = self._tbl.tr_lst - tr_idx = tr_lst.index(self._tr) - if tr_idx == 0: - raise ValueError("no tr above topmost tr") - return tr_lst[tr_idx - 1] + tr_aboves = self.xpath("./ancestor::w:tr[position()=1]/preceding-sibling::w:tr[1]") + if not tr_aboves: + raise ValueError("no tr above topmost tr in w:tbl") + return tr_aboves[0] @property def _tr_below(self) -> CT_Row | None: