add_paragraph
Insert a paragraph into a Microsoft Word document. Specify the filename, text content, and optional style to enhance document formatting and structure.
Instructions
Add a paragraph to a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| style | No | ||
| text | Yes |
Implementation Reference
- word_document_server/main.py:643-671 (handler)MCP-registered tool handler for 'add_paragraph'. Loads document via resolver, adds paragraph with optional style application, handles errors, and saves the document.async def add_paragraph(filename: str, text: str, style: Optional[str] = None): """Add a paragraph to a Word document.""" try: # Use resolver to find the document doc, resolved_path = load_document_with_resolver(filename) # Add the paragraph paragraph = doc.add_paragraph(text) # Apply style if provided if style: try: paragraph.style = style except KeyError: # Style doesn't exist, use normal and report it paragraph.style = doc.styles['Normal'] # Save and return with warning save_document_with_resolver(doc, filename, resolved_path) return f"Paragraph added to {filename} with Normal style ('{style}' style not found)" # Save the document save_document_with_resolver(doc, filename, resolved_path) return f"Paragraph added to {filename}" except FileNotFoundError as e: return str(e) except Exception as e: return f"Failed to add paragraph: {str(e)}"
- Supporting helper function implementing paragraph addition logic with file checks and style handling, used as reference or by other components.async def add_paragraph(filename: str, text: str, style: Optional[str] = None) -> str: """Add a paragraph to a Word document. Args: filename: Path to the Word document text: Paragraph text style: Optional paragraph style name """ 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}" doc.save(filename) return f"Paragraph added to {filename}" except Exception as e: return f"Failed to add paragraph: {str(e)}"
- word_document_server/tools/__init__.py:16-20 (registration)Re-export/registration of the add_paragraph function from content_tools module for convenient import in other parts of the codebase.from word_document_server.tools.content_tools import ( add_heading, add_paragraph, add_table, add_picture, add_page_break, add_table_of_contents, delete_paragraph, search_and_replace )
- word_document_server/main.py:643-644 (schema)Type hints and docstring defining the input schema for the MCP tool.async def add_paragraph(filename: str, text: str, style: Optional[str] = None): """Add a paragraph to a Word document."""