HeaderFooterCollection class

HeaderFooterCollection class

Provides typed access to HeaderFooter nodes of a Section. To learn more, visit the Working with Headers and Footers documentation article.

Remarks

There can be maximum of one HeaderFooter

of eachHeaderFooterType per Section. HeaderFooter objects can occur in any order in the collection.

Inheritance: HeaderFooterCollectionNodeCollection

Indexers

NameDescription
__getitem__(index)Retrieves a HeaderFooter at the given index.

Properties

NameDescription
countGets the number of nodes in the collection.
(Inherited from NodeCollection)
footer_evenRetrieves a footer for even numbered pages.
footer_firstRetrieves a footer for the first page of the section.
footer_primaryRetrieves a primary footer, also used for odd numbered pages.
header_evenRetrieves a header for even numbered pages.
header_firstRetrieves a header for the first page of the section.
header_primaryRetrieves a primary header, also used for odd numbered pages.

Methods

NameDescription
add(node)Adds a node to the end of the collection.
(Inherited from NodeCollection)
clear()Removes all nodes from this collection and from the document.
(Inherited from NodeCollection)
contains(node)Determines whether a node is in the collection.
(Inherited from NodeCollection)
get_by_header_footer_type(header_footer_type)Retrieves a HeaderFooter of the specified type.
index_of(node)Returns the zero-based index of the specified node.
(Inherited from NodeCollection)
insert(index, node)Inserts a node into the collection at the specified index.
(Inherited from NodeCollection)
link_to_previous(is_link_to_previous)Links or unlinks all headers and footers to the corresponding headers and footers in the previous section.
link_to_previous(header_footer_type, is_link_to_previous)Links or unlinks the specified header or footer to the corresponding header or footer in the previous section.
remove(node)Removes the node from the collection and from the document.
(Inherited from NodeCollection)
remove_at(index)Removes the node at the specified index from the collection and from the document.
(Inherited from NodeCollection)
to_array()Copies all HeaderFooter s from the collection to a new array of HeaderFooter s.

Examples

Shows how to create a header and a footer.

doc = aw.Document()
# Create a header and append a paragraph to it. The text in that paragraph
# will appear at the top of every page of this section, above the main body text.
header = aw.HeaderFooter(doc, aw.HeaderFooterType.HEADER_PRIMARY)
doc.first_section.headers_footers.add(header)
para = header.append_paragraph('My header.')
self.assertTrue(header.is_header)
self.assertTrue(para.is_end_of_header_footer)
# Create a footer and append a paragraph to it. The text in that paragraph
# will appear at the bottom of every page of this section, below the main body text.
footer = aw.HeaderFooter(doc, aw.HeaderFooterType.FOOTER_PRIMARY)
doc.first_section.headers_footers.add(footer)
para = footer.append_paragraph('My footer.')
self.assertFalse(footer.is_header)
self.assertTrue(para.is_end_of_header_footer)
self.assertEqual(footer, para.parent_story)
self.assertEqual(footer.parent_section, para.parent_section)
self.assertEqual(footer.parent_section, header.parent_section)
doc.save(file_name=ARTIFACTS_DIR + 'HeaderFooter.Create.docx')

Shows how to delete all footers from a document.

doc = aw.Document(MY_DIR + 'Header and footer types.docx')
# Iterate through each section and remove footers of every kind.
for section in doc:
    section = section.as_section()
    # There are three kinds of footer and header types.
    # 1 -  The "First" header/footer, which only appears on the first page of a section.
    footer = section.headers_footers[aw.HeaderFooterType.FOOTER_FIRST]
    if footer is not None:
        footer.remove()
    # 2 -  The "Primary" header/footer, which appears on odd pages.
    footer = section.headers_footers[aw.HeaderFooterType.FOOTER_PRIMARY]
    if footer is not None:
        footer.remove()
    # 3 -  The "Even" header/footer, which appears on even pages.
    footer = section.headers_footers[aw.HeaderFooterType.FOOTER_EVEN]
    if footer is not None:
        footer.remove()
    self.assertEqual(0, len([node for node in section.headers_footers if not node.as_header_footer().is_header]))
doc.save(ARTIFACTS_DIR + 'HeaderFooter.remove_footers.docx')

See Also