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.0
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.0.tar.gz | 21.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wechat_formatter-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 43.3 kB
Release files / wechat_formatter-1.0.0.tar.gz
| Download URL | wechat_formatter-1.0.0.tar.gz |
|---|---|
| Size | 21.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
564545127ef7f59dcffe45430da9f3b31cd0cc8b0863103f3cb7124854678f99
|
|
BLAKE2b-256 checksum How to use checksums |
2afa3746f694942ebddf7ac11a133d07d5b51ed970157e7efd2b1d39259e80f0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Release files / wechat_formatter-1.0.0-py3-none-any.whl
| Download URL | wechat_formatter-1.0.0-py3-none-any.whl |
|---|---|
| Size | 21.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fe2db97368439f497bf930ace4953c2b758527c563d2695d9e39510585bfaf5
|
|
BLAKE2b-256 checksum How to use checksums |
bd63543611a69b3f137ea7c333173f2418f688064bcb07af0d4cf6c3d99c4266
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|