Skip to content

Commit 3598440

Browse files
committed
hdr: add Section.header
1 parent bbbc287 commit 3598440

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

docx/header.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
from __future__ import (
88
absolute_import, division, print_function, unicode_literals
99
)
10-
1110
from .shared import ElementProxy
1211

1312

1413
class Header(ElementProxy):
1514
"""
16-
The default page header of a section.
15+
Proxy for ``<w:hdr>`` element in this section, having primarily a
16+
container role.
1717
"""
1818

1919
__slots__ = ()

docx/section.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from __future__ import absolute_import, print_function, unicode_literals
88

99
from collections import Sequence
10+
from .shared import lazyproperty
11+
from .header import Header
1012

1113

1214
class Sections(Sequence):
@@ -37,6 +39,7 @@ class Section(object):
3739
"""
3840
Document section, providing access to section and page setup settings.
3941
"""
42+
4043
def __init__(self, sectPr):
4144
super(Section, self).__init__()
4245
self._sectPr = sectPr
@@ -80,6 +83,16 @@ def gutter(self):
8083
def gutter(self, value):
8184
self._sectPr.gutter = value
8285

86+
@lazyproperty
87+
def header(self):
88+
"""
89+
Return the |Header| object representing the default header for this
90+
section. A |Header| object is always returned, whether such a header is
91+
present or not. The header itself is added, updated, or removed using the
92+
returned object.
93+
"""
94+
return Header(self._sectPr)
95+
8396
@property
8497
def header_distance(self):
8598
"""

tests/test_section.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from docx.enum.section import WD_ORIENT, WD_SECTION
1212
from docx.section import Section, Sections
1313
from docx.shared import Inches
14+
from docx.header import Header
1415

1516
from .unitutil.cxml import element, xml
17+
from .unitutil.mock import instance_mock, class_mock
1618

1719

1820
class DescribeSections(object):
@@ -61,6 +63,12 @@ def document_elm(self):
6163

6264
class DescribeSection(object):
6365

66+
def it_provides_access_to_its_header(self, header_fixture):
67+
section, Header_, sectPr, header_ = header_fixture
68+
header = section.header
69+
Header_.assert_called_once_with(sectPr)
70+
assert header is header_
71+
6472
def it_knows_its_start_type(self, start_type_get_fixture):
6573
section, expected_start_type = start_type_get_fixture
6674
assert section.start_type is expected_start_type
@@ -111,6 +119,12 @@ def it_can_change_its_page_margins(self, margins_set_fixture):
111119

112120
# fixtures -------------------------------------------------------
113121

122+
@pytest.fixture
123+
def header_fixture(self, Header_, header_):
124+
sectPr = element('w:sectPr')
125+
section = Section(sectPr)
126+
return section, Header_, sectPr, header_
127+
114128
@pytest.fixture(params=[
115129
('w:sectPr/w:pgMar{w:left=120}', 'left_margin', 76200),
116130
('w:sectPr/w:pgMar{w:right=240}', 'right_margin', 152400),
@@ -247,3 +261,16 @@ def start_type_set_fixture(self, request):
247261
section = Section(element(initial_cxml))
248262
expected_xml = xml(expected_cxml)
249263
return section, new_start_type, expected_xml
264+
265+
# fixture components ---------------------------------------------
266+
267+
@pytest.fixture
268+
def header_(self, request):
269+
return instance_mock(request, Header)
270+
271+
@pytest.fixture
272+
def Header_(self, request, header_):
273+
return class_mock(
274+
request, 'docx.section.Header',
275+
return_value=header_
276+
)

0 commit comments

Comments
 (0)