Skip to content

1.2.0: _block_width treats a zero page margin as 1 inch, so tables are too narrow #1638

Open

Description

@Jamalofski

Since 1.2.0, Document._block_width falls back to 1 inch when a margin is falsy:

left_margin = section.left_margin or Inches(1)
right_margin = section.right_margin or Inches(1)

A margin explicitly set to 0 is falsy, so it counts as 1 inch. add_table() then computes the grid (w:tblGrid/w:gridCol) one inch narrower per zero margin. In 1.1.2 the width was page_width - left_margin - right_margin, and the table spanned the full page.

import docx
from docx.shared import Mm

doc = docx.Document()
section = doc.sections[-1]
section.page_width, section.page_height = Mm(210), Mm(297)
section.left_margin = section.right_margin = 0
table = doc.add_table(rows=1, cols=2)
print(docx.__version__, [col.width for col in table.columns], "page", section.page_width)
1.1.2 [3780155, 3780155] page 7560310
1.2.0 [2865755, 2865755] page 7560310

Word lays the table out from the cell widths and looks fine, but LibreOffice follows the grid and cuts text off. We hit it through pdf2docx, which copies the PDF's margins (often 0) into the section.

Suggested fix: fall back only when the value is missing, e.g. Inches(1) if section.left_margin is None else section.left_margin (same for right_margin and page_width).

Activity

  1. ub3dqy commented on Nov 3, 2026

    @ub3dqy

    Your None-only fallback suggestion checks out. I have a patch ready with zero/missing-margin and saved-document regression tests. On Windows/Python 3.11, 1,649 unit tests and 650 acceptance scenarios pass. I haven't verified LibreOffice rendering.

    Would $20 in USDC on Base for the tested patch and a PR work for you?

    Dima Mak / ub3dqy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions