Skip to main content
Glama

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
NameRequiredDescriptionDefault
filepathYes
contentYes

Implementation Reference

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

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