Footnotes are a valuable tool in Microsoft Word that allows you to enhance the content of your documents by providing additional information, references, or citations at the bottom of a page. For example, you can use footnotes to provide in-depth explanations of complex concepts, cite sources to support your arguments, or offer tangential information that might be interesting to your readers. Whether you're working on an academic paper, a book, or any document that requires citations or explanations, footnotes offer a convenient way to maintain a clean and organized layout while presenting supplementary details. In this article, we will explain how to insert or remove footnotes in a Word document in Python using Spire.Doc for Python.
- Insert a Footnote for a Specific Paragraph in Word in Python
- Insert a Footnote for a Specific Text in Word in Python
- Remove Footnotes in a Word Document in 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 command.
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
Insert a Footnote for a Specific Paragraph in Word in Python
You can use the Paragraph.AppendFootnote(FootnoteType.Footnote) method provided by Spire.Doc for Python to easily add a footnote for a specific paragraph. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document using Document.Section[int] property and then get a specific paragraph of the section using Section.Paragraphs[int] property.
- Add a footnote at the end of the paragraph using Paragraph.AppendFootnote(FootnoteType.Footnote) method.
- Set the text content of the footnote, and then set the font and color for the footnote text and the footnote reference mark.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document instance document = Document() # Load a sample Word document document.LoadFromFile("Sample.docx") # Get the first section section = document.Sections[0] # Get a specified paragraph in the section paragraph = section.Paragraphs[3] # Add a footnote at the end of the paragraph footnote = paragraph.AppendFootnote(FootnoteType.Footnote) # Set the text content of the footnote text = footnote.TextBody.AddParagraph().AppendText("The industry code list is available online.") # Set the text font and color text.CharacterFormat.FontName = "Arial" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.get_DarkBlue() # Set the font and color of the footnote reference mark footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.get_DarkCyan() # Save the result document document.SaveToFile("AddFootnoteForParagraph.docx", FileFormat.Docx2016) document.Close()
Insert a Footnote for a Specific Text in Word in Python
To add a footnote for a specific text, you need to find the text in the document, get the location of the text in its owner paragraph, and then insert the footnote after the text. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Find a specific text using Document.FindString() method.
- Get the found text as a single text range using TextSelection.GetAsOneRange() method.
- Get the paragraph where the text range is located using TextRange.OwnerParagraph property.
- Get the index position of the text range in the paragraph using Paragraph.ChildObjects.IndexOf() method.
- Add a footnote to the paragraph using Paragraph.AppendFootnote(FootnoteType.Footnote) method, and then insert the footnote after the specific text using Paragraph.ChildObjects.Insert() method.
- Set the text content of the footnote, and then set the font and color for the footnote text and the footnote reference mark.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document instance document = Document() # Load a sample Word document document.LoadFromFile("Sample.docx") # Find a specific text selection = document.FindString("major players", False, True) # Get the found text as a single text range textRange = selection.GetAsOneRange() # Get the paragraph where the text range is located paragraph = textRange.OwnerParagraph # Get the index position of the text range in the paragraph index = paragraph.ChildObjects.IndexOf(textRange) # Add a footnote to the paragraph footnote = paragraph.AppendFootnote(FootnoteType.Footnote) # Insert the footnote after the text range paragraph.ChildObjects.Insert(index + 1, footnote) # Set the text content of the footnote text = footnote.TextBody.AddParagraph().AppendText("Including suppliers, competitors, and customers") # Set the text font and color text.CharacterFormat.FontName = "Arial" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.get_DarkBlue() # Set the font and color of the footnote reference mark footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.get_DarkGreen() # Save the result document document.SaveToFile("AddFootnoteForText.docx", FileFormat.Docx2016) document.Close()
Remove Footnotes in a Word Document in Python
When the footnotes of a Word document are no longer needed, you can remove them to make the document neater. The detailed steps are as follows.
- Create an object of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section using Document.Sections[int] property.
- Loop through each paragraph in the section to find the footnotes.
- Remove the footnotes using Paragraph.ChildObjects.RemoveAt() method.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document instance document = Document() # Load a sample Word document document.LoadFromFile("AddFootnoteForParagraph.docx") # Get the first section of the document section = document.Sections[0] # Loop through the paragraphs in the section for y in range(section.Paragraphs.Count): para = section.Paragraphs.get_Item(y) index = -1 i = 0 cnt = para.ChildObjects.Count while i < cnt: pBase = para.ChildObjects[i] if isinstance(para.ChildObjects[i], ParagraphBase) else None if isinstance(pBase, Footnote): index = i break i += 1 if index > -1: # Remove the footnotes from the paragraph para.ChildObjects.RemoveAt(index) # Save the result document document.SaveToFile("RemoveFootnotes.docx", FileFormat.Docx) document.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.