
HTML (HyperText Markup Language) is a markup language used to create web pages, allowing developers to build rich and visually appealing layouts. However, HTML files often contain a large number of tags, which makes them difficult to read if you only need the main content. By using Python to convert HTML to text, this problem can be easily solved. Unlike raw HTML, the converted text file strips away all unnecessary markup, leaving only clean and readable content that is easier to store, analyze, or process further.
- Install HTML to Text Converter in Python
- Python Convert HTML File to Text
- Python Convert HTML String to Text
- The Conclusion
- FAQs
Install HTML to Text Converter in Python
To simplify the task, we recommend using Spire.Doc for Python. This Python Word library allows you to quickly remove HTML markup and extract clean plain text with ease. It not only works as an HTML-to-text converter, but also offers a wide range of features—covering almost everything you can do in Microsoft Word.
To install it, you can run the following pip command:
pip install spire.doc
Alternatively, you can download the Spire.Doc package and install it manually.
Python Convert HTML Files to Text in 3 Steps
After preparing the necessary tools, let's dive into today's main topic: how to convert HTML to plain text using Python. With the help of Spire.Doc, this task can be accomplished in just three simple steps: create a new document object, load the HTML file, and save it as a text file. It’s straightforward and efficient, even for beginners. Let’s take a closer look at how this process can be implemented in code!
Code Example – Converting an HTML File to a Text File:
from spire.doc import *
from spire.doc.common import *
# Open an html file
document = Document()
document.LoadFromFile("/input/htmlsample.html", FileFormat.Html, XHTMLValidationType.none)
# Save it as a Text document.
document.SaveToFile("/output/HtmlFileTotext.txt", FileFormat.Txt)
document.Close()
The following is a preview comparison between the source document (.html) and the output document (.txt):

Note that if the HTML file contains tables, the output text file will only retain the values within the tables and cannot preserve the original table formatting. If you want to keep certain styles while removing markup, it is recommended to convert HTML to a Word document . This way, you can retain headings, tables, and other formatting, making the content easier to edit and use.
How to Convert an HTML String to Text in Python
Sometimes, we don’t need the entire content of a web page and only want to extract specific parts. In such cases, you can convert an HTML string directly to text. This approach allows you to precisely control the information you need without further editing. Using Python to convert an HTML string to a text file is also straightforward. Here’s a detailed step-by-step guide:
Steps to convert an HTML string to a text document using Spire.Doc:
- Input the HTML string directly or read it from a local file.
- Create a Document object and add sections and paragraphs.
- Use Paragraph.AppendHTML() method to insert the HTML string into a paragraph.
- Save the document as a .txt file using Document.SaveToFile() method.
The following code demonstrates how to convert an HTML string to a text file using Python:
from spire.doc import *
from spire.doc.common import *
#Get html string.
#with open(inputFile) as fp:
#HTML = fp.read()
# Load HTML from string
html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML to Text Example</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
header { background: #f4f4f4; padding: 10px; }
nav a { margin: 0 10px; text-decoration: none; color: #333; }
main { margin-top: 20px; }
</style>
</head>
<body>
<header>
<h1>My Demo Page</h1>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</nav>
</header>
<main>
<h2>Convert HTML to Text</h2>
<p>This is a simple demo showing how HTML content can be displayed before converting it to plain text.</p>
</main>
</body>
</html>
"""
# Create a new document
document = Document()
section = document.AddSection()
section.AddParagraph().AppendHTML(html)
# Save directly as TXT
document.SaveToFile("/output/HtmlStringTotext.txt", FileFormat.Txt)
document.Close()
Here's the preview of the converted .txt file: 
The Conclusion
In today’s tutorial, we focused on how to use Python to convert HTML to a text file. With the help of Spire.Doc, you can handle both HTML files and HTML strings in just a few lines of code, easily generating clean plain text files. If you’re interested in the other powerful features of the Python Word library, you can request a 30-day free trial license and explore its full capabilities for yourself.
FAQs about Converting HTML to Text in Python
Q1: How can I convert HTML to plain text using Python?
A: Use Spire.Doc to load an HTML file or string, insert it into a Document object with AppendHTML(), and save it as a .txt file.
Q2: Can I keep some formatting when converting HTML to text?
A: To retain styles like headings or tables, convert HTML to a Word document first, then export to text if needed.
Q3: Is it possible to convert only part of an HTML page to text?
A: Yes, extract the specific HTML segment as a string and convert it to text using Python for precise control.
