HTML2Text

Resiliparse simple HTML (main) content extraction API documentation.

Extract text from HTML strings and parsed DOM trees.

resiliparse.extract.html2text.extract_plain_text(html, preserve_formatting=Ellipsis, main_content=False, list_bullets=True, alt_texts=True, links=False, form_fields=False, noscript=False, comments=True, post_meta=True, hidden_elements=False, skip_elements=None)

Perform a simple plain-text extraction from an HTML string or parsed DOM tree.

Extracts all visible text (excluding script/style elements, comment nodes etc.) and collapses consecutive white space characters.

If preserve_formatting is True, line breaks, paragraphs, other block-level elements, list elements, and pre-formatted text will be preserved. Use the special value 'minimal_html' to add reduced HTML markup to the formatted output, preserving headings (<h1-6>), paragraphs (<p>), lists (<ul>, <ol>), <pre> text, <br> line breaks, and links (<a>, if links=True).

Extraction of particular elements and attributes such as links, alt texts, or form fields can be configured individually by setting the corresponding parameter to True. Alternative text and list bullets are enabled by default; links, form fields, and noscript content are disabled.

Parameters:
  • html – HTML as DOM tree or Unicode string

  • preserve_formatting – Preserve basic block-level formatting (use 'minimal_html' for minimal HTML markup in output)

  • main_content – Apply simple heuristics for extracting only “main-content” elements

  • list_bullets – Insert bullets / numbers for list items

  • alt_texts – Preserve alternative text descriptions

  • links – Extract link target URLs

  • form_fields – Extract form fields and their values

  • noscript – Extract contents of <noscript> elements

  • comments – Treat comment sections as main content

  • post_meta – Preserve blog post / article meta data in main content extract

  • hidden_elements – Keep elements with hidden class names during main-content extraction; explicit hidden attributes and inline CSS remain subject to filtering

  • skip_elements – List of CSS selectors for elements to skip

Returns:

Extracted plain text

Raises:
  • TypeError – If html is neither a Unicode string nor an HTMLTree, or an option has an invalid type

  • HTMLParserException – If parsing the HTML string fails

  • CSSParserException – If a skip selector cannot be parsed