strip-markdown
strip-markdown is a Python library that converts Markdown text to plain text, offering both a programmatic interface and a command-line utility. It is currently at version 1.3, released in April 2022, and maintains an active status with infrequent but functional updates.
Common errors
-
AttributeError: 'module' object has no attribute 'strip'
cause Attempting to call `strip_markdown.strip()` instead of the full function name `strip_markdown.strip_markdown()`.fixThe main function to strip markdown from a string is `strip_markdown.strip_markdown()`. Use `plain_text = strip_markdown.strip_markdown(your_markdown_string)`. -
ModuleNotFoundError: No module named 'strip_markdown'
cause The `strip-markdown` package has not been installed, or the environment where the script is run does not have access to the installed package.fixInstall the package using pip: `pip install strip-markdown`. Ensure your Python environment is correctly activated if using virtual environments. -
TypeError: expected string or bytes-like object, got _io.TextIOWrapper
cause You are passing an open file object directly to `strip_markdown.strip_markdown()` instead of reading its content into a string.fixRead the content of the file into a string first using `.read()` or use the `strip_markdown.strip_markdown_file()` function for file-based operations. Example: `with open('your_file.md', 'r') as f: markdown_content = f.read(); plain_text = strip_markdown.strip_markdown(markdown_content)`.
Warnings
- gotcha Be aware of name collisions with other markdown stripping libraries or JavaScript packages. This `strip-markdown` is a Python library, distinct from `remarkjs/strip-markdown` (a Node.js plugin) or Python alternatives like `qstrip` or `markdownify`. Ensure you've installed and are importing the correct package.
- gotcha The `strip-markdown` library is designed to convert Markdown directly to plain text, removing all formatting. It does not convert Markdown to HTML first and then strip the HTML, which is a common pattern in other markdown processing workflows (e.g., using `Python-Markdown` + `BeautifulSoup`). If you need HTML as an intermediate step or require more granular control over HTML elements, consider those alternative libraries.
- gotcha When using `strip_markdown.strip_markdown_file(MD_fn)`, version 1.2 introduced automatic calculation of the output file name if not explicitly provided. This might lead to unexpected file creations or overwrites if you were relying on older behavior or expecting to always define the output path.
Install
-
pip install strip-markdown
Imports
- strip_markdown
from strip_markdown import strip_markdown
import strip_markdown
Quickstart
import strip_markdown
markdown_text = "# Hello World\n\nThis is **bold** text and an [example link](https://example.com/).\n- Item 1\n- Item 2"
plain_text = strip_markdown.strip_markdown(markdown_text)
print(plain_text)
# Example of stripping from a file
# with open('input.md', 'w') as f:
# f.write(markdown_text)
# stripped_file_content = strip_markdown.strip_markdown_file('input.md')
# print(stripped_file_content)