Skip to main content
Glama

add_paragraph

Insert a new paragraph into a Word document by specifying the filename, text, and optional style. Enhances document editing with structured and formatted content.

Instructions

Add a paragraph to a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
styleNo
textYes

Implementation Reference

  • Core handler function that executes the add_paragraph tool: loads the document, adds a new paragraph with the given text, applies optional style and run formatting (font, size, bold, italic, color), saves the document, and returns success/error message.
    async def add_paragraph(filename: str, text: str, style: Optional[str] = None, font_name: Optional[str] = None, font_size: Optional[int] = None, bold: Optional[bool] = None, italic: Optional[bool] = None, color: Optional[str] = None) -> str: """Add a paragraph to a Word document with optional formatting. Args: filename: Path to the Word document text: Paragraph text style: Optional paragraph style name font_name: Font family (e.g., 'Helvetica', 'Times New Roman') font_size: Font size in points (e.g., 14, 36) bold: True/False for bold text italic: True/False for italic text color: RGB color as hex string (e.g., '000000' for black) """ 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: # Suggest creating a copy return f"Cannot modify document: {error_message}. Consider creating a copy first or creating a new document." try: doc = Document(filename) paragraph = doc.add_paragraph(text) if style: try: paragraph.style = style except KeyError: # Style doesn't exist, use normal and report it paragraph.style = doc.styles['Normal'] doc.save(filename) return f"Style '{style}' not found, paragraph added with default style to {filename}" # Apply formatting to all runs in the paragraph if any([font_name, font_size, bold is not None, italic is not None, color]): for run in paragraph.runs: if font_name: run.font.name = font_name if font_size: run.font.size = Pt(font_size) if bold is not None: run.font.bold = bold if italic is not None: run.font.italic = italic if color: # Remove any '#' prefix if present color_hex = color.lstrip('#') run.font.color.rgb = RGBColor.from_string(color_hex) doc.save(filename) return f"Paragraph added to {filename}" except Exception as e: return f"Failed to add paragraph: {str(e)}"
  • MCP tool registration using FastMCP @mcp.tool() decorator. Defines the tool interface, JSON schema via args/docstring, and delegates execution to content_tools.add_paragraph.
    @mcp.tool() def add_paragraph(filename: str, text: str, style: str = None, font_name: str = None, font_size: int = None, bold: bool = None, italic: bool = None, color: str = None): """Add a paragraph to a Word document with optional formatting. Args: filename: Path to Word document text: Paragraph text content style: Optional paragraph style name font_name: Font family (e.g., 'Helvetica', 'Times New Roman') font_size: Font size in points (e.g., 14, 36) bold: Make text bold italic: Make text italic color: Text color as hex RGB (e.g., '000000') """ return content_tools.add_paragraph(filename, text, style, font_name, font_size, bold, italic, color)
  • Input schema and documentation for the add_paragraph tool, used by FastMCP to generate JSON Schema for MCP protocol.
    """Add a paragraph to a Word document with optional formatting. Args: filename: Path to Word document text: Paragraph text content style: Optional paragraph style name font_name: Font family (e.g., 'Helvetica', 'Times New Roman') font_size: Font size in points (e.g., 14, 36) bold: Make text bold italic: Make text italic color: Text color as hex RGB (e.g., '000000') """

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/GongRzhe/Office-Word-MCP-Server'

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