Section class

Section class

Represents a single section in a document. To learn more, visit the Working with Sections documentation article.

Section can have one Body and maximum one HeaderFooter of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section.

A minimal valid section needs to have Body with one Paragraph.

Each section has its own set of properties that specify page size, orientation, margins etc.

You can create a copy of a section using Node.clone(). The copy can be inserted into the same or different document.

To add, insert or remove a whole section including section break and section properties use methods of the Document.sections object.

To copy and insert just content of the section excluding the section break and section properties use Section.append_content() and Section.prepend_content() methods.

Inheritance: SectionCompositeNodeNode

Constructors

Name Description
Section(doc) Initializes a new instance of the Section class.

Properties

Name Description
body Returns the Body child node of the section.
child_nodes Gets all immediate child nodes of this node.
(Inherited from CompositeNode)
count Gets the number of immediate children of this node.
(Inherited from CompositeNode)
custom_node_id Specifies custom node identifier.
(Inherited from Node)
document Gets the document to which this node belongs.
(Inherited from Node)
first_child Gets the first child of the node.
(Inherited from CompositeNode)
has_child_nodes Returns True if this node has any child nodes.
(Inherited from CompositeNode)
headers_footers Provides access to the headers and footers nodes of the section.
is_composite Returns True if this node can contain other nodes.
(Inherited from Node)
last_child Gets the last child of the node.
(Inherited from CompositeNode)
next_sibling Gets the node immediately following this node.
(Inherited from Node)
node_type Returns NodeType.SECTION.
page_setup Returns an object that represents page setup and section properties.
parent_node Gets the immediate parent of this node.
(Inherited from Node)
previous_sibling Gets the node immediately preceding this node.
(Inherited from Node)
protected_for_forms True if the section is protected for forms. When a section is protected for forms, users can select and modify text only in form fields in Microsoft Word.
range Returns a Range object that represents the portion of a document that is contained in this node.
(Inherited from Node)

Methods

Name Description
accept(visitor) Accepts a visitor.
append_child(new_child) Adds the specified node to the end of the list of child nodes for this node.
(Inherited from CompositeNode)
append_content(source_section) Inserts a copy of content of the source section at the end of this section.
clear_content() Clears the section.
clear_headers_footers() Clears the headers and footers of this section.
clone() Creates a duplicate of this section.
clone(is_clone_children) Creates a duplicate of this section.
delete_header_footer_shapes() Deletes all shapes (drawing objects) from the headers and footers of this section.
ensure_minimum() Ensures that the section has Section.body with one Paragraph.
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)
prepend_content(source_section) Inserts a copy of content of the source section at the beginning of this section.
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 construct an Aspose.Words document by hand.

doc = aw.Document()

# A blank document contains one section, one body and one paragraph.
# Call the "remove_all_children" method to remove all those nodes,
# and end up with a document node with no children.
doc.remove_all_children()

# This document now has no composite child nodes that we can add content to.
# If we wish to edit it, we will need to repopulate its node collection.
# First, create a new section, and then append it as a child to the root document node.
section = aw.Section(doc)
doc.append_child(section)

# Set some page setup properties for the section.
section.page_setup.section_start = aw.SectionStart.NEW_PAGE
section.page_setup.paper_size = aw.PaperSize.LETTER

# A section needs a body, which will contain and display all its contents
# on the page between the section's header and footer.
body = aw.Body(doc)
section.append_child(body)

# Create a paragraph, set some formatting properties, and then append it as a child to the body.
para = aw.Paragraph(doc)

para.paragraph_format.style_name = "Heading 1"
para.paragraph_format.alignment = aw.ParagraphAlignment.CENTER

body.append_child(para)

# Finally, add some content to do the document. Create a run,
# set its appearance and contents, and then append it as a child to the paragraph.
run = aw.Run(doc)
run.text = "Hello World!"
run.font.color = drawing.Color.red
para.append_child(run)

self.assertEqual("Hello World!", doc.get_text().strip())

doc.save(ARTIFACTS_DIR + "Section.create_manually.docx")

See Also