X Tutup
Skip to content

Commit 7b5e961

Browse files
author
Steve Canny
committed
sect: add Section.orientation getter
1 parent 212a655 commit 7b5e961

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

docx/oxml/section.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Section-related custom element classes.
55
"""
66

7-
from ..enum.section import WD_SECTION_START
7+
from ..enum.section import WD_ORIENTATION, WD_SECTION_START
88
from .simpletypes import ST_TwipsMeasure
99
from .xmlchemy import BaseOxmlElement, OptionalAttribute, ZeroOrOne
1010

@@ -15,6 +15,9 @@ class CT_PageSz(BaseOxmlElement):
1515
"""
1616
w = OptionalAttribute('w:w', ST_TwipsMeasure)
1717
h = OptionalAttribute('w:h', ST_TwipsMeasure)
18+
orient = OptionalAttribute(
19+
'w:orient', WD_ORIENTATION, default=WD_ORIENTATION.PORTRAIT
20+
)
1821

1922

2023
class CT_SectPr(BaseOxmlElement):
@@ -35,6 +38,18 @@ class CT_SectPr(BaseOxmlElement):
3538
__child_sequence__[__child_sequence__.index('w:pgSz')+1:]
3639
))
3740

41+
@property
42+
def orientation(self):
43+
"""
44+
The member of the ``WD_ORIENTATION`` enumeration corresponding to the
45+
value of the ``orient`` attribute of the ``<w:pgSz>`` child element,
46+
or ``WD_ORIENTATION.PORTRAIT`` if not present.
47+
"""
48+
pgSz = self.pgSz
49+
if pgSz is None:
50+
return WD_ORIENTATION.PORTRAIT
51+
return pgSz.orient
52+
3853
@property
3954
def page_height(self):
4055
"""

docx/section.py

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

18+
@property
19+
def orientation(self):
20+
"""
21+
Page orientation for this section, one of ``WD_ORIENT.PORTRAIT`` or
22+
``WD_ORIENT.LANDSCAPE``.
23+
"""
24+
return self._sectPr.orientation
25+
1826
@property
1927
def page_height(self):
2028
"""

features/sct-section-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ Feature: Access and change section properties
4343
And the reported page height is 8.5 inches
4444

4545

46-
@wip
4746
Scenario Outline: Get section orientation
4847
Given a section known to have <orientation> orientation
4948
Then the reported page orientation is <reported-orientation>

tests/test_section.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from docx.enum.section import WD_SECTION
11+
from docx.enum.section import WD_ORIENT, WD_SECTION
1212
from docx.section import Section
1313
from docx.shared import Inches
1414

@@ -43,8 +43,25 @@ def it_can_change_its_page_height(self, page_height_set_fixture):
4343
section.page_height = new_page_height
4444
assert section._sectPr.xml == expected_xml
4545

46+
def it_knows_its_page_orientation(self, orientation_get_fixture):
47+
section, expected_orientation = orientation_get_fixture
48+
assert section.orientation is expected_orientation
49+
4650
# fixtures -------------------------------------------------------
4751

52+
@pytest.fixture(params=[
53+
(True, 'landscape', WD_ORIENT.LANDSCAPE),
54+
(True, 'portrait', WD_ORIENT.PORTRAIT),
55+
(True, None, WD_ORIENT.PORTRAIT),
56+
(False, None, WD_ORIENT.PORTRAIT),
57+
])
58+
def orientation_get_fixture(self, request):
59+
has_pgSz_child, orient, expected_orientation = request.param
60+
pgSz_bldr = self.pgSz_bldr(has_pgSz_child, orient=orient)
61+
sectPr = self.sectPr_bldr(pgSz_bldr).element
62+
section = Section(sectPr)
63+
return section, expected_orientation
64+
4865
@pytest.fixture(params=[
4966
(True, 2880, Inches(2)),
5067
(True, None, None),
@@ -135,14 +152,16 @@ def start_type_set_fixture(self, request):
135152

136153
# fixture components ---------------------------------------------
137154

138-
def pgSz_bldr(self, has_pgSz=True, w=None, h=None):
155+
def pgSz_bldr(self, has_pgSz=True, w=None, h=None, orient=None):
139156
if not has_pgSz:
140157
return None
141158
pgSz_bldr = a_pgSz()
142159
if w is not None:
143160
pgSz_bldr.with_w(w)
144161
if h is not None:
145162
pgSz_bldr.with_h(h)
163+
if orient is not None:
164+
pgSz_bldr.with_orient(orient)
146165
return pgSz_bldr
147166

148167
def sectPr_bldr(self, *child_bldrs):

0 commit comments

Comments
 (0)
X Tutup