Remove Header and Footer in Word using Python

This article guides how to remove header and footer in Word using Python. It has details to set the environment for working with this feature, a list of steps to develop the application, and a sample code demonstrating how to delete header and footer in Word using Python. You will learn to remove all the headers and footers with a single API call or delete the selected headers or footers only as per the requirements.

  1. Set the IDE for working with Aspose.Words for Python via .NET
  2. Load the Word file using the Document object having multiple headers and footers
  3. Iterate through each section of the loaded Word file using the section collection
  4. Set the type of the object in the collection using the as_section() method
  5. Access the desired types of header/footer from the headers_footers collection in a section
  6. Remove the header or footer from the document before saving the final document

These steps describe how to remove header and footer in Word using Python. Start the process by loading the source Word file having various types of headers and footers in it followed by parsing all the sections of the document. In each iteration, either call the clear() method from the headers_footers collection or access the target item using the HeaderFooterType and delete it using the remove() method.

The above code has shown how to remove footer in Word using Python. There are three types of headers and footers in a section that can be deleted individually using the HeaderFooterType enumerator contains values HEADER_FIRST and FOOTER_FIRST for first-page different option, HEADER_PRIMARY and FOOTER_PRIMARY for one type of header/footer throughout the document or odd page headers/footers, and HEADER_EVEN and FOOTER_EVEN for accessing the even page headers and footers. Call the remove() method from the HeaderFooter class to delete the respective item.

This article has taught us to remove footer in Word using Python or a header if required. To remove blank pages from the Word file, refer to the article on how to remove blank page in Word using Python.

 English