PyDocX is an extensible Python library for reading Microsoft Word DOCX (Office Open XML) documents and exporting their content to markup.
It includes an HTML exporter and a command-line interface. Developers can customize the HTML output or implement exporters for other markup formats using the underlying document model and export framework.
- Built-in DOCX-to-HTML conversion.
- Support for headings, paragraphs, text formatting, hyperlinks, and images.
- Support for nested lists and tables, including merged cells.
- Customizable export behavior through exporter subclasses.
- Input from file paths, binary file objects, or file-like objects.
HTML is the built-in output format. Other formats require a custom exporter. PyDocX does not aim to reproduce Word's page layout exactly.
pip install pydocxConvert a document from the command line:
pydocx --html input.docx output.htmlOr use the Python API:
from pydocx import PyDocX
html = PyDocX.to_html("input.docx")You can also pass an open binary file:
from pydocx import PyDocX
with open("input.docx", "rb") as document:
html = PyDocX.to_html(document)Use PyDocXHTMLExporter directly when you need control over HTML
conversion:
from pydocx.export import PyDocXHTMLExporter
exporter = PyDocXHTMLExporter("input.docx")
html = exporter.export()Subclass PyDocXHTMLExporter to customize HTML output, or
pydocx.export.base.PyDocXExporter to implement another markup format.
See the extension guide for details.
Bug reports, documentation improvements, and code contributions are welcome. When reporting a conversion problem, include your Python and PyDocX versions, the expected output, and a minimal DOCX example with sensitive information removed.
PyDocX is licensed under the Apache License, Version 2.0. See the LICENSE file for details.