apply_paragraph_style
Apply named paragraph styles like Heading 1 or Normal to specific paragraphs in Word documents to maintain consistent formatting and document structure.
Instructions
Apply a named paragraph style to a paragraph.
Args: filepath: Path to the document paragraph_index: Index of the paragraph (0-based) style_name: Name of the style to apply (e.g., "Heading 1", "Normal")
Returns: Dictionary with status
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| paragraph_index | Yes | ||
| style_name | Yes |
Implementation Reference
- src/docx_mcp/server.py:654-698 (handler)The handler for the apply_paragraph_style tool, responsible for modifying the style of a specific paragraph in a DOCX document using the python-docx library.
@app.tool() def apply_paragraph_style(filepath: str, paragraph_index: int, style_name: str) -> dict[str, Any]: """ Apply a named paragraph style to a paragraph. Args: filepath: Path to the document paragraph_index: Index of the paragraph (0-based) style_name: Name of the style to apply (e.g., "Heading 1", "Normal") Returns: Dictionary with status """ logger.info( "Applying paragraph style", extra={"tool": "apply_paragraph_style", "filepath": filepath}, ) try: doc = safe_open_document(filepath) if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs): raise InvalidParameterError("paragraph_index", "Index out of range") paragraph = doc.paragraphs[paragraph_index] paragraph.style = style_name safe_save_document(doc, filepath) logger.info(f"Applied style {style_name} to paragraph", extra={"filepath": filepath}) return { "status": "success", "filepath": filepath, "paragraph_index": paragraph_index, "style_applied": style_name, } except DocxMcpError as e: logger.warning( e.message, extra={"tool": "apply_paragraph_style", "error_code": e.error_code}, ) return {"status": "error", "error": e.message, "error_code": e.error_code} except Exception as e: logger.error(f"Unexpected error applying paragraph style: {str(e)}") return {"status": "error", "error": str(e)}