Skip to main content

微信公众号开发 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: 微信公众号的 AppID
  • secret: 微信公众号的 AppSecret
  • token_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
}

注意事项

  1. 需要在微信公众平台开启开发者模式
  2. 确保服务器IP在微信白名单中
  3. 图片大小建议小于2MB,支持 jpg、png 格式
  4. Access Token 会自动缓存,有效期7200秒

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wechat_publisher-0.1.1.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wechat_publisher-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

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

Hashes for wechat_publisher-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fe80e0013808bbd4742c483496c549a9330f2260c01a45ef244798a35bbbe379
MD5 d342e9dae2fe9b6af1a5b407323eb3f5
BLAKE2b-256 6317b24749477273266cb0d0eac77f607620f31f5c9042bf570738ed5e97d04e

See more details on using hashes here.

File details

Details for the file wechat_publisher-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for wechat_publisher-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98dca931f0afbf1072f282c384efc4e1766f7a2326fe8eaf7bf3d38e57ebda60
MD5 08cbaedd4f88ca4f6b3fbae7c4a27bf5
BLAKE2b-256 47105aa0604593cdfa98638cef6f64e46a84773c00b7d5024b043021ef32030b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page