Skip to content

Commit cefccbb

Browse files
clement-escolanokbattersby
authored andcommitted
Remove nested tables
1 parent 81e4815 commit cefccbb

3 files changed

Lines changed: 16 additions & 78 deletions

File tree

html2docx/html2docx.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from docx import Document
66
from docx.enum.text import WD_ALIGN_PARAGRAPH
77
from docx.shared import Pt
8-
from docx.table import Table, _Cell
8+
from docx.table import Table
99
from docx.text.paragraph import Paragraph
1010
from docx.text.run import Run
1111
from tinycss2 import parse_declaration_list
@@ -79,8 +79,7 @@ def _reset(self) -> None:
7979

8080
# Formatting options
8181
self.pre = False
82-
self.table_cell: Optional[_Cell] = None
83-
self.tables: List[Tuple[Table, int, int]] = []
82+
self.table: Optional[Tuple[Table, int, int]] = None
8483
self.alignment: Optional[int] = None
8584
self.padding_left: Optional[Pt] = None
8685
self.attrs: List[List[Tuple[str, Any]]] = []
@@ -105,33 +104,34 @@ def finish_p(self) -> None:
105104
self._reset()
106105

107106
def init_table(self, attrs: List[Tuple[str, Optional[str]]]) -> None:
108-
container = self.doc if self.table_cell is None else self.table_cell
109-
table = container.add_table(rows=0, cols=0)
110-
self.tables.append((table, -1, -1))
107+
self.table = (self.doc.add_table(rows=0, cols=0), -1, -1)
111108

112109
def finish_table(self) -> None:
113-
table = self.tables.pop()[0]
110+
if self.table is None:
111+
return
114112
section = self.doc.sections[0]
115113
page_width = section.page_width - section.left_margin - section.right_margin
116-
page_width = int(page_width * (0.5 ** len(self.tables)))
114+
table = self.table[0]
117115
for col in table.columns:
118116
col.width = page_width // len(table.columns)
117+
self.table = None
119118

120119
def init_tr(self) -> None:
121-
table, row, col = self.tables[-1]
122-
row += 1
123-
col = -1
124-
self.tables[-1] = (table, row, col)
120+
if self.table is None:
121+
return
122+
table, row, col = self.table
125123
table.add_row()
124+
self.table = table, row + 1, -1
126125

127126
def init_tdth(self) -> None:
128-
table, row, col = self.tables[-1]
127+
if self.table is None:
128+
return
129+
table, row, col = self.table
129130
col += 1
130-
self.tables[-1] = (table, row, col)
131+
self.table = (table, row, col)
131132
if col >= len(table.columns):
132133
table.add_column(0)
133-
self.table_cell = table.cell(row, col)
134-
self.p = self.table_cell.paragraphs[0]
134+
self.p = self.table[0].cell(row, col).paragraphs[0]
135135
self.r = None
136136

137137
def init_run(self, attrs: List[Tuple[str, Any]]) -> None:
@@ -264,6 +264,5 @@ def handle_endtag(self, tag: str) -> None:
264264
elif tag == "table":
265265
self.finish_table()
266266
elif tag in ["td", "th"]:
267-
self.table_cell = None
268267
self.p = None
269268
self.r = None

tests/data/table_nested.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/data/table_nested.json

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)