customize_footnote_style
Modify footnote numbering format, starting number, and font style in Microsoft Word documents to meet specific formatting requirements.
Instructions
Customize footnote numbering and formatting in a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| numbering_format | No | 1, 2, 3 | |
| start_number | No | ||
| font_name | No | ||
| font_size | No |
Implementation Reference
- Primary asynchronous handler for the customize_footnote_style MCP tool. Loads the document, customizes footnote text style, finds footnote references, generates custom numbering symbols, applies formatting via helper functions, and saves the document.async def customize_footnote_style(filename: str, numbering_format: str = "1, 2, 3", start_number: int = 1, font_name: Optional[str] = None, font_size: Optional[int] = None) -> str: """Customize footnote numbering and formatting in a Word document. Args: filename: Path to the Word document numbering_format: Format for footnote numbers (e.g., "1, 2, 3", "i, ii, iii", "a, b, c") start_number: Number to start footnote numbering from font_name: Optional font name for footnotes font_size: Optional font size for footnotes (in points) """ filename = ensure_docx_extension(filename) 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) # Create or get footnote style footnote_style_name = "Footnote Text" footnote_style = None try: footnote_style = doc.styles[footnote_style_name] except KeyError: # Create the style if it doesn't exist footnote_style = doc.styles.add_style(footnote_style_name, WD_STYLE_TYPE.PARAGRAPH) # Apply formatting to footnote style if footnote_style: if font_name: footnote_style.font.name = font_name if font_size: footnote_style.font.size = Pt(font_size) # Find all existing footnote references footnote_refs = find_footnote_references(doc) # Generate format symbols for the specified numbering format format_symbols = get_format_symbols(numbering_format, len(footnote_refs) + start_number) # Apply custom formatting to footnotes count = customize_footnote_formatting(doc, footnote_refs, format_symbols, start_number, footnote_style) # Save the document doc.save(filename) return f"Footnote style and numbering customized in {filename}" except Exception as e: return f"Failed to customize footnote style: {str(e)}"
- word_document_server/main.py:328-335 (registration)Registers the customize_footnote_style tool with the FastMCP server using the @mcp.tool() decorator. This wrapper delegates to the implementation in footnote_tools.py and defines the tool schema via its signature and docstring.@mcp.tool() def customize_footnote_style(filename: str, numbering_format: str = "1, 2, 3", start_number: int = 1, font_name: str = None, font_size: int = None): """Customize footnote numbering and formatting in a Word document.""" return footnote_tools.customize_footnote_style( filename, numbering_format, start_number, font_name, font_size )
- Core helper function called by the tool handler to apply custom numbering symbols and styles to found footnote references.def customize_footnote_formatting(doc, footnote_refs, format_symbols, start_number, footnote_style): """Apply custom formatting to footnotes.""" count = 0 for i, ref in enumerate(footnote_refs): if i < len(format_symbols): # Update the footnote reference text ref['run'].text = format_symbols[i] ref['run'].font.superscript = True # Apply style if available if footnote_style: try: ref['paragraph'].style = footnote_style except: pass count += 1 return count
- Helper function to locate all superscript runs likely to be footnote references in the document.def find_footnote_references(doc): """Find all footnote references in the document.""" footnote_refs = [] for para_idx, para in enumerate(doc.paragraphs): for run_idx, run in enumerate(para.runs): # Check if this run has superscript formatting if run.font.superscript: # Check if it's likely a footnote reference if run.text.isdigit() or run.text in "¹²³⁴⁵⁶⁷⁸⁹⁰†‡§¶": footnote_refs.append({ 'paragraph_index': para_idx, 'run_index': run_idx, 'text': run.text, 'paragraph': para, 'run': run }) return footnote_refs
- Helper function to generate the specified numbering format symbols (numeric, roman, alpha, symbols) for the footnotes.def get_format_symbols(format_type: str, count: int) -> list: """Generate format symbols for footnote numbering.""" symbols = [] if format_type == "1, 2, 3": symbols = [str(i) for i in range(1, count + 1)] elif format_type == "i, ii, iii": # Roman numerals roman_map = [(10, 'x'), (9, 'ix'), (5, 'v'), (4, 'iv'), (1, 'i')] for i in range(1, count + 1): result = '' num = i for value, numeral in roman_map: count_sym, num = divmod(num, value) result += numeral * count_sym symbols.append(result) elif format_type == "a, b, c": # Alphabetic for i in range(1, count + 1): if i <= 26: symbols.append(chr(96 + i)) else: # For numbers > 26, use aa, ab, etc. first = (i - 1) // 26 second = (i - 1) % 26 + 1 symbols.append(chr(96 + first) + chr(96 + second)) elif format_type == "*, †, ‡": # Special symbols special = ['*', '†', '‡', '§', '¶', '#'] for i in range(1, count + 1): if i <= len(special): symbols.append(special[i - 1]) else: # Repeat symbols with numbers symbols.append(special[(i - 1) % len(special)] + str((i - 1) // len(special) + 1)) else: # Default to numeric symbols = [str(i) for i in range(1, count + 1)] return symbols