微信公众号开发 SDK - 支持文章发布、图片上传等功能
Project description
WeChat Publisher
一个简单易用的微信公众号开发 Python SDK,支持文章发布、图片上传、草稿管理等功能。
功能特性
- 🚀 支持从 Markdown 和 HTML 创建微信草稿
- 🖼️ 自动处理图片上传和压缩
- 📝 支持多篇文章批量发布
- 💾 Token 自动缓存和刷新
- 🛡️ 完善的错误处理
安装
从 PyPI 安装
pip install wechat-publisher
从 GitHub 安装
pip install git+https://github.com/penxxy/wechat-publisher.git
使用方法
基本使用
from wechat_publisher import WeChatPublisher, Article
# 初始化发布器
publisher = WeChatPublisher(
appid="your_appid",
secret="your_secret"
)
# 创建文章
articles: list[Article] = [{
"title": "我的第一篇文章",
"content": "这是文章内容",
"type": "html", # 或 "markdown"
"author": "作者名",
"thumb_media_id": None # 可选,封面图片ID
}]
# 创建草稿
media_id = publisher.create_draft_from_articles(articles)
print(f"草稿创建成功,media_id: {media_id}")
高级使用
# 上传单独图片
image_url = publisher.upload_image("path/to/image.jpg")
print(f"图片上传成功: {image_url}")
# 从 URL 上传图片
image_url = publisher.upload_image("https://example.com/image.jpg")
配置说明
appid: 微信公众号的 AppIDsecret: 微信公众号的 AppSecrettoken_cache_path: Access Token 缓存文件路径(可选,默认 ".token_cache")
文章格式
文章对象支持以下字段:
from wechat_publisher import Article
article: Article = {
"title": "文章标题", # 必填:字符串
"content": "文章内容", # 必填:字符串
"type": "html", # 必填: "html" 或 "markdown"
"author": "作者名", # 必填:字符串
"thumb_media_id": None, # 可选:封面图片ID,字符串或None
}
注意事项
- 需要在微信公众平台开启开发者模式
- 确保服务器IP在微信白名单中
- 图片大小建议小于2MB,支持 jpg、png 格式
- Access Token 会自动缓存,有效期7200秒
许可证
MIT License
贡献
欢迎提交 Issue 和 Pull Request!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wechat_publisher-0.1.1.tar.gz.
File metadata
- Download URL: wechat_publisher-0.1.1.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe80e0013808bbd4742c483496c549a9330f2260c01a45ef244798a35bbbe379
|
|
| MD5 |
d342e9dae2fe9b6af1a5b407323eb3f5
|
|
| BLAKE2b-256 |
6317b24749477273266cb0d0eac77f607620f31f5c9042bf570738ed5e97d04e
|
File details
Details for the file wechat_publisher-0.1.1-py3-none-any.whl.
File metadata
- Download URL: wechat_publisher-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98dca931f0afbf1072f282c384efc4e1766f7a2326fe8eaf7bf3d38e57ebda60
|
|
| MD5 |
08cbaedd4f88ca4f6b3fbae7c4a27bf5
|
|
| BLAKE2b-256 |
47105aa0604593cdfa98638cef6f64e46a84773c00b7d5024b043021ef32030b
|