SnakeMD
SnakeMD is a Python library designed for programmatically generating Markdown documents. It allows users to create headings, paragraphs, lists, tables, and other Markdown elements using a Pythonic API. The library is actively maintained, with version 2.4.0 being the latest stable release, and typically sees updates every few months.
Common errors
-
AttributeError: 'Document' object has no attribute 'add_element'
cause Attempting to use a method (`add_element`) that was removed in SnakeMD 2.0.0. This was a common way to add content in 1.x.x versions.fixReplace calls to `doc.add_element(...)` with specific methods like `doc.add_heading(...)`, `doc.add_paragraph(...)`, `doc.add_table(...)`, etc. Refer to the SnakeMD 2.0.0 documentation for the updated API. -
ModuleNotFoundError: No module named 'snakemd.elements'
cause Attempting to import elements (e.g., `Paragraph`, `Heading`) directly from an internal or deprecated module path. In current versions, most public elements are exposed directly under the `snakemd` package.fixChange your import statements from `from snakemd.elements import ...` to `from snakemd import ...` (e.g., `from snakemd import Paragraph, Heading`). -
Markdown output does not show bold text inside a link, it's just plain text.
cause This is a known bug in SnakeMD versions prior to 2.4.0, where the linking mechanism would inadvertently strip inline styles from its content.fixUpgrade your SnakeMD installation to version 2.4.0 or newer: `pip install --upgrade snakemd`.
Warnings
- breaking Version 2.0.0 of SnakeMD removed all previously deprecated classes, parameters, and methods. Code written for 1.x.x versions (e.g., using `add_element` or older class names) will likely result in `AttributeError` or `TypeError`.
- gotcha Prior to v2.4.0, a bug existed where inline styles (like bold or italics) inside linked text would be stripped, leading to plain text links. This affects markdown rendering where complex inline styling within links is desired.
- gotcha In versions prior to 2.4.0, the processing of inline elements within `BlockQuote` (and potentially other elements) could lead to issues where embedded formatting was not correctly respected, resulting in improperly rendered Markdown.
Install
-
pip install snakemd
Imports
- Document
from snakemd import Document
- Paragraph
from snakemd.elements import Paragraph
from snakemd import Paragraph
- Heading
from snakemd import Heading
- Table
from snakemd import Table
Quickstart
import snakemd
# Create a new Document
doc = snakemd.Document("My Article")
# Add a heading
doc.add_heading("Welcome to SnakeMD!")
# Add a paragraph
doc.add_paragraph("This is an example of how to use SnakeMD to generate Markdown.")
# Add an ordered list
doc.add_ordered_list(["First item", "Second item", "Third item"])
# Add a table
doc.add_table(
headers=["Name", "Age"],
data=[["Alice", "30"], ["Bob", "24"]]
)
# Output the Markdown to a file
# In a real scenario, you might want to save to a specific path
# and handle potential file I/O errors.
output_filename = os.environ.get('SNAKEMD_OUTPUT_FILE', 'output.md')
with open(output_filename, "w") as f:
f.write(doc.render())
print(f"Markdown written to {output_filename}")