Skip to main content

wechat-md

PyPI Python License: MIT CI

Markdown → 微信公众号 HTML 渲染器。

输出全部行内 style、零 class、零外部 CSS 的 HTML,粘贴进微信公众号编辑器即为最终排版——标题、代码块、表格、引用、列表样式完整保留,不依赖任何外部资源。

为什么需要它

微信公众号编辑器会过滤 class 属性与外部样式表。通用 Markdown 渲染器输出的高亮 class 会被 strip,导致代码块排版错乱;依赖外部 CSS 的主题在公众号里整体失效。

wechat-md 针对公众号编辑器逐项适配,所有样式内联在元素上,输出即终稿。

安装

pip install wechat-md

生产环境锁定版本(升级是显式决策,不隐性漂移):

pip install wechat-md==0.4.0

引用 GitHub 源时锁 tag(.../archive/refs/tags/vX.Y.Z.tar.gz),不要引滚动的 main。

快速开始

from wechat_md import markdown_to_html

html = markdown_to_html("# 标题\n\n正文…")

返回的 HTML 可直接作为公众号草稿正文提交,或粘贴进编辑器。

主题(可选)

传入 theme 即可按主题配色渲染(键与 wechat-publish-service 的 ThemeConfig 对齐):

html = markdown_to_html(
    md,
    theme={"h1_color": "#3498db", "h2_color": "#3498db", "strong_color": "#3498db"},
)

支持键:h1_color / h2_color / h3_color / strong_color / quote_bg / quote_border / code_bg / code_font_size / text_color。未提供或未知键保持默认; 不传 theme 时输出与历史版本逐字节一致。

排版规则

Markdown 元素 公众号渲染效果
文首 H1 移除(标题由草稿字段承载,正文不重复)
H1 / H2 加粗 + 左侧红色竖条(border-left,复制时不产生多余换行)
H3 ▪ 前缀加粗
代码块 灰底圆角卡片、等宽字体、缩进以   保留
引用块 灰底 + 左红边卡片
表格 全边框、表头灰底
链接 公众号蓝(#576b95)、可点击
列表 行内样式,并修复渲染器把 - 行并进段落的伪列表问题
--- 分隔线 移除(编辑器渲染为冗余灰线)
连续「角色名:」行 自动聚合成对话卡片
标题后紧跟加粗小节 自动聚合成结论区卡片

设计约束

  • 纯函数、无网络、无配置——markdown_to_html(text, theme=None) 一个入口;主题为可选配色字典,由调用方传入。
  • 唯一运行时依赖 markdown2(未安装时自动回退 markdown)。
  • 兼容 Python 3.9+。

开发

pip install -e ".[dev]"
pytest

质量门:ruff(E,W,F,I,B,C4,UP,SIM)+ mypy --strict + pytest 全绿后才发版。

License

MIT

Metadata

Release files for wechat-md 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for wechat-md 0.4.0
File Size Uploaded Provenance Actions
wechat_md-0.4.0.tar.gz 15.9 kB With provenance Details for wechat_md-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wechat-md 0.4.0
File Interpreter ABI Platform Actions
wechat_md-0.4.0-py3-none-any.whl Python 3 none any Details for wechat_md-0.4.0-py3-none-any.whl

Total release size: 28.1 kB

Release files / wechat_md-0.4.0.tar.gz

Download URL wechat_md-0.4.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
112a482b45e4711ceb1d76cc9ebcee736679c7c3256298f6d83bf85964c02301
BLAKE2b-256 checksum
How to use checksums
f1d61d6659210aca72ace20aa874152818f38afefb99eb510de8f84a72f5e639
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 23, 2026.

Transparency log

Release files / wechat_md-0.4.0-py3-none-any.whl

Download URL wechat_md-0.4.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2992920b73ad7e21cdd31617389832316140111e4f21591809544522add9f4bd
BLAKE2b-256 checksum
How to use checksums
dcca80a49a960774652983ad81e1a66c6b31ef52598de1fef764c5f264df9cb1
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 23, 2026.

Transparency log

Security for wechat-md 0.4.0

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for wechat_md-0.4.0.tar.gz

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 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page