Skip to content

Commit 32c24d6

Browse files
committed
hdr: add Section.different_first_page_header_footr
1 parent 1db743c commit 32c24d6

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

docx/oxml/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
6666

6767
from .shared import CT_DecimalNumber, CT_OnOff, CT_String # noqa
6868
register_element_cls("w:evenAndOddHeaders", CT_OnOff)
69+
register_element_cls("w:titlePg", CT_OnOff)
6970

7071

7172
from .coreprops import CT_CoreProperties # noqa

docx/oxml/section.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class CT_SectPr(BaseOxmlElement):
6969
type = ZeroOrOne("w:type", successors=_tag_seq[3:])
7070
pgSz = ZeroOrOne("w:pgSz", successors=_tag_seq[4:])
7171
pgMar = ZeroOrOne("w:pgMar", successors=_tag_seq[5:])
72+
titlePg = ZeroOrOne("w:titlePg", successors=_tag_seq[14:])
7273
del _tag_seq
7374

7475
def add_footerReference(self, type_, rId):
@@ -310,6 +311,14 @@ def start_type(self, value):
310311
type = self.get_or_add_type()
311312
type.val = value
312313

314+
@property
315+
def titlePg_val(self):
316+
"""Value of `w:titlePg/@val` or |None| if not present"""
317+
titlePg = self.titlePg
318+
if titlePg is None:
319+
return False
320+
return titlePg.val
321+
313322
@property
314323
def top_margin(self):
315324
"""

docx/section.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ def bottom_margin(self):
6161
def bottom_margin(self, value):
6262
self._sectPr.bottom_margin = value
6363

64+
@property
65+
def different_first_page_header_footer(self):
66+
"""True if this section displays a distinct first-page header and footer.
67+
68+
Read/write. The definition of the first-page header and footer are accessed
69+
using :attr:`.first_page_header` and :attr:`.first_page_footer` respectively.
70+
"""
71+
return self._sectPr.titlePg_val
72+
6473
@lazyproperty
6574
def footer(self):
6675
"""|_Footer| object representing default page footer for this section.

features/sct-section.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access and change section properties
44
I need a way to get and set the properties of a section
55

66

7-
@wip
87
Scenario Outline: Section.different_first_page_header_footer getter
98
Given a Section object <with-or-without> a distinct first-page header as section
109
Then section.different_first_page_header_footer is <value>

tests/test_section.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ def section_(self, request):
9292

9393
class DescribeSection(object):
9494

95+
def it_knows_when_it_displays_a_distinct_first_page_header(
96+
self, diff_first_header_get_fixture
97+
):
98+
sectPr, expected_value = diff_first_header_get_fixture
99+
section = Section(sectPr, None)
100+
101+
different_first_page_header_footer = section.different_first_page_header_footer
102+
103+
assert different_first_page_header_footer is expected_value
104+
95105
def it_provides_access_to_its_default_footer(
96106
self, document_part_, _Footer_, footer_
97107
):
@@ -198,6 +208,20 @@ def it_can_change_its_page_margins(self, margins_set_fixture):
198208

199209
# fixtures -------------------------------------------------------
200210

211+
@pytest.fixture(
212+
params=[
213+
("w:sectPr", False),
214+
("w:sectPr/w:titlePg", True),
215+
("w:sectPr/w:titlePg{w:val=0}", False),
216+
("w:sectPr/w:titlePg{w:val=1}", True),
217+
("w:sectPr/w:titlePg{w:val=true}", True),
218+
]
219+
)
220+
def diff_first_header_get_fixture(self, request):
221+
sectPr_cxml, expected_value = request.param
222+
sectPr = element(sectPr_cxml)
223+
return sectPr, expected_value
224+
201225
@pytest.fixture(params=[
202226
('w:sectPr/w:pgMar{w:left=120}', 'left_margin', 76200),
203227
('w:sectPr/w:pgMar{w:right=240}', 'right_margin', 152400),

0 commit comments

Comments
 (0)