wechat-formatter
把 Markdown 渲染成内联样式 HTML,可直接复制粘贴到微信公众号编辑器。
基于 mistune,内置 6 种视觉风格 × 12 种配色 = 72 套模板,所有 CSS 都写在 style="..." 内联属性里,不依赖任何外部 CSS 文件或 JS。
安装
pip install wechat-formatter
或从源码安装:
cd wechat-formatter
pip install .
快速开始
from wechat_formatter import get_template, render_article, FormatTweaks
markdown_text = """\
# 你好,公众号
这是一段**普通文字**,包含 ==高亮== 内容和 ^上标^、~下标~。
## 一个小标题
- 列表项一
- 列表项二
- ~~删除线~~
> 这是一段引用
```python
print("hello")
"""
按中文风格名 + 颜色名选择模板
template = get_template("极简风", "经典")
可选:覆盖字号、行高、边距等
tweaks = FormatTweaks( fontSize=15, lineHeight=1.75, paragraphSpacing=16, )
html = render_article(markdown_text, template, tweaks)
把 html 写入文件,用浏览器打开后全选复制,粘贴到公众号编辑器
with open("article.html", "w", encoding="utf-8") as f: f.write(html)
## 模板选择
`get_template(category, color)` 接受中文风格名或英文 id:
| 风格中文名 | 英文 id |
|---|---|
| 新粗野风 | `neo-brutalism` |
| 极简风 | `minimalist` |
| 商务风 | `business` |
| 文艺风 | `literary` |
| 科技风 | `tech` |
| 节庆风 | `festive` |
颜色名(12 种):`经典`、`雅致`、`先锋`、`深邃`、`晨光`、`星穹`、`暖阳`、`暮色`、`清泉`、`破晓`、`璀璨`、`幽蓝`。
也可以遍历全部模板:
```python
from wechat_formatter import all_templates, grouped_templates
for tpl in all_templates:
print(tpl.category, tpl.name, tpl.themeColor)
# 按风格分组
for group in grouped_templates:
print(group["name"], len(group["templates"]))
FormatTweaks 参数
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
themeColor |
str | None |
None |
覆盖模板主题色(hex) |
fontSize |
int |
15 |
正文字号 px |
lineHeight |
float |
1.75 |
行高 |
paragraphSpacing |
int |
16 |
段间距 px |
firstLineIndent |
bool |
False |
首行缩进 2em |
letterSpacing |
float |
0.5 |
字间距 px |
imageRadius |
int |
4 |
图片圆角 px |
h1Layout |
str | None |
None |
一级标题对齐:left/center/right |
h2Layout |
str | None |
None |
二级标题对齐 |
pagePaddingTop |
int |
20 |
页面上边距 px |
pagePaddingRight |
int |
20 |
页面右边距 px |
pagePaddingBottom |
int |
20 |
页面下边距 px |
pagePaddingLeft |
int |
20 |
页面左边距 px |
支持的 Markdown 语法
- 标准:标题
#/##/###、段落、加粗、斜体、行内代码、链接、图片、引用、有序/无序列表、分割线---、表格 - 扩展:
==高亮==→<mark>^上标^→<sup>~下标~→<sub>~~删除线~~→<del>- 任务列表(
- [x]/- [ ]) - 代码块带 macOS 风格圆点头部
- 段落中多图自动排成一行
API 一览
from wechat_formatter import (
# 渲染
render_article,
# 模板
all_templates, # list[TemplateConfig]
grouped_templates, # list[dict],按风格分组
get_template, # (category, color) -> TemplateConfig
build_template, # 自定义构建
get_styles_by_category,
# 类型
TemplateConfig,
FormatTweaks,
BaseStyle,
# 颜色
color_palettes,
categories_list,
names,
hex_to_rgb,
hex_to_rgba,
# 工具
get_style_value,
ensure_style_value,
bg_fallback,
)
技术交流
扫码添加微信,交流使用问题、模板定制与合作:
项目赞助
本项目由微信公众号 「程序员白大力」 提供赞助,感谢支持:
License
MIT
Release files for wechat-formatter 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wechat_formatter-1.0.1.tar.gz | 22.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wechat_formatter-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 44.0 kB
Release files / wechat_formatter-1.0.1.tar.gz
| Download URL | wechat_formatter-1.0.1.tar.gz |
|---|---|
| Size | 22.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6ec5a7faa6b4dcf601faf996eeb7b45dc8a99e312f20d6845c20018779b52839
|
|
BLAKE2b-256 checksum How to use checksums |
913c3095af74599437152945d2ba334ba0197cb4cd40e3dea5d4da025ba5ad37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.
Transparency logRelease files / wechat_formatter-1.0.1-py3-none-any.whl
| Download URL | wechat_formatter-1.0.1-py3-none-any.whl |
|---|---|
| Size | 21.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b30e13b4fe3106227ac3d83402bc593196b75f3985c6451995774f05db91f928
|
|
BLAKE2b-256 checksum How to use checksums |
a15d63237f5ce093c72d379f96bc159e9f727db2c37fe10cf41f16ea76cb3f59
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 22, 2026.
Transparency log