Skip to content

Commit 6202ca6

Browse files
author
Steve Canny
committed
sect: add Section.page_width getter
1 parent 9b371a4 commit 6202ca6

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

docx/oxml/section.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CT_PageSz(BaseOxmlElement):
1414
``<w:pgSz>`` element, defining page dimensions and orientation.
1515
"""
1616
w = OptionalAttribute('w:w', ST_TwipsMeasure)
17+
h = OptionalAttribute('w:h', ST_TwipsMeasure)
1718

1819

1920
class CT_SectPr(BaseOxmlElement):
@@ -34,6 +35,17 @@ class CT_SectPr(BaseOxmlElement):
3435
__child_sequence__[__child_sequence__.index('w:pgSz')+1:]
3536
))
3637

38+
@property
39+
def page_height(self):
40+
"""
41+
Value in EMU of the ``h`` attribute of the ``<w:pgSz>`` child
42+
element, or |None| if not present.
43+
"""
44+
pgSz = self.pgSz
45+
if pgSz is None:
46+
return None
47+
return pgSz.h
48+
3749
@property
3850
def page_width(self):
3951
"""

docx/section.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ def __init__(self, sectPr):
1515
super(Section, self).__init__()
1616
self._sectPr = sectPr
1717

18+
@property
19+
def page_height(self):
20+
"""
21+
Total page height used for this section, inclusive of all edge spacing
22+
values such as margins. Page orientation is taken into account, so
23+
for example, its expected value would be ``Inches(8.5)`` for
24+
letter-sized paper when orientation is landscape.
25+
"""
26+
return self._sectPr.page_height
27+
1828
@property
1929
def page_width(self):
2030
"""

features/sct-section-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Feature: Access and change section properties
2929
| NEW_COLUMN | None | NEW_PAGE |
3030

3131

32-
@wip
3332
Scenario: Get section page width
3433
Given a section having known page dimension
3534
Then the reported page width is 8.5 inches

tests/test_section.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,32 @@ def it_knows_its_page_width(self, page_width_get_fixture):
3030
section, expected_page_width = page_width_get_fixture
3131
assert section.page_width == expected_page_width
3232

33+
def it_knows_its_page_height(self, page_height_get_fixture):
34+
section, expected_page_height = page_height_get_fixture
35+
assert section.page_height == expected_page_height
36+
3337
# fixtures -------------------------------------------------------
3438

39+
@pytest.fixture(params=[
40+
(True, 2880, Inches(2)),
41+
(True, None, None),
42+
(False, None, None),
43+
])
44+
def page_height_get_fixture(self, request):
45+
has_pgSz_child, h, expected_page_height = request.param
46+
pgSz_bldr = self.pgSz_bldr(has_pgSz_child, h=h)
47+
sectPr = self.sectPr_bldr(pgSz_bldr).element
48+
section = Section(sectPr)
49+
return section, expected_page_height
50+
3551
@pytest.fixture(params=[
3652
(True, 1440, Inches(1)),
3753
(True, None, None),
3854
(False, None, None),
3955
])
4056
def page_width_get_fixture(self, request):
4157
has_pgSz_child, w, expected_page_width = request.param
42-
pgSz_bldr = self.pgSz_bldr(has_pgSz_child, w)
58+
pgSz_bldr = self.pgSz_bldr(has_pgSz_child, w=w)
4359
sectPr = self.sectPr_bldr(pgSz_bldr).element
4460
section = Section(sectPr)
4561
return section, expected_page_width
@@ -82,12 +98,14 @@ def start_type_set_fixture(self, request):
8298

8399
# fixture components ---------------------------------------------
84100

85-
def pgSz_bldr(self, has_pgSz, w):
101+
def pgSz_bldr(self, has_pgSz, w=None, h=None):
86102
if not has_pgSz:
87103
return None
88104
pgSz_bldr = a_pgSz()
89105
if w is not None:
90106
pgSz_bldr.with_w(w)
107+
if h is not None:
108+
pgSz_bldr.with_h(h)
91109
return pgSz_bldr
92110

93111
def sectPr_bldr(self, *child_bldrs):

0 commit comments

Comments
 (0)