A Python utility that converts Markdown files into beautifully styled HTML pages. Perfect for generating documentation, README displays, blog posts, and more.
- 📝 Convert Markdown to clean, responsive HTML
- 🎨 GitHub-inspired styling out of the box
- 🔧 Support for extended Markdown features (tables, code blocks, footnotes)
- 💻 Syntax highlighting for code blocks
- 📱 Mobile-responsive design
- ⚡ Simple command-line interface
- 🎯 Customizable page titles
- 📦 Self-contained HTML output (CSS included)
- Python 3.6+
markdownlibrary
-
Clone or download this repository
-
Install required dependencies:
pip install markdownConvert a Markdown file to HTML:
python md_to_html.py input.mdThis creates input.html in the same directory.
python md_to_html.py README.md index.htmlpython md_to_html.py document.md output.html "My Documentation"You can also use it programmatically in your Python code:
from md_to_html import convert_markdown_to_html
# Basic conversion
convert_markdown_to_html('input.md')
# With custom output and title
convert_markdown_to_html('input.md', 'output.html', 'My Custom Title')python md_to_html.py <input.md> [output.html] [title]
| Argument | Description | Required | Default |
|---|---|---|---|
input.md |
Input Markdown file | Yes | - |
output.html |
Output HTML file | No | Same name as input with .html extension |
title |
HTML page title | No | Input filename without extension |
- Bold and italic text
- Headers (H1-H6)
- Links and images
- Ordered and unordered lists
- Blockquotes
- Horizontal rules
- Tables with proper styling
- Fenced code blocks with language specification
- Inline code formatting
- Footnotes
- Definition lists
- Automatic link detection
The converter supports syntax highlighting for code blocks:
```python
def hello_world():
print("Hello, World!")
```Supported languages include Python, JavaScript, Java, C, C++, HTML, CSS, and many more.
The generated HTML includes:
- Proper HTML5 doctype and structure
- UTF-8 character encoding
- Responsive viewport meta tag
- Embedded CSS styling
- Clean, semantic HTML markup
The default styling includes:
- Maximum width: 800px for optimal readability
- Typography: System font stack for native look
- Code blocks: Syntax highlighting with GitHub-style background
- Tables: Bordered cells with hover effects
- Links: Styled with hover effects
- Headers: Proper hierarchy with bottom borders
- Blockquotes: Left border accent
- Responsive: Mobile-friendly design
# My Project
This is a **great** project that does amazing things.
## Features
- Fast performance
- Easy to use
- Well documented
## Code Example
```python
print("Hello, World!")Run this command:
pip install myproject
### Command:
```bash
python md_to_html.py example.md
A beautifully styled HTML page with proper formatting, code highlighting, and responsive design.
Convert README.md files to HTML for hosting or sharing:
python md_to_html.py README.md docs.html "Project Documentation"Transform Markdown blog posts to HTML:
python md_to_html.py post.md blog-post.html "My Blog Post"Create styled HTML versions of your notes:
python md_to_html.py notes.md study-guide.html "Study Guide"Generate simple project pages:
python md_to_html.py project.md index.html "My Project"The CSS is embedded in the HTML template within the script. To customize styling:
- Open
md_to_html.py - Locate the
html_templatevariable in theconvert_markdown_to_htmlfunction - Modify the
<style>section to your preferences - Run the conversion again
Dark mode:
body {
background-color: #1e1e1e;
color: #d4d4d4;
}Custom fonts:
body {
font-family: 'Georgia', serif;
}Custom colors:
a {
color: #ff6b6b;
}The converter uses the following Python Markdown extensions:
- extra: Enables tables, footnotes, definition lists, and more
- codehilite: Provides syntax highlighting for code blocks
To add more extensions, modify the markdown.markdown() call in the script:
html_body = markdown.markdown(md_content, extensions=['extra', 'codehilite', 'toc'])Available extensions: toc (table of contents), abbr (abbreviations), attr_list (attribute lists), and more.
To convert multiple Markdown files at once, use a shell script:
Linux/Mac:
for file in *.md; do
python md_to_html.py "$file"
doneWindows (PowerShell):
Get-ChildItem *.md | ForEach-Object {
python md_to_html.py $_.Name
}The script handles common errors gracefully:
- File not found: Clear error message if input file doesn't exist
- Permission errors: Reports issues with reading/writing files
- Encoding issues: Uses UTF-8 encoding by default
- Invalid Markdown: Processes what it can, ignoring invalid syntax
- Preview in browser: Open the generated HTML in a browser to verify formatting
- Validate Markdown: Use a Markdown linter before conversion for best results
- Image paths: Use relative paths for images to maintain portability
- Large files: The converter handles large Markdown files efficiently
- Version control: Keep both .md and .html files if tracking changes
| Feature | This Tool | Pandoc | Online Converters |
|---|---|---|---|
| Offline | ✅ | ✅ | ❌ |
| Customizable | ✅ | ✅ | ❌ |
| Lightweight | ✅ | ❌ | ✅ |
| Built-in styling | ✅ | ❌ | ✅ |
| Command-line | ✅ | ✅ | ❌ |
| Python API | ✅ | ❌ | ❌ |
Problem: Code blocks appear without syntax highlighting
Solution: Install the Pygments library for enhanced highlighting:
pip install PygmentsProblem: Special characters show as � or boxes
Solution: The script uses UTF-8 by default. Ensure your Markdown file is saved as UTF-8.
Problem: HTML shows plain text without styling
Solution: The CSS is embedded in the HTML. Check that the output file was fully written.
Problem: ModuleNotFoundError: No module named 'markdown'
Solution: Install the markdown library:
pip install markdown