42 lines
1004 B
Python
42 lines
1004 B
Python
"""Markdown to PNG converter
|
|
|
|
Dependencies:
|
|
- pip install markdown2image playwright && playwright install chromium
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from markdown2image import Markdown2Image
|
|
|
|
|
|
def markdown_to_png(md_text, img_path):
|
|
"""将 Markdown 渲染为 PNG 图片"""
|
|
Path(img_path).parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
m2i = Markdown2Image()
|
|
m2i.b64_decode_and_dump(markdown_text=md_text, output_path=img_path)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print("Usage: python md_convert.py <input.md> <output.png>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
input_path = sys.argv[1]
|
|
output_path = sys.argv[2]
|
|
|
|
if not os.path.exists(input_path):
|
|
print(f"Error: Input file not found: {input_path}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
md_text = Path(input_path).read_text(encoding="utf-8")
|
|
markdown_to_png(md_text, output_path)
|
|
|
|
print(f"Converted: {input_path} -> {output_path}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|