Knowing how to remove headers or footers in Word is an essential skill as there may be times you need to change the formatting of your document or collaborate with others who do not need the headers or footers. In this article, you will learn how to remove headers or footers in Word in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Remove Headers in a Word Document in Python
Spire.Doc for Python supports getting different headers in the first pages, odd pages, and even pages, and then delete all of them through the HeaderFooter.ChildObjects.Clear() method. The following are the detailed steps:
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Iterate through all paragraphs in the section, and then all child objects in each paragraph.
- Get the headers for the first, odd, and even pages using Section.HeadersFooters[hfType: HeaderFooterType] property, and then delete them using HeaderFooter.ChildObjects.Clear() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * inputFile = "HeaderFooter.docx" outputFile = "RemoveHeaders.docx" # Create a Document instance doc = Document() # Load a Word document doc.LoadFromFile(inputFile) # Get the first section section = doc.Sections[0] # Iterate through all paragraphs in the section for i in range(section.Paragraphs.Count): para = section.Paragraphs.get_Item(i) # Iterate through all child objects in each paragraph for j in range(para.ChildObjects.Count): obj = para.ChildObjects.get_Item(j) # Delete header in the first page header = None header = section.HeadersFooters[HeaderFooterType.HeaderFirstPage] if header is not None: header.ChildObjects.Clear() # Delete headers in the odd pages header = section.HeadersFooters[HeaderFooterType.HeaderOdd] if header is not None: header.ChildObjects.Clear() # Delete headers in the even pages header = section.HeadersFooters[HeaderFooterType.HeaderEven] if header is not None: header.ChildObjects.Clear() # Save the result document doc.SaveToFile(outputFile, FileFormat.Docx) doc.Close()
Remove Footers in a Word Document in Python
Deleting footers is similar to that of deleting headers, you can also get the footers on different pages first and then delete them at once. The following are the detailed steps:
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Iterate through all paragraphs in the section, and then all child objects in each paragraph.
- Get the footers for the first, odd, and even pages using Section.HeadersFooters[hfType: HeaderFooterType] property, and then delete them using HeaderFooter.ChildObjects.Clear() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * inputFile = "HeaderFooter.docx" outputFile = "RemoveFooters.docx" # Create a Document instance doc = Document() # Load a Word document doc.LoadFromFile(inputFile) # Get the first section section = doc.Sections[0] # Iterate through all paragraphs in the section for i in range(section.Paragraphs.Count): para = section.Paragraphs.get_Item(i) # Iterate through all child objects in each paragraph for j in range(para.ChildObjects.Count): obj = para.ChildObjects.get_Item(j) # Delete footer in the first page footer = None footer = section.HeadersFooters[HeaderFooterType.FooterFirstPage] if footer is not None: footer.ChildObjects.Clear() # Delete footers in the odd pages footer = section.HeadersFooters[HeaderFooterType.FooterOdd] if footer is not None: footer.ChildObjects.Clear() # Delete footers in the even pages footer = section.HeadersFooters[HeaderFooterType.FooterEven] if footer is not None: footer.ChildObjects.Clear() # Save the result document doc.SaveToFile(outputFile, FileFormat.Docx) doc.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.