Skip to main content
Glama

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
NameRequiredDescriptionDefault
filepathYes
paragraph_indexYes
style_nameYes

Implementation Reference

  • 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)}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Andrew82106/LLM_Docx_Agent_MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server