Skip to content

Commit 0ebbd67

Browse files
fix: table column widths word (#80)
* fix: table column widths in MS Word * version bump
1 parent 8da5630 commit 0ebbd67

2 files changed

Lines changed: 8 additions & 15 deletions

File tree

src/cmi_docx/declarative/document.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -893,16 +893,16 @@ def _pack_table(
893893

894894
if tbl.layout == "autofit":
895895
docx_table.autofit = True
896-
else:
896+
elif tbl.layout == "fixed" or tbl.column_widths is not None:
897897
# column_widths or layout="fixed" both imply fixed layout
898-
if tbl.layout == "fixed" or tbl.column_widths is not None:
899-
docx_table.autofit = False
900-
if tbl.column_widths is not None:
901-
_apply_column_widths(docx_table, tbl.column_widths, num_cols)
898+
docx_table.autofit = False
902899

903900
for row_idx, row in enumerate(filtered_rows):
904901
_pack_table_row(docx_doc, docx_table.rows[row_idx], row, default_comment_author) # ty:ignore[invalid-argument-type] already awaited.
905902

903+
if tbl.layout != "autofit" and tbl.column_widths is not None:
904+
_apply_column_widths(docx_table, tbl.column_widths, num_cols)
905+
906906

907907
def _apply_column_widths(
908908
tbl: docx_table.Table,
@@ -925,8 +925,9 @@ def _apply_column_widths(
925925
f"must match number of columns ({num_cols})"
926926
)
927927
raise ValueError(msg)
928-
for i, width in enumerate(column_widths):
929-
tbl.columns[i].width = shared.Twips(width)
928+
929+
for index, width in enumerate(column_widths):
930+
tbl.columns[index].width = shared.Twips(width)
930931

931932

932933
def _pack_table_row(

src/cmi_docx/declarative/table.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class Table(base.Component):
7676
column_widths: List of column widths in twips (DXA). 1440 twips equals
7777
1 inch; approximately 567 twips equals 1cm. Setting this implies
7878
fixed layout (autofit is disabled automatically).
79-
width: Table width configuration (dict with 'size' and 'type' keys).
80-
borders: Table border configuration.
81-
alignment: Table alignment ('left', 'center', 'right').
82-
indent: Table indent from left margin.
8379
layout: Table layout type. ``"fixed"`` sets fixed layout (autofit
8480
disabled). ``"autofit"`` sets autofit layout and suppresses column
8581
widths even if ``column_widths`` is also provided.
@@ -91,9 +87,5 @@ class Table(base.Component):
9187
| Callable[[], MutableSequence[TableRow | Coroutine[None, None, TableRow]]]
9288
)
9389
column_widths: Sequence[int] | None = None
94-
width: dict[str, int | str] | None = None
95-
borders: dict[str, dict[str, str | int]] | None = None
96-
alignment: str | None = None
97-
indent: int | None = None
9890
layout: Literal["autofit", "fixed"] | None = None
9991
style: str | None = None

0 commit comments

Comments
 (0)