A robust, local-only Mermaid to PNG converter with comprehensive Markdown processing support. Convert Mermaid diagrams seamlessly without any web requests or external dependencies beyond your local Mermaid CLI installation.
- π Pure Local Operation: No web requests - exclusively uses local Mermaid CLI
- π‘οΈ Robust Error Handling: Comprehensive exception hierarchy with detailed error messages and validation
- π Markdown File Processing: Automatically detects and processes Mermaid code blocks in
.md
files - βοΈ Flexible Configuration: Customizable diagram appearance (themes, sizes, backgrounds)
- π Batch Processing: Convert multiple files and directories with single commands
- π Metadata Support: Configure diagrams using code block metadata
- π§Ή Resource Management: Proper temporary file cleanup with context manager support
- π Detailed Reporting: Structured results with execution metrics and success rates
- Node.js (v14 or higher) - Download here
- Mermaid CLI:
npm install -g @mermaid-js/mermaid-cli
pip install pymmdc
# Convert single Mermaid file
pymmdc diagram.mmd diagram.png
# Process Markdown file with Mermaid blocks
pymmdc document.md --output-dir ./images
# Batch process multiple Markdown files
pymmdc "docs/*.md" --batch --output-dir ./generated_images
# Replace Mermaid blocks with images in output
pymmdc README.md --replace-blocks --output-dir ./assets
# Custom theme and dimensions
pymmdc diagram.mmd --theme dark --width 1600 --height 1200
from pymmdc import EnhancedMermaidConverter
# Simple conversion
with EnhancedMermaidConverter() as converter:
result = converter.convert_file("diagram.mmd", "output.png")
# Markdown processing
result = converter.convert_file(
"document.md",
process_markdown=True,
output_dir="./images",
replace_blocks=True
)
# Batch processing
results = converter.batch_convert(["file1.md", "file2.mmd"])
# Convert .mmd or .mermaid files to PNG
pymmdc flowchart.mmd flowchart.png
pymmdc sequence.mermaid sequence.png
Process Markdown files containing Mermaid code blocks:
# My Document
Here's a flowchart:
```mermaid
graph TD
A[Start] --> B{Decision}
B -->|Yes| C[Action 1]
B -->|No| D[Action 2]
```
And a sequence diagram:
```mermaid
sequenceDiagram
Alice->>Bob: Hello Bob!
Bob-->>Alice: Hello Alice!
```
# Process all Mermaid blocks in Markdown
pymmdc document.md --output-dir ./diagrams
# Replace code blocks with images in new Markdown file
pymmdc document.md --replace-blocks --output-dir ./assets
Use metadata in code blocks for per-diagram settings:
```mermaid { "theme": "dark", "width": 1600, "title": "System Architecture" }
graph TB
subgraph Frontend
A[Web App]
B[Mobile App]
end
subgraph Backend
C[API Server]
D[Database]
end
A --> C
B --> C
C --> D
```
Flag | Description | Default |
---|---|---|
--timeout |
Conversion timeout in seconds | 60 |
--width |
Diagram width in pixels | 1200 |
--height |
Diagram height in pixels | 800 |
--theme |
Diagram theme (default , dark , forest , neutral ) |
default |
--bg-color |
Background color | transparent |
--output-dir |
Output directory for images | Current directory |
--replace-blocks |
Replace code blocks with images in Markdown | False |
--batch |
Process multiple files | False |
--verbose |
Enable detailed output | False |
Configure diagrams using JSON metadata in code blocks:
{
"theme": "dark",
"width": 1600,
"height": 900,
"title": "My Diagram",
"bgcolor": "white"
}
# Process all Markdown files in a directory
pymmdc "project/docs/**/*.md" --batch --recursive --output-dir ./generated
# Dry run to see what would be processed
pymmdc "*.md" --batch --dry-run
# Custom output directory and theme
pymmdc "**/*.md" --batch --output-dir ./docs/assets --theme forest
# Pre-process documentation before building
pymmdc "docs/**/*.md" --batch --replace-blocks --output-dir docs/assets
# CI/CD integration example
pymmdc README.md --output-dir ./badges || echo "Diagram generation failed"
-
Mermaid CLI not found:
npm install -g @mermaid-js/mermaid-cli
-
Node.js version too old:
# Update Node.js npm install -g n n stable
-
Permission errors:
# Ensure write permissions in output directory chmod +w ./output-directory
Enable verbose logging for troubleshooting:
pymmdc document.md --verbose --output-dir ./debug
Successful processing provides detailed reports:
Markdown Processing Complete!
File: document.md
Blocks found: 3
Blocks converted: 3
Success rate: 100.0%
Output files:
- document_architecture.png
- document_flowchart.png
- document_sequence.png
We welcome contributions! Please see our contributing guidelines for details.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
See the LICENSE file for more details. See the LICENSE file for details.
- π Documentation
- π Issue Tracker
- π¬ Discussions
PyMMDC: Making Mermaid diagram conversion reliable, fast, and completely local. π