write_docx
Create or overwrite Microsoft Word documents with plain text content. Specify file path and text to generate DOCX files.
Instructions
Create or overwrite a document with plain text content.
Args: filepath: Path to the document content: Text content to write
Returns: Dictionary with status
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| content | Yes |
Implementation Reference
- src/docx_mcp/server.py:134-171 (handler)The 'write_docx' handler, decorated with @app.tool(), which creates or overwrites a Word document with provided text content.
@app.tool() def write_docx(filepath: str, content: str) -> dict[str, Any]: """ Create or overwrite a document with plain text content. Args: filepath: Path to the document content: Text content to write Returns: Dictionary with status """ logger.info("Writing to document", extra={"tool": "write_docx", "filepath": filepath}) try: path = normalize_path(filepath) path.parent.mkdir(parents=True, exist_ok=True) doc = Document() for paragraph_text in content.split("\n"): if paragraph_text.strip(): doc.add_paragraph(paragraph_text) doc.save(str(path)) logger.info("Document written successfully", extra={"filepath": filepath}) return { "status": "success", "filepath": str(path), "message": f"Document written: {path.name}", } except DocxMcpError as e: logger.warning(e.message, extra={"tool": "write_docx", "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 writing document: {str(e)}") return {"status": "error", "error": str(e)}