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.
Steps to Remove Header and Footer in Word using Python
- Set the IDE for working with Aspose.Words for Python via .NET
- Load the Word file using the Document object having multiple headers and footers
- Iterate through each section of the loaded Word file using the section collection
- Set the type of the object in the collection using the as_section() method
- Access the desired types of header/footer from the headers_footers collection in a section
- 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.
Code to Delete Header and Footer in Word using Python
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.