Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/docx/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ def tables(self) -> List[Table]:
@property
def _block_width(self) -> Length:
"""A |Length| object specifying the space between margins in last section."""
# No sectPr (some generators): same Letter defaults as None page/margin values.
if not self.sections:
return Emu(Inches(8.5) - Inches(1) - Inches(1))
section = self.sections[-1]
page_width = section.page_width or Inches(8.5)
left_margin = section.left_margin or Inches(1)
Expand Down
13 changes: 12 additions & 1 deletion tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from docx.section import Section, Sections
from docx.settings import Settings
from docx.shape import InlineShape, InlineShapes
from docx.shared import Length
from docx.shared import Inches, Length
from docx.styles.styles import Styles
from docx.table import Table
from docx.text.paragraph import Paragraph
Expand Down Expand Up @@ -286,6 +286,17 @@ def it_determines_block_width_to_help(
assert isinstance(width, Length)
assert width == 3500

def it_uses_default_block_width_when_document_has_no_sections(
self, document: Document, sections_prop_: Mock
):
# No sectPr: same Letter defaults as None page/margin (avoids sections[-1]).
sections_prop_.return_value = []

width = document._block_width

assert isinstance(width, Length)
assert width == Inches(8.5) - Inches(1) - Inches(1)

# -- fixtures --------------------------------------------------------------------------------

@pytest.fixture
Expand Down