🌐 AI搜索 & 代理 主页
Skip to content

Commit 100654a

Browse files
committed
hdr: add _Footer._add_definition()
1 parent e6c64a0 commit 100654a

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

docx/oxml/section.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,22 @@ class CT_SectPr(BaseOxmlElement):
6565
'w:docGrid', 'w:printerSettings', 'w:sectPrChange',
6666
)
6767
headerReference = ZeroOrMore("w:headerReference", successors=_tag_seq)
68+
footerReference = ZeroOrMore("w:footerReference", successors=_tag_seq)
6869
type = ZeroOrOne("w:type", successors=_tag_seq[3:])
6970
pgSz = ZeroOrOne("w:pgSz", successors=_tag_seq[4:])
7071
pgMar = ZeroOrOne("w:pgMar", successors=_tag_seq[5:])
7172
del _tag_seq
7273

74+
def add_footerReference(self, type_, rId):
75+
"""Return newly added CT_HdrFtrRef element of *type_* with *rId*.
76+
77+
The element tag is `w:footerReference`.
78+
"""
79+
footerReference = self._add_footerReference()
80+
footerReference.type_ = type_
81+
footerReference.rId = rId
82+
return footerReference
83+
7384
def add_headerReference(self, type_, rId):
7485
"""Return newly added CT_HdrFtrRef element of *type_* with *rId*.
7586

docx/parts/document.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class DocumentPart(XmlPart):
2525
objects provides access to this part object for that purpose.
2626
"""
2727

28+
def add_footer_part(self):
29+
"""Return (footer_part, rId) pair for newly-created footer part."""
30+
raise NotImplementedError
31+
2832
def add_header_part(self):
2933
"""Return (header_part, rId) pair for newly-created header part."""
3034
header_part = HeaderPart.new(self.package)

docx/parts/hdrftr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from docx.oxml import parse_xml
1212

1313

14+
class FooterPart(XmlPart):
15+
"""Definition of a section footer."""
16+
17+
1418
class HeaderPart(XmlPart):
1519
"""Definition of a section header."""
1620

docx/section.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,12 @@ def _has_definition(self):
260260
class _Footer(_BaseHeaderFooter):
261261
"""Page footer."""
262262

263+
def _add_definition(self):
264+
"""Return newly-added footer part."""
265+
footer_part, rId = self._document_part.add_footer_part()
266+
self._sectPr.add_footerReference(WD_HEADER_FOOTER.PRIMARY, rId)
267+
return footer_part
268+
263269
def _drop_definition(self):
264270
"""Remove footer definition (footer part) associated with this section."""
265271
rId = self._sectPr.remove_footerReference(WD_HEADER_FOOTER.PRIMARY)

tests/test_section.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from docx.enum.section import WD_ORIENT, WD_SECTION
1010
from docx.parts.document import DocumentPart
11-
from docx.parts.hdrftr import HeaderPart
11+
from docx.parts.hdrftr import FooterPart, HeaderPart
1212
from docx.section import _BaseHeaderFooter, _Footer, _Header, Section, Sections
1313
from docx.shared import Inches
1414

@@ -423,6 +423,19 @@ def _has_definition_prop_(self, request):
423423

424424
class Describe_Footer(object):
425425

426+
def it_can_add_a_footer_part_to_help(self, document_part_, footer_part_):
427+
sectPr = element("w:sectPr{r:a=b}")
428+
document_part_.add_footer_part.return_value = footer_part_, "rId3"
429+
footer = _Footer(sectPr, document_part_)
430+
431+
footer_part = footer._add_definition()
432+
433+
document_part_.add_footer_part.assert_called_once_with()
434+
assert sectPr.xml == xml(
435+
"w:sectPr{r:a=b}/w:footerReference{w:type=default,r:id=rId3}"
436+
)
437+
assert footer_part is footer_part_
438+
426439
def it_can_drop_the_related_footer_part_to_help(self, document_part_):
427440
sectPr = element("w:sectPr{r:a=b}/w:footerReference{w:type=default,r:id=rId42}")
428441
footer = _Footer(sectPr, document_part_)
@@ -458,6 +471,10 @@ def has_definition_fixture(self, request):
458471
def document_part_(self, request):
459472
return instance_mock(request, DocumentPart)
460473

474+
@pytest.fixture
475+
def footer_part_(self, request):
476+
return instance_mock(request, FooterPart)
477+
461478

462479
class Describe_Header(object):
463480

0 commit comments

Comments
 (0)