Skip to content

Commit c68f667

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

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

docx/header.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11-
from .shared import ElementProxy
11+
from .blkcntnr import BlockItemContainer
1212

1313

14-
class Header(ElementProxy):
14+
class Header(BlockItemContainer):
1515
"""
16-
The default page header of a section.
16+
Proxy for ``<w:hdr>`` element in this section, having primarily a
17+
container role.
1718
"""
18-
19-
__slots__ = ()

docx/oxml/header.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from . import OxmlElement
2+
from .xmlchemy import BaseOxmlElement, ZeroOrMore
3+
4+
5+
class CT_Hdr(BaseOxmlElement):
6+
"""
7+
``<w:hdr>``, the container element for the header content
8+
"""
9+
p = ZeroOrMore('w:p', successors=())
10+
11+
@classmethod
12+
def new(cls):
13+
header_elm = OxmlElement('w:hdr')
14+
return header_elm

docx/section.py

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

99
from collections import Sequence
10+
from .header import Header
1011

1112

1213
class Sections(Sequence):
@@ -37,10 +38,24 @@ class Section(object):
3738
"""
3839
Document section, providing access to section and page setup settings.
3940
"""
41+
_header = None
42+
4043
def __init__(self, sectPr):
4144
super(Section, self).__init__()
4245
self._sectPr = sectPr
4346

47+
@property
48+
def header(self):
49+
if not self._header:
50+
# here's how I did the header element in my previous implementation:
51+
# from .oxml.header import CT_Hdr
52+
# header_elm = CT_Hdr.new()
53+
# not sure what is appropriate here for this first unit test
54+
header_elm = 'pretend element'
55+
self._header = Header(header_elm, self)
56+
57+
return self._header
58+
4459
@property
4560
def bottom_margin(self):
4661
"""

tests/test_section.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
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 (
18+
instance_mock, class_mock
19+
)
1620

1721

1822
class DescribeSections(object):
@@ -61,6 +65,16 @@ def document_elm(self):
6165

6266
class DescribeSection(object):
6367

68+
def it_provides_access_to_its_header(self, header_fixture):
69+
section, Header_, header_ = header_fixture
70+
header = section.header
71+
Header_.assert_called_once_with('pretend element', section)
72+
73+
# access header again, dont instantiate with Header_ again, return same header
74+
header = section.header
75+
assert Header_.call_count == 1
76+
assert header is header_
77+
6478
def it_knows_its_start_type(self, start_type_get_fixture):
6579
section, expected_start_type = start_type_get_fixture
6680
assert section.start_type is expected_start_type
@@ -111,6 +125,22 @@ def it_can_change_its_page_margins(self, margins_set_fixture):
111125

112126
# fixtures -------------------------------------------------------
113127

128+
@pytest.fixture
129+
def Header_(self, request, header_):
130+
return class_mock(
131+
request, 'docx.section.Header',
132+
return_value=header_
133+
)
134+
135+
@pytest.fixture
136+
def header_(self, request):
137+
return instance_mock(request, Header)
138+
139+
@pytest.fixture
140+
def header_fixture(self, Header_, header_):
141+
section = Section(element('w:sectPr'))
142+
return section, Header_, header_
143+
114144
@pytest.fixture(params=[
115145
('w:sectPr/w:pgMar{w:left=120}', 'left_margin', 76200),
116146
('w:sectPr/w:pgMar{w:right=240}', 'right_margin', 152400),

0 commit comments

Comments
 (0)