Registry / serialization / html-text

html-text

JSON →
library0.7.1pypypi✓ verified 23d ago

html-text is a Python library designed to extract clean, readable plain text from HTML content. It goes beyond simple text extraction by removing invisible non-text content like inline styles, JavaScript, and comments. The library intelligently normalizes whitespace and can optionally add newlines after block-level elements (e.g., headers, paragraphs) to produce text that more closely resembles browser rendering, making it suitable for text classification or further natural language processing. The current version is 0.7.1, and it maintains an active development status.

pip install html-text
INSTALL
IMPORT
SIG · HTML-TEXT
H
html-text
serializationpythonv0.7.1
Install
2.2s avg
Import
86ms
Disk
28MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.1 · 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.090s · 29.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.2s · import 0.082s · 30MB
28MB installed
● package 28MB
Code
Verified usage

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

extract_text
from html_text import extract_text
parse_html
from html_text import parse_html
cleaner
from html_text import cleaner
etree_to_text
from html_text import etree_to_text
cleaned_selector
from html_text import cleaned_selector

Demonstrates the basic usage of `html_text.extract_text` to convert an HTML string into plain text, including an example of disabling layout guessing for a flatter output.

import html_text html_content = '<h1>Hello</h1><p>This is a <b>paragraph</b> with <span>inline</span> text.</p>' plain_text = html_text.extract_text(html_content) print(plain_text) # To get text without layout-driven newlines (e.g., after h1) plain_text_flat = html_text.extract_text(html_content, guess_layout=False) print(plain_text_flat)
Debug
Known issues
gotchaBy default, `html_text.extract_text()` attempts to 'guess layout' and inserts newlines after block-level HTML elements (e.g., <h1>, <p>) to improve readability. If a flat, single-line text output is desired, explicitly set `guess_layout=False`.
fix
For flatter output, call `html_text.extract_text(html_string, guess_layout=False)`.
affects: All versions
gotchaWhen working with pre-parsed `lxml.html.HtmlElement` trees or `parsel.Selector` objects, lower-level functions like `html_text.etree_to_text()` or `html_text.selector_to_text()` do NOT automatically clean the HTML. You must manually apply cleaning using `html_text.cleaner.clean_html()` or `html_text.cleaned_selector()` first.
fix
Always clean manually before using lower-level extraction functions, e.g., `cleaned_tree = html_text.cleaner.clean_html(tree)` or `cleaned_sel = html_text.cleaned_selector(html_content)`.
affects: All versions
gotchaThe `html-text` library depends on `lxml`. Installing `lxml` can sometimes be complex on various operating systems, requiring system-level development packages (e.g., `libxml2-dev` and `libxslt-dev` on Debian/Ubuntu, or Xcode Command Line Tools on macOS) for its C extensions to compile correctly.
fix
Refer to the `lxml` installation guide (often linked from its PyPI page or documentation) for specific system dependencies required before `pip install lxml` or `pip install html-text`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'html-text'
The Python package `html-text` is installed, but the correct import name uses an underscore instead of a hyphen.
fix
Use `import html_text` instead of `import html-text` in your Python code.
ModuleNotFoundError: No module named 'html2text'
The user has installed the `html-text` library but is mistakenly trying to import a different, similarly named library called `html2text`.
fix
If you intended to use the `html-text` library, import it as `import html_text`. If you intended to use the `html2text` library, install it with `pip install html2text` and then `import html2text`.
AttributeError: module 'html_text' has no attribute 'extract_text'
This error occurs when attempting to call a function that either does not exist or is misspelled/incorrectly cased within the `html_text` module. The primary function for text extraction is `extract_text`.
fix
Ensure you are calling the correct function with the proper casing: `html_text.extract_text(your_html_content)`.
Upgrade
Version history
0.7.1latest on PyPI · released Oct 6, 2025
Audit
Dependencies
lxmlrequiredCore dependency for HTML parsing; may require system-level development packages for installation.
Agent activity
7 hits · last 30 days
node
6
Resources
html-text — pip install html-text · libregistry