Skip to content

Commit 334af24

Browse files
eupharisSteve Canny
authored and
Steve Canny
committed
hdr: add Section.header
1 parent bbbc287 commit 334af24

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docx/section.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
from collections import Sequence
1010

11+
from .header import Header
12+
from .shared import lazyproperty
13+
1114

1215
class Sections(Sequence):
1316
"""
@@ -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
91+
is present or not. The header itself is added, updated, or removed
92+
using the returned object.
93+
"""
94+
return Header(self._sectPr)
95+
8396
@property
8497
def header_distance(self):
8598
"""

features/sct-access-header.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Feature: Access section headers and footers
33
As a developer using python-docx
44
I need access to the section headers and footers
55

6-
@wip
6+
77
Scenario: Access default header of section
88
Given a section
99
Then section.header is a Header object

tests/test_section.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import pytest
1010

1111
from docx.enum.section import WD_ORIENT, WD_SECTION
12+
from docx.header import Header
1213
from docx.section import Section, Sections
1314
from docx.shared import Inches
1415

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

1719

1820
class DescribeSections(object):
@@ -109,8 +111,20 @@ def it_can_change_its_page_margins(self, margins_set_fixture):
109111
setattr(section, margin_prop_name, new_value)
110112
assert section._sectPr.xml == expected_xml
111113

114+
def it_provides_access_to_its_header(self, header_fixture):
115+
section, Header_, sectPr, header_ = header_fixture
116+
header = section.header
117+
Header_.assert_called_once_with(sectPr)
118+
assert header is header_
119+
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,15 @@ 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, header_):
269+
return class_mock(
270+
request, 'docx.section.Header', return_value=header_
271+
)
272+
273+
@pytest.fixture
274+
def header_(self, request):
275+
return instance_mock(request, Header)

0 commit comments

Comments
 (0)