Use this file to discover Exa products, pricing, evaluations, comparisons, use cases, agent interfaces, and technical documentation before exploring further.
Python DOCX libraries create, read, and convert Microsoft Word documents from Python code. A DOCX file is a zip archive of XML parts that a library can edit without opening Word. These libraries let apps and AI agents generate reports, contracts, and letters as Word files.
Popularity
python-docx has the most monthly downloads in this comparison, at 139.9M.
def generate_docx_file(
self, content: str, filename: Optional[str] = None, title: Optional[str] = None
) -> ToolResult:
"""Generate a DOCX file from the provided content.
Args:
content: The text content to write to the DOCX file.
filename: Optional filename for the generated file. If not provided, a UUID will be used.
title: Optional title for the DOCX document.
Returns:
ToolResult: Result containing the generated DOCX file as a FileArtifact.
"""
if not DOCX_AVAILABLE:
return ToolResult(
content="DOCX generation is not available. Please install python-docx: pip install python-docx"
)
try:
log_debug(f"Generating DOCX file with content length: {len(content)}")
document = Document()
if title:
document.add_paragraph(title)
paragraphs = content.split("\n\n")
for para in paragraphs:
if para.strip():
document.add_paragraph(para.strip())
buffer = io.BytesIO()
document.save(buffer)
docx_content = buffer.getvalue()
buffer.close()
return self._create_file_artifact(
docx_content,
filename,
file_type="docx",
mime_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document",
display_name="DOCX",
)
except Exception as e:
logger.exception("Failed to generate DOCX file")
return ToolResult(content=f"Error generating DOCX file: {e}")
Mammoth
Converts DOCX documents into semantic HTML.
Downloads / mo
18.2M
Stars
1.1K
Last release
1.13.0
License
BSD-2-Clause
Strengths
Keeps Word heading structure.Convert heading styles into semantic HTML headings.
Accepts custom style maps.Map named Word styles to the HTML elements the application expects.
Limitations
Generated HTML needs sanitizing.Sanitize converted HTML before inserting it into an application page.
External file access stays disabled.Enable external_file_access only when trusted external document references must be read.
def convert_image(self, image: Any) -> list[Any]:
stream_info = StreamInfo(
mimetype=image.content_type,
extension=(
mimetypes.guess_extension(image.content_type)
if image.content_type
else None
),
)
with image.open() as image_stream:
try:
fragment = self._render(image_stream, stream_info, **self._options)
except InvalidFileReferenceError as exc:
# Mammoth swallows this exception for missing document images, # but an error raised by the override must reach the dispatcher. raise RuntimeError("_image_to_html failed") from exc
soup = _parse_image_html(fragment)
if soup is None:
return images.data_uri(image)
key = str(len(self._fragments))
self._fragments[key] = soup
return [html.element("img", {self._attribute: key})]
docx2txt
Extracts text and images from DOCX files in pure Python.
import aspose.words as aw
document = aw.Document('input.docx')
document.save('output.pdf')
FAQ
python-docx suits creating and editing Word files paragraph by paragraph. docxtpl suits filling a Word template with Jinja-style tags. Mammoth suits converting DOCX into clean HTML.
python-docx creates, reads, and updates Microsoft Word .docx files. Its document API covers paragraphs, tables, sections, and inline shapes.
docxtpl fills a template authored in Word with Jinja tags and values from a Python context. It also supports rich text and subdocuments.
docx2pdf converts DOCX to PDF on Windows or macOS through an installed copy of Microsoft Word. aspose-words renders Word documents to other formats without Office.
docx2txt extracts plain text, including headers, footers, and hyperlinks, and can save embedded images. docx2python returns nested body, header, and footer content with images.
For coding agents
Found your Word DOCX library? Help your agent use it.
Connect Exa so your coding agent can find the library’s documentation, check its API, and look up implementations while you build.