Registry / serialization / html2docx

html2docx

JSON →
library1.6.0pypypi✓ verified 83d ago

html2docx is a Python library that converts valid HTML input into Microsoft Word (.docx) documents. It leverages `python-docx` for document generation and `BeautifulSoup` for robust HTML parsing, aiming to translate common HTML structures and basic styling into an editable Word format. The current version is 1.6.0, with a release cadence that focuses on bug fixes and minor feature enhancements rather than frequent major API changes.

pip install html2docx
INSTALL
IMPORT
SIG · HTML2DOCX
H
html2docx
serializationpythonv1.6.0
Install
2.5s avg
Import
Disk
31MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.6.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 33.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.5s · import 0.000s · 34MB
31MB installed
● package 31MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HTML2Docx
from html2docx import HTML2Docx
from html2docx import Html2Docx

This quickstart demonstrates how to convert a string of HTML content into a .docx file using `html2docx`. It initializes `Html2Docx`, parses the HTML, and saves the resulting `python-docx` Document object to a temporary file, suitable for viewing or further processing.

import os import tempfile from html2docx import Html2Docx # Example HTML content with basic structure and an external image html_content = """ <h1>Hello World!</h1> <p>This is a paragraph with some <strong>bold</strong> and <em>italic</em> text.</p> <ul> <li>List Item 1</li> <li>List Item 2</li> </ul> <p style="text-align: center;">A centered paragraph.</p> <img src="https://www.python.org/static/community_logos/python-logo-only.png" width="100px" alt="Python Logo"> """ # Initialize the parser new_parser = Html2Docx() # Parse the HTML and get a python-docx Document object docx = new_parser.parse_html_section(html_content) # Save the document to a temporary file with tempfile.NamedTemporaryFile(suffix=".docx", delete=False) as temp_file: docx.save(temp_file.name) print(f"Generated DOCX saved to: {temp_file.name}") # To view the file, uncomment the following line (might not work on all OSs) # os.startfile(temp_file.name) # On Windows # import subprocess; subprocess.call(['open', temp_file.name]) # On macOS # In a real application, you might want to clean up the temp file after use # os.remove(temp_file.name)
Debug
Known issues
gotchahtml2docx primarily translates HTML *structure and semantics* (e.g., `<h1>`, `<p>`, `<ul>`, `<strong>`) rather than exact *visual layout* dictated by complex CSS. Advanced CSS properties (e.g., `float`, `position`, `flexbox`) are often ignored or translated imperfectly due to DOCX format limitations.
fix
Simplify HTML and CSS to use basic semantic tags and inline styles that map well to Word document capabilities. Avoid relying on complex CSS for layout; use simpler constructs like `text-align` or basic list/paragraph styles.
affects: All versions
gotchaRelative image paths in the HTML may not resolve correctly when converting, especially if the DOCX is generated in a different context than the HTML was intended to be viewed. The library needs direct access to image files.
fix
Always use absolute URLs for remote images or ensure local image paths are absolute or correctly resolvable from the script's execution directory. Verify the image files exist and are accessible.
affects: All versions
gotchaThe library expects well-formed and valid HTML. Malformed tags, unclosed elements, or overly complex/non-standard HTML structures can lead to unexpected output, missing content, or errors during parsing.
fix
Pre-process HTML with an HTML linter or a library like `BeautifulSoup` itself to clean up and ensure it's well-formed before passing it to `html2docx`. Avoid non-standard or deprecated HTML tags.
affects: All versions
Upgrade
Version history
1.6.0latest on PyPI · released Jan 19, 2023
Audit
Dependencies
python-docxrequiredCore dependency for generating .docx files.
beautifulsoup4requiredCore dependency for parsing HTML content.
Agent activity
4 hits · last 30 days
node
4
Resources
html2docx — pip install html2docx · libregistry