搜索

PythonReactSEOPDF
OpenClaw

Html Cn Render Fix

🔧 HTML 转图片中文无乱码解决方案

bo1708140 来源安装数☆ 0 仓库 Stars

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

SkillHub CLI

skillhub install bo170814/html-cn-render-fix

打开来源仓库,查看说明和兼容环境后,按来源提供的命令或步骤安装。

将来源链接提供给你的 Agent,让它先读取说明、核对依赖,并向你说明将执行的安装步骤。

查看来源 ↗使用指南

技能说明

🔧 HTML 转图片中文无乱码解决方案

解决 Python 生成图片时中文显示为方框/乱码的问题

问题背景

在使用 matplotlib、pyppeteer 等工具生成包含中文的图片时,经常遇到:

  • ❌ 中文显示为方框 □□□
  • ❌ 部分字符显示为乱码
  • ❌ emoji 显示异常

根本原因

  1. 字体缺失 - 系统没有安装中文字体
  2. 字体配置错误 - matplotlib 默认使用 DejaVu 字体(不支持中文)
  3. 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
impor...

来源信息

Source
github
Repository
openclaw/skills
Path
skills/bo170814/html-cn-render-fix
Version
—
Commit
a9a838eab331fbefdac4a1644408886aec42fe4f
分类来源
rules
状态
active
可见性
public

这些信息来自收录时的来源分析,可能滞后;使用前请核对最新来源文件。

README

🔧 HTML 转图片中文无乱码解决方案

快速解决 Python 生成图片时中文显示为方框/乱码的问题。

快速使用

1. 安装中文字体
# Ubuntu/Debian
apt-get install fonts-noto-cjk -y

# CentOS/RHEL
yum install google-noto-sans-cjk-fonts -y
2. 使用示例
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('中文标题', fontproperties=font_prop)
ax.set_xlabel('X 轴', fontproperties=font_prop)

核心要点

  1. 使用 FontProperties 直接加载字体文件
  2. 避免使用复杂 emoji(特别是国旗 emoji)
  3. 每个文本元素都要设置 fontproperties

详细文档请查看 SKILL.md

许可证

MIT

来源内容

SKILL.md
# 🔧 HTML 转图片中文无乱码解决方案

> 解决 Python 生成图片时中文显示为方框/乱码的问题

## 问题背景

在使用 matplotlib、pyppeteer 等工具生成包含中文的图片时,经常遇到:
- ❌ 中文显示为方框 □□□
- ❌ 部分字符显示为乱码
- ❌ emoji 显示异常

## 根本原因

1. **字体缺失** - 系统没有安装中文字体
2. **字体配置错误** - matplotlib 默认使用 DejaVu 字体(不支持中文)
3. **emoji 兼容性问题** - 某些 emoji 在某些字体/系统中不支持

## 解决方案

### 方案 1:使用 FontProperties 直接加载字体文件(推荐)

```python
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(不推荐)

```python
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 🇨🇳🇺🇸)在某些系统中不支持:

```python
# ❌ 避免使用
title = "🇨🇳 中国股票"

# ✅ 使用文字代替
title = "中国股票"

# ✅ 或使用通用 emoji
title = "📈 中国股票"  # 图表 emoji 兼容性更好
```

## 完整示例

```python
#!/usr/bin/env python3
"""
HTML 转图片中文无乱码示例
"""

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import io
impor...
README
# 🔧 HTML 转图片中文无乱码解决方案

快速解决 Python 生成图片时中文显示为方框/乱码的问题。

## 快速使用

### 1. 安装中文字体

```bash
# Ubuntu/Debian
apt-get install fonts-noto-cjk -y

# CentOS/RHEL
yum install google-noto-sans-cjk-fonts -y
```

### 2. 使用示例

```python
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('中文标题', fontproperties=font_prop)
ax.set_xlabel('X 轴', fontproperties=font_prop)
```

## 核心要点

1. **使用 FontProperties 直接加载字体文件**
2. **避免使用复杂 emoji(特别是国旗 emoji)**
3. **每个文本元素都要设置 fontproperties**

详细文档请查看 SKILL.md

## 许可证

MIT

相关技能

OpenClaw

bambu-studio-ai

heyixuan2

Bambu Lab 3D printer control and automation. Activate when user mentions: printer status, 3D printing, slice, analyze model, generate 3D, AMS filament, print monitor, Bambu Lab, or any 3D printing task. Full pi...

OpenClaw

geo-optimization

capt-marbles

Generative Engine Optimization (GEO) for AI search visibility. Optimize content to appear in ChatGPT, Perplexity, Claude, and Google AI Overviews. Use when optimizing websites, pages, or content for LLM discove...

OpenClaw

0protocol

0isone

Agents can sign plugins, rotate credentials without losing identity, and publicly attest to behavior.

OpenClaw

0x0-messenger

eijiac24

Send and receive P2P messages using disposable numbers and PINs. No servers, no accounts. Use for human notifications, approval flows, and agent-to-agent communication.