125 lines
4.0 KiB
Python
125 lines
4.0 KiB
Python
import markdown
|
||
from playwright.sync_api import sync_playwright
|
||
import os
|
||
|
||
|
||
# 将 Markdown 字符串转换为 HTML
|
||
def md_str_to_html(md_content, output_html):
|
||
"""
|
||
将 Markdown 字符串转换为 HTML 文件,并添加支持中文和 Emoji 的样式。
|
||
|
||
:param md_content: 输入的 Markdown 字符串
|
||
:param output_html: 输出的 HTML 文件路径
|
||
"""
|
||
# 转换 Markdown 为 HTML,启用额外功能(如表格、代码高亮)
|
||
html_content = markdown.markdown(md_content, extensions=['extra', 'codehilite'])
|
||
|
||
# 添加基本的 HTML 结构和样式,支持中文和 Emoji
|
||
css = """
|
||
<style>
|
||
body {
|
||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||
padding: 20px;
|
||
line-height: 1.6;
|
||
max-width: 700px;
|
||
margin: 0 auto;
|
||
}
|
||
pre, code {
|
||
background-color: #f5f5f5;
|
||
padding: 10px;
|
||
border-radius: 5px;
|
||
font-family: 'Courier New', Courier, monospace;
|
||
}
|
||
table {
|
||
border-collapse: collapse;
|
||
width: 100%;
|
||
}
|
||
th, td {
|
||
border: 1px solid #ddd;
|
||
padding: 8px;
|
||
text-align: left;
|
||
}
|
||
th {
|
||
background-color: #f2f2f2;
|
||
}
|
||
/* 确保 Emoji 正确渲染 */
|
||
span, p, li {
|
||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||
}
|
||
</style>
|
||
"""
|
||
|
||
# 写入 HTML 文件
|
||
with open(output_html, 'w', encoding='utf-8') as f:
|
||
f.write('<html><head>')
|
||
f.write('<meta charset="UTF-8">') # 确保 UTF-8 编码
|
||
f.write(css)
|
||
f.write('</head><body>')
|
||
f.write(html_content)
|
||
f.write('</body></html>')
|
||
|
||
|
||
# 使用 Playwright 将 HTML 渲染并截图
|
||
def html_to_image(html_file, output_image):
|
||
"""
|
||
使用 Playwright 加载 HTML 文件并截图。
|
||
|
||
:param html_file: 输入的 HTML 文件路径
|
||
:param output_image: 输出的图片文件路径
|
||
"""
|
||
with sync_playwright() as p:
|
||
# TODO 这里使用的比较呆的固定路径,后期优化
|
||
browser = p.chromium.launch(executable_path=r"C:\Users\Liu_WIN10\AppData\Local\Google\Chrome\Application\chrome.exe")
|
||
page = browser.new_page()
|
||
|
||
# 加载本地的 HTML 文件
|
||
page.goto(f'file://{os.path.abspath(html_file)}')
|
||
|
||
# 设置 viewport(可选,根据需要调整)
|
||
page.set_viewport_size({"width": 900, "height": 700})
|
||
|
||
# 截图
|
||
page.screenshot(path=output_image, full_page=True)
|
||
|
||
browser.close()
|
||
|
||
|
||
# 主函数:从字符串转换 Markdown 到图片
|
||
def convert_md_str_to_image(md_content, output_image):
|
||
"""
|
||
将 Markdown 字符串转换为图片。
|
||
|
||
:param md_content: 输入的 Markdown 字符串
|
||
:param output_image: 输出的图片文件路径
|
||
"""
|
||
# 中间生成的 HTML 文件
|
||
temp_html = 'temp_output.html'
|
||
|
||
# 第一步:将 Markdown 字符串转换为 HTML
|
||
md_str_to_html(md_content, temp_html)
|
||
|
||
# 第二步:将 HTML 渲染为图片
|
||
html_to_image(temp_html, output_image)
|
||
|
||
# 可选:删除临时的 HTML 文件
|
||
os.remove(temp_html)
|
||
print(f"图片已生成:{output_image}")
|
||
return os.path.abspath(output_image)
|
||
|
||
# 示例使用
|
||
if __name__ == "__main__":
|
||
# 示例 Markdown 字符串(包含中文和 Emoji)
|
||
md_content = """
|
||
# 示例标题
|
||
这是一个示例 Markdown 字符串,包含中文和 Emoji 😊🚀。
|
||
|
||
## 子标题
|
||
- 项目 1:学习 Python 🐍
|
||
- 项目 2:编写代码 💻
|
||
|
||
### 代码示例
|
||
```python
|
||
print("你好,世界!🌍")
|
||
"""
|
||
spath = convert_md_str_to_image(md_content, "output.png")
|
||
print(spath) |