HeaderFooter class

HeaderFooter class

Represents a container for the header or footer text of a section. To learn more, visit the Working with Headers and Footers documentation article.

Remarks

HeaderFooter can contain Paragraph and Table child nodes.

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter of each HeaderFooter.header_footer_type in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word.

When HeaderFooter contains at least one Paragraph, it is no longer considered linked to previous in Microsoft Word.

Inheritance: HeaderFooterStoryCompositeNodeNode

Constructors

NameDescription
HeaderFooter(doc, header_footer_type)Creates a new header or footer of the specified type.

Properties

NameDescription
countGets the number of immediate children of this node.
(Inherited from CompositeNode)
custom_node_idSpecifies custom node identifier.
(Inherited from Node)
documentGets the document to which this node belongs.
(Inherited from Node)
first_childGets the first child of the node.
(Inherited from CompositeNode)
first_paragraphGets the first paragraph in the story.
(Inherited from Story)
has_child_nodesReturns True if this node has any child nodes.
(Inherited from CompositeNode)
header_footer_typeGets the type of this header/footer.
is_compositeReturns True if this node can contain other nodes.
(Inherited from Node)
is_headerTrue if this HeaderFooter object is a header.
is_linked_to_previousTrue if this header or footer is linked to the corresponding header or footer in the previous section.
last_childGets the last child of the node.
(Inherited from CompositeNode)
last_paragraphGets the last paragraph in the story.
(Inherited from Story)
next_siblingGets the node immediately following this node.
(Inherited from Node)
node_typeReturns NodeType.HEADER_FOOTER.
paragraphsGets a collection of paragraphs that are immediate children of the story.
(Inherited from Story)
parent_nodeGets the immediate parent of this node.
(Inherited from Node)
parent_sectionGets the parent section of this story.
previous_siblingGets the node immediately preceding this node.
(Inherited from Node)
rangeReturns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node)
story_typeGets the type of this story.
(Inherited from Story)
tablesGets a collection of tables that are immediate children of the story.
(Inherited from Story)

Methods

NameDescription
accept(visitor)Accepts a visitor.
accept_end(visitor)Accepts a visitor for visiting the end of the header.
accept_start(visitor)Accepts a visitor for visiting the start of the header.
append_child(new_child)Adds the specified node to the end of the list of child nodes for this node.
(Inherited from CompositeNode)
append_paragraph(text)A shortcut method that creates a Paragraph object with optional text and appends it to the end of this object.
(Inherited from Story)
clone(is_clone_children)Creates a duplicate of the node.
(Inherited from Node)
delete_shapes()Deletes all shapes from the text of this story.
(Inherited from Story)
get_ancestor(ancestor_type)Gets the first ancestor of the specified object type.
(Inherited from Node)
get_ancestor(ancestor_type)Gets the first ancestor of the specified NodeType.
(Inherited from Node)
get_child(node_type, index, is_deep)Returns an Nth child node that matches the specified type.
(Inherited from CompositeNode)
get_child_nodes(node_type, is_deep)Returns a live collection of child nodes that match the specified type.
(Inherited from CompositeNode)
get_text()Gets the text of this node and of all its children.
(Inherited from Node)
index_of(child)Returns the index of the specified child node in the child node array.
(Inherited from CompositeNode)
insert_after(new_child, ref_child)Inserts the specified node immediately after the specified reference node.
(Inherited from CompositeNode)
insert_before(new_child, ref_child)Inserts the specified node immediately before the specified reference node.
(Inherited from CompositeNode)
next_pre_order(root_node)Gets next node according to the pre-order tree traversal algorithm.
(Inherited from Node)
node_type_to_string(node_type)A utility method that converts a node type enum value into a user friendly string.
(Inherited from Node)
prepend_child(new_child)Adds the specified node to the beginning of the list of child nodes for this node.
(Inherited from CompositeNode)
previous_pre_order(root_node)Gets the previous node according to the pre-order tree traversal algorithm.
(Inherited from Node)
remove()Removes itself from the parent.
(Inherited from Node)
remove_all_children()Removes all the child nodes of the current node.
(Inherited from CompositeNode)
remove_child(old_child)Removes the specified child node.
(Inherited from CompositeNode)
remove_smart_tags()Removes all SmartTag descendant nodes of the current node.
(Inherited from CompositeNode)
select_nodes(xpath)Selects a list of nodes matching the XPath expression.
(Inherited from CompositeNode)
select_single_node(xpath)Selects the first Node that matches the XPath expression.
(Inherited from CompositeNode)
to_string(save_format)Exports the content of the node into a string in the specified format.
(Inherited from Node)
to_string(save_options)Exports the content of the node into a string using the specified save options.
(Inherited from Node)

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(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")

Shows how to replace text in a document’s footer.

doc = aw.Document(MY_DIR + "Footer.docx")

headers_footers = doc.first_section.headers_footers
footer = headers_footers.get_by_header_footer_type(aw.HeaderFooterType.FOOTER_PRIMARY)

options = aw.replacing.FindReplaceOptions()
options.match_case = False
options.find_whole_words_only = False

current_year = date.today().year
footer.range.replace("(C) 2006 Aspose Pty Ltd.", f"Copyright (C) {current_year} by Aspose Pty Ltd.", options)

doc.save(ARTIFACTS_DIR + "HeaderFooter.replace_text.docx")

See Also