add_footnote_to_document
Insert a footnote into a specific paragraph of a Word document by specifying the filename, paragraph index, and footnote text.
Instructions
Add a footnote to a specific paragraph in a Word document.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| footnote_text | Yes | ||
| paragraph_index | Yes |
Input Schema (JSON Schema)
{
"properties": {
"filename": {
"title": "Filename",
"type": "string"
},
"footnote_text": {
"title": "Footnote Text",
"type": "string"
},
"paragraph_index": {
"title": "Paragraph Index",
"type": "integer"
}
},
"required": [
"filename",
"paragraph_index",
"footnote_text"
],
"type": "object"
}
Implementation Reference
- The primary handler function implementing the logic to add a footnote to a specific paragraph. Includes parameter validation, file checks, attempts native python-docx footnote addition with fallback to manual superscript and footnote section creation.async def add_footnote_to_document(filename: str, paragraph_index: int, footnote_text: str) -> str: """Add a footnote to a specific paragraph in a Word document. Args: filename: Path to the Word document paragraph_index: Index of the paragraph to add footnote to (0-based) footnote_text: Text content of the footnote """ filename = ensure_docx_extension(filename) # Ensure paragraph_index is an integer try: paragraph_index = int(paragraph_index) except (ValueError, TypeError): return "Invalid parameter: paragraph_index must be an integer" if not os.path.exists(filename): return f"Document {filename} does not exist" # Check if file is writeable is_writeable, error_message = check_file_writeable(filename) if not is_writeable: return f"Cannot modify document: {error_message}. Consider creating a copy first." try: doc = Document(filename) # Validate paragraph index if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs): return f"Invalid paragraph index. Document has {len(doc.paragraphs)} paragraphs (0-{len(doc.paragraphs)-1})." paragraph = doc.paragraphs[paragraph_index] # In python-docx, we'd use paragraph.add_footnote(), but we'll use a more robust approach try: footnote = paragraph.add_run() footnote.text = "" # Create the footnote reference reference = footnote.add_footnote(footnote_text) doc.save(filename) return f"Footnote added to paragraph {paragraph_index} in {filename}" except AttributeError: # Fall back to a simpler approach if direct footnote addition fails last_run = paragraph.add_run() last_run.text = "¹" # Unicode superscript 1 last_run.font.superscript = True # Add a footnote section at the end if it doesn't exist found_footnote_section = False for p in doc.paragraphs: if p.text.startswith("Footnotes:"): found_footnote_section = True break if not found_footnote_section: doc.add_paragraph("\n").add_run() doc.add_paragraph("Footnotes:").bold = True # Add footnote text footnote_para = doc.add_paragraph("¹ " + footnote_text) footnote_para.style = "Footnote Text" if "Footnote Text" in doc.styles else "Normal" doc.save(filename) return f"Footnote added to paragraph {paragraph_index} in {filename} (simplified approach)" except Exception as e: return f"Failed to add footnote: {str(e)}"
- word_document_server/main.py:298-301 (registration)MCP tool registration using FastMCP @mcp.tool() decorator. Defines the tool schema via function signature and docstring, delegates execution to the footnote_tools module.def add_footnote_to_document(filename: str, paragraph_index: int, footnote_text: str): """Add a footnote to a specific paragraph in a Word document.""" return footnote_tools.add_footnote_to_document(filename, paragraph_index, footnote_text)
- Export of the add_footnote_to_document function from footnote_tools.py, making it available for import in main.py and other modules.# Footnote tools from word_document_server.tools.footnote_tools import ( add_footnote_to_document, add_endnote_to_document, convert_footnotes_to_endnotes_in_document, customize_footnote_style )