ANALYSIS TOOL BOX

File Management

ConvertWordToHTML

Convert Word document to HTML with embedded images and preserved formatting.

filesioexportimport

Introduction

Publishing a Word document on the web usually means stripping out its formatting and re-linking every embedded image by hand. ConvertWordToHTML skips that work: it uses the Mammoth library to translate a .docx file into clean, semantic HTML, preserving headings, tables, lists, and formatting while embedding every image as a base64 data URI — so the output is a single, self-contained HTML file with no external assets to manage or lose.

Teaching Note

Word-to-HTML conversion is essential for:

  • Web publishing and content management system migration
  • Creating accessible, responsive documentation from Word files
  • Email newsletter generation with embedded content
  • Archiving documents in open, platform-independent format
  • Integrating Word content into web applications and portals
  • Creating self-contained HTML reports and documentation
  • Converting legacy Word documents to modern web formats
  • Enabling full-text search and indexing of document content

Parameters

ParameterTypeDefaultDescription
docx_file_pathrequiredAbsolute or relative path to the input Word document (.docx format). The file must exist and be readable. Older .doc format is not supported.
html_file_pathNonePath where the output HTML file will be saved. Can be a full file path ending in .html, a directory path (function derives the filename from the source), or None to save alongside the input file with a .html extension. Missing directories are created automatically. Defaults to None.

Returns

None — the function writes an HTML file to html_file_path (or alongside the source file) and prints status messages, including any conversion warnings, to stdout.

Example

python
from analysistoolbox.file_management import ConvertWordToHTML

# Basic conversion with automatic output path
ConvertWordToHTML('report.docx')
# Creates 'report.html' in the same folder as the input file

# Specify a custom output location
ConvertWordToHTML(
    docx_file_path='C:/Documents/Annual_Report.docx',
    html_file_path='C:/Website/reports/annual_report.html'
)
# Converts to the specified path, creating directories if needed