Html Cn Render Fix
Skill by bo170814
Install via CLI (Recommended)
clawhub install openclaw/skills/skills/bo170814/html-cn-render-fix🔧 HTML 转图片中文无乱码解决方案
解决 Python 生成图片时中文显示为方框/乱码的问题
问题背景
在使用 matplotlib、pyppeteer 等工具生成包含中文的图片时,经常遇到:
- ❌ 中文显示为方框 □□□
- ❌ 部分字符显示为乱码
- ❌ emoji 显示异常
根本原因
- 字体缺失 - 系统没有安装中文字体
- 字体配置错误 - matplotlib 默认使用 DejaVu 字体(不支持中文)
- emoji 兼容性问题 - 某些 emoji 在某些字体/系统中不支持
解决方案
方案 1:使用 FontProperties 直接加载字体文件(推荐)
from matplotlib.font_manager import FontProperties
# 直接加载字体文件
font_path = '/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc'
font_prop = FontProperties(fname=font_path)
# 在每个文本渲染处使用
fig.suptitle('中文标题', fontsize=16, fontproperties=font_prop)
ax.set_xlabel('X 轴标签', fontproperties=font_prop)
ax.set_ylabel('Y 轴标签', fontproperties=font_prop)
ax.annotate('标注文字', xy=(x, y), fontproperties=font_prop)
# 设置图例
leg = ax.legend()
for text in leg.get_texts():
text.set_fontproperties(font_prop)
# 设置坐标轴标签
for label in ax.get_xticklabels():
label.set_fontproperties(font_prop)
优点:
- ✅ 不依赖系统字体配置
- ✅ 明确指定字体文件,可靠性高
- ✅ 适用于所有 matplotlib 文本元素
方案 2:配置 rcParams(不推荐)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Noto Sans CJK SC', 'SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
缺点:
- ❌ 依赖系统字体配置
- ❌ 可能被其他配置覆盖
- ❌ 在某些环境下无效
方案 3:避免使用 emoji(最安全)
某些 emoji(特别是国旗 emoji 🇨🇳🇺🇸)在某些系统中不支持:
# ❌ 避免使用
title = "🇨🇳 中国股票"
# ✅ 使用文字代替
title = "中国股票"
# ✅ 或使用通用 emoji
title = "📈 中国股票" # 图表 emoji 兼容性更好
完整示例
#!/usr/bin/env python3
"""
HTML 转图片中文无乱码示例
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import io
import base64
# 加载字体文件
font_path = '/usr/share/fonts/google-noto-cjk/NotoSansCJK-Regular.ttc'
font_prop = FontProperties(fname=font_path)
font_prop_title = FontProperties(fname=font_path, size=16, weight='bold')
font_prop_label = FontProperties(fname=font_path, size=12)
# 创建图表
fig, ax = plt.subplots(figsize=(10, 6))
fig.suptitle('中文标题示例', fontsize=16, fontproperties=font_prop_title)
# 绘制数据
ax.plot([1, 2, 3, 4], [1, 4, 2, 3], label='数据曲线')
# 设置标签(使用 fontproperties)
ax.set_xlabel('X 轴', fontsize=12, fontproperties=font_prop_label)
ax.set_ylabel('Y 轴', fontsize=12, fontproperties=font_prop_label)
ax.set_title('图表标题', fontsize=12, fontproperties=font_prop_label)
# 设置图例
leg = ax.legend()
for text in leg.get_texts():
text.set_fontproperties(font_prop)
# 设置坐标轴刻度标签
for label in ax.get_xticklabels():
label.set_fontproperties(font_prop)
for label in ax.get_yticklabels():
label.set_fontproperties(font_prop)
# 添加标注
ax.annotate('最高点', xy=(2, 4), xytext=(2.5, 4.5),
fontproperties=font_prop,
arrowprops=dict(arrowstyle='->'))
# 保存为 base64
buf = io.BytesIO()
plt.savefig(buf, dpi=100, bbox_inches='tight', format='png')
buf.seek(0)
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close()
print(f"✅ 生成成功!图片大小:{len(img_base64)} bytes")
系统字体安装
Ubuntu/Debian
Metadata
Not sure this is the right skill?
Describe what you want to build — we'll match you to the best skill from 16,000+ options.
Find the right skillPaste this into your clawhub.json to enable this plugin.
{
"plugins": {
"official-bo170814-html-cn-render-fix": {
"enabled": true,
"auto_update": true
}
}
}Related Skills
evomap-auto-task-publish
EvoMap 自动任务执行器 v3.0 - 深度整合版。心跳保活 | Webhook 通知 | Swarm 协作 | 智能筛选
feishu-img-tool
Feishu image upload and send tool. Send images to Feishu chats by uploading first then sending with image_key. Usage: `node feishu-image-tool.js send --target <open_id> --file <path>`
stock-daily-report
A 股每日报告生成器 - 含 K 线图、技术指标、市场分析(仅供学习参考)
evomap-lite-client
EvoMap 轻量客户端 - 完整功能版。支持任务循环、心跳保活、Webhook 通知、Swarm 协作、收益追踪等。基于 GEP-A2A 协议。
Agent Browser
A fast Rust-based headless browser automation CLI with Node.js fallback that enables AI agents to navigate, click, type, and snapshot pages via structured commands.