Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f4e820

Browse files
committed
hdr: add DocumentPart.add_footer_part()
1 parent 100654a commit 7f4e820

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

docx/parts/document.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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
11+
from docx.parts.hdrftr import FooterPart, HeaderPart
1212
from docx.parts.numbering import NumberingPart
1313
from docx.parts.settings import SettingsPart
1414
from docx.parts.styles import StylesPart
@@ -27,7 +27,9 @@ class DocumentPart(XmlPart):
2727

2828
def add_footer_part(self):
2929
"""Return (footer_part, rId) pair for newly-created footer part."""
30-
raise NotImplementedError
30+
footer_part = FooterPart.new(self.package)
31+
rId = self.relate_to(footer_part, RT.FOOTER)
32+
return footer_part, rId
3133

3234
def add_header_part(self):
3335
"""Return (header_part, rId) pair for newly-created header part."""

docx/parts/hdrftr.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
class FooterPart(XmlPart):
1515
"""Definition of a section footer."""
1616

17+
@classmethod
18+
def new(cls, package):
19+
"""Return newly created footer part."""
20+
raise NotImplementedError
21+
1722

1823
class HeaderPart(XmlPart):
1924
"""Definition of a section header."""

tests/parts/test_document.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +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
14+
from docx.parts.hdrftr import FooterPart, HeaderPart
1515
from docx.parts.image import ImagePart
1616
from docx.parts.numbering import NumberingPart
1717
from docx.parts.settings import SettingsPart
@@ -29,6 +29,18 @@
2929

3030
class DescribeDocumentPart(object):
3131

32+
def it_can_add_a_footer_part(self, package_, FooterPart_, footer_part_, relate_to_):
33+
FooterPart_.new.return_value = footer_part_
34+
relate_to_.return_value = "rId12"
35+
document_part = DocumentPart(None, None, None, package_)
36+
37+
footer_part, rId = document_part.add_footer_part()
38+
39+
FooterPart_.new.assert_called_once_with(package_)
40+
relate_to_.assert_called_once_with(document_part, footer_part_, RT.FOOTER)
41+
assert footer_part is footer_part_
42+
assert rId == "rId12"
43+
3244
def it_can_add_a_header_part(self, package_, HeaderPart_, header_part_, relate_to_):
3345
HeaderPart_.new.return_value = header_part_
3446
relate_to_.return_value = "rId7"
@@ -296,6 +308,14 @@ def core_properties_(self, request):
296308
def drop_rel_(self, request):
297309
return method_mock(request, DocumentPart, "drop_rel", autospec=True)
298310

311+
@pytest.fixture
312+
def FooterPart_(self, request):
313+
return class_mock(request, 'docx.parts.document.FooterPart')
314+
315+
@pytest.fixture
316+
def footer_part_(self, request):
317+
return instance_mock(request, FooterPart)
318+
299319
@pytest.fixture
300320
def get_or_add_image_(self, request):
301321
return method_mock(request, DocumentPart, 'get_or_add_image')

0 commit comments

Comments
 (0)