Registry / serialization / htmldocx

htmldocx

JSON →
library0.0.6pypypi✓ verified 21d ago

The `htmldocx` library provides functionality to convert HTML content into DOCX format, building upon `python-docx` and `beautifulsoup4`. While its last release was in August 2021, it is considered to be in a maintenance state, with more actively developed forks available that address limitations and bugs present in this version.

pip install htmldocx
INSTALL
IMPORT
SIG · HTMLDOCX
H
htmldocx
serializationpythonv0.0.6
Install
2.5s avg
Import
427ms
Disk
32MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.6 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.438s · 33.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.5s · import 0.416s · 34MB
32MB installed
● package 32MB
Code
Verified usage

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

HtmlToDocx
from htmldocx import HtmlToDocx

Initialise `HtmlToDocx` and use `add_html_to_document` to insert HTML into a `python-docx` Document object, or use `parse_html_file` / `parse_html_string` for direct conversion.

from docx import Document from htmldocx import HtmlToDocx document = Document() new_parser = HtmlToDocx() html_content = '<h1>Hello world</h1><p>This is a paragraph.</p>' # Add HTML to an existing Document object new_parser.add_html_to_document(html_content, document) # Save the document document.save('your_file_name.docx') # Or convert a file directly # new_parser.parse_html_file('input.html', 'output.docx') # Or convert from an HTML string to a new docx object # docx_object = new_parser.parse_html_string('<h2>Another title</h2>') # docx_object.save('another_file.docx')
Debug
Known issues
gotchaThe `htmldocx` package has not been updated since August 2021. This means it may lack modern HTML rendering features, bug fixes, or compatibility updates present in more recently developed alternatives or forks.
fix
Consider using more actively maintained forks or alternative libraries for robust HTML to DOCX conversion, such as `html-for-docx`.
affects: <=0.0.6
gotchaDevelopers who have forked this project (e.g., `html-for-docx`) have cited "limitations and bugs" in the original `pqzx/html2docx` codebase (which `htmldocx` is based on) that prevented them from completing tasks. Users may encounter similar rendering issues with complex HTML structures or specific CSS styles.
fix
Test thoroughly with your specific HTML inputs. For advanced styling or complex layouts, prepare to use workarounds or explore alternative libraries.
affects: <=0.0.6
gotchaTables are not styled by default when converted. To apply styles like borders or shading, you must explicitly set the `table_style` attribute on the `HtmlToDocx` parser instance.
fix
Set `new_parser.table_style = 'Light Shading Accent 4'` or another valid style (e.g., 'TableGrid') before adding HTML content.
affects: <=0.0.6
gotchaNo specific style is applied to paragraphs by default. While additional styling defined in HTML will be applied, a base paragraph style is not automatically set.
fix
Set the `paragraph_style` attribute on the `HtmlToDocx` parser instance to apply a default style to all paragraphs if needed (e.g., `new_parser.paragraph_style = 'Normal'`).
affects: <=0.0.6
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'htmldocx'
The `htmldocx` package is either not installed in your Python environment or is installed in a different environment than the one running your script.
fix
Install the library using pip: `pip install htmldocx`. Ensure your Python interpreter is configured to use the environment where `htmldocx` is installed.
AttributeError: 'HtmlToDocx' object has no attribute 'run'
This is a known bug in `htmldocx` (and its upstream `html2docx`) often triggered by `<br>` tags in the HTML input, particularly at the beginning of the document or within `<td>` elements, causing the internal parser to attempt operations on an uninitialized object.
fix
Review your HTML input for problematic `<br>` tags, especially those at the start of the document or within table data cells, and try to remove or refactor them. Consider using a more actively maintained fork like `html4docx` if the issue persists, as it may have addressed this bug.
UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>
The HTML input file contains characters (e.g., accented characters, special symbols) that cannot be decoded using the default system encoding, which is often not 'utf-8'.
fix
When providing HTML content from a file, explicitly read the file with the correct encoding (usually 'utf-8') before passing the string to `htmldocx`'s `add_html_to_document` or `parse_html_string` methods.
```python
from htmldocx import HtmlToDocx
from docx import Document

with open('input.html', 'r', encoding='utf-8') as f:
    html_content = f.read()

document = Document()
new_parser = HtmlToDocx()
new_parser.add_html_to_document(html_content, document)
document.save('output.docx')
```
AttributeError: 'NoneType' object has no attribute 'replace_with()'
This error occurs when the `BeautifulSoup` parsing, used internally by `htmldocx`, attempts to find an HTML element but receives `None` because the element was not found in the input HTML. Subsequently, a method like `replace_with()` is called on this `None` object, leading to the error.
fix
Ensure your HTML input is well-formed and contains all the expected elements that `htmldocx` is designed to process. This error often indicates a mismatch between the expected HTML structure and the actual HTML provided, causing `htmldocx`'s internal parsing to fail to locate a required tag.
Upgrade
Version history
0.0.6latest on PyPI · released Aug 25, 2021
Audit
Dependencies
python-docxrequiredRequired for creating and manipulating DOCX files.
beautifulsoup4requiredRequired for parsing HTML content.
Agent activity
7 hits · last 30 days
node
6
Resources
htmldocx — pip install htmldocx · libregistry