Skip to content

Commit 3512601

Browse files
ondrej-111scanny
authored andcommitted
hdr: add DocumentPart.add_header_part()
1 parent 2afbbb6 commit 3512601

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docx/parts/document.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from docx.opc.constants import RELATIONSHIP_TYPE as RT
99
from docx.opc.part import XmlPart
1010
from docx.oxml.shape import CT_Inline
11+
from docx.parts.hdrftr import HeaderPart
1112
from docx.parts.numbering import NumberingPart
1213
from docx.parts.settings import SettingsPart
1314
from docx.parts.styles import StylesPart
@@ -26,7 +27,9 @@ class DocumentPart(XmlPart):
2627

2728
def add_header_part(self):
2829
"""Return (header_part, rId) pair for newly-created header part."""
29-
raise NotImplementedError
30+
header_part = HeaderPart.new(self.package)
31+
rId = self.relate_to(header_part, RT.HEADER)
32+
return header_part, rId
3033

3134
@property
3235
def core_properties(self):

docx/parts/hdrftr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@
99

1010
class HeaderPart(XmlPart):
1111
"""Definition of a section header."""
12+
13+
@classmethod
14+
def new(cls, package):
15+
"""Return newly created header part."""
16+
raise NotImplementedError

tests/parts/test_document.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from docx.opc.coreprops import CoreProperties
1212
from docx.package import Package
1313
from docx.parts.document import DocumentPart
14+
from docx.parts.hdrftr import HeaderPart
1415
from docx.parts.image import ImagePart
1516
from docx.parts.numbering import NumberingPart
1617
from docx.parts.settings import SettingsPart
@@ -30,6 +31,18 @@
3031

3132
class DescribeDocumentPart(object):
3233

34+
def it_can_add_a_header_part(self, package_, HeaderPart_, header_part_, relate_to_):
35+
HeaderPart_.new.return_value = header_part_
36+
relate_to_.return_value = "rId7"
37+
document_part = DocumentPart(None, None, None, package_)
38+
39+
header_part, rId = document_part.add_header_part()
40+
41+
HeaderPart_.new.assert_called_once_with(package_)
42+
relate_to_.assert_called_once_with(document_part, header_part_, RT.HEADER)
43+
assert header_part is header_part_
44+
assert rId == "rId7"
45+
3346
def it_can_drop_a_specified_header_part(self, drop_rel_):
3447
document_part = DocumentPart(None, None, None, None)
3548

@@ -277,6 +290,14 @@ def drop_rel_(self, request):
277290
def get_or_add_image_(self, request):
278291
return method_mock(request, DocumentPart, 'get_or_add_image')
279292

293+
@pytest.fixture
294+
def HeaderPart_(self, request):
295+
return class_mock(request, 'docx.parts.document.HeaderPart')
296+
297+
@pytest.fixture
298+
def header_part_(self, request):
299+
return instance_mock(request, HeaderPart)
300+
280301
@pytest.fixture
281302
def image_(self, request):
282303
return instance_mock(request, Image)

0 commit comments

Comments
 (0)