要使用Python发送微信消息,通常需要借助第三方库,如`itchat`。然而,由于你要求不使用任何外部包,我们将无法直接通过API发送消息。不过,我可以提供一个基于`itchat`的示例代码,你可以在本地安装`itchat`库后运行它。
以下是一个简单的示例,展示了如何使用`itchat`库发送文字、图片和文件消息给指定好友和微信群。请记住,这个代码需要你先安装`itchat`库,并且需要你的微信扫码登录。
```python
from itchat.content import TEXT, PICTURE, FILE
from itchat import login
# 登录微信
login()
# 获取好友列表和微信群列表
friends = itchat.get_friends(update=True)
group_list = itchat.get_group_list()
# 发送文字消息
def send_text(message, to_user):
itchat.send(message, to_user)
# 发送图片消息
def send_image(image_path, to_user):
itchat.send(image_path, to_user, file_type=PICTURE)
# 发送文件消息
def send_file(file_path, to_user):
itchat.send(file_path, to_user, file_type=FILE)
# 发送消息给好友
for friend in friends:
friend_id = friend['UserName']
# 发送文字消息
send_text('Hello, this is a text message!', friend_id)
# 发送
相关内容:
本示例是调用Windows API模拟发送,用Python调用win32api这个库来调用Windows API模拟人的手动操作来发送消息。
在使用前,请将你微信的窗口设置为在最前面,这样就便于程序找到你的窗口!
私信小编01即可获取大量Python学习资源

import pyautogui
import time
import win32api
import win32con
import win32gui
import win32clipboard as w
def FindWindow(chatroom):
win = win32gui.FindWindow(None, chatroom)
print("找到窗口句柄:%x" % win)
if win != 0:
win32gui.ShowWindow(win, win32con.SW_SHOWMINIMIZED)
win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)
win32gui.ShowWindow(win, win32con.SW_SHOW)
win32gui.SetWindowPos(win, win32con.HWND_TOPMOST, 0, 0, 300, 500, win32con.SWP_SHOWWINDOW)
win32gui.SetForegroundWindow(win) # 获取控制
time.sleep(1)
tit = win32gui.GetWindowText(win)
print('已启动【'+str(tit)+'】窗口')
else:
print('找不到【%s】窗口' % chatroom)
exit()
# 设置和粘贴剪贴板
def ClipboardText(ClipboardText):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, ClipboardText)
w.CloseClipboard()
time.sleep(1)
win32api.keybd_event(17,0,0,0)
win32api.keybd_event(86,0,0,0)
win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
# 模拟发送动作
def SendMsg():
win32api.keybd_event(18, 0, 0, 0)
win32api.keybd_event(83,0,0,0)
win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)
# 模拟发送微信消息
def SendWxMsg(wxid,sendtext):
# 先启动微信
FindWindow('微信')
time.sleep(1)
# 定位到搜索框
pyautogui.moveTo(143, 39)
pyautogui.click()
# 搜索窗口
ClipboardText(wxid)
time.sleep(1)
# 进入窗口
pyautogui.moveTo(155, 120)
pyautogui.click()
# 粘贴文本内容
ClipboardText(sendtext)
SendMsg()
print('已发送')
# 调用函数(微信号或微信昵称或备注,需要发送的文本消息)
SendWxMsg('face6009','Python发送微信消息')
动图演示

1、发送消息给好友
2、发送消息到微信群
3、微信进群
4、将微信移出群聊
5、发布群公告
6、转发一篇图文
2021-09-02新增发送文件和转发群消息
import pyautogui
import time
import win32api
import win32con
import win32gui
import win32clipboard as w
def FindWindow(chatroom):
win = win32gui.FindWindow(None, chatroom)
print("找到窗口句柄:%x" % win)
if win != 0:
win32gui.ShowWindow(win, win32con.SW_SHOWMINIMIZED)
win32gui.ShowWindow(win, win32con.SW_SHOWNORMAL)
win32gui.ShowWindow(win, win32con.SW_SHOW)
win32gui.SetWindowPos(win, win32con.HWND_TOP, 0, 0, 500, 700, win32con.SWP_SHOWWINDOW)
win32gui.SetForegroundWindow(win) # 获取控制
time.sleep(1)
tit = win32gui.GetWindowText(win)
print('已启动【'+str(tit)+'】窗口')
else:
print('找不到【%s】窗口' % chatroom)
exit()
# 设置和粘贴剪贴板
def ClipboardText(ClipboardText):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, ClipboardText)
w.CloseClipboard()
time.sleep(1)
win32api.keybd_event(17,0,0,0)
win32api.keybd_event(86,0,0,0)
win32api.keybd_event(86,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
# 模拟发送动作
def SendMsg():
win32api.keybd_event(18, 0, 0, 0)
win32api.keybd_event(83,0,0,0)
win32api.keybd_event(18,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(83,0,win32con.KEYEVENTF_KEYUP,0)
# 模拟发送微信文本消息
def SendWxMsg(wxid,sendtext):
# 先启动微信
FindWindow('微信')
time.sleep(1)
# 定位到搜索框
pyautogui.moveTo(143, 39)
pyautogui.click()
# 搜索微信
ClipboardText(wxid)
time.sleep(1)
# 进入聊天窗口
pyautogui.moveTo(155, 120)
pyautogui.click()
# 粘贴文本内容
ClipboardText(sendtext)
# 发送
SendMsg()
print('已发送')
# 关闭微信窗口
time.sleep(1)
pyautogui.moveTo(683, 16)
pyautogui.click()
# 模拟发送文件消息(图片、文档、压缩包等)
def SendWxFileMsg(wxid,imgpath):
# 先启动微信
FindWindow('微信')
time.sleep(1)
# 定位到搜索框
pyautogui.moveTo(143, 39)
pyautogui.click()
# 搜索微信
ClipboardText(wxid)
time.sleep(1)
# 进入聊天窗口
pyautogui.moveTo(155, 120)
pyautogui.click()
# 选择文件
pyautogui.moveTo(373, 570)
pyautogui.click()
ClipboardText(imgpath)
time.sleep(1)
pyautogui.moveTo(784, 509)
pyautogui.click()
# 发送
SendMsg()
print('已发送')
# 关闭微信窗口
time.sleep(1)
pyautogui.moveTo(683, 16)
pyautogui.click()
# 转发群里最新的一条消息
def ZhuanfaMsg(wxid,groupname):
# 先启动微信
FindWindow('微信')
time.sleep(1)
# 定位到搜索框
pyautogui.moveTo(143, 39)
pyautogui.click()
# 搜索群
ClipboardText(groupname)
time.sleep(1)
# 进入群窗口
pyautogui.moveTo(155, 120)
pyautogui.click()
# 开始转发
pyautogui.moveTo(484, 439)
time.sleep(1)
pyautogui.rightClick()
pyautogui.moveTo(543, 454)
time.sleep(1)
pyautogui.click()
# 搜索用户
ClipboardText(wxid)
time.sleep(1)
pyautogui.moveTo(828, 406)
pyautogui.click()
time.sleep(1)
# 确定转发
pyautogui.moveTo(1108, 755)
pyautogui.click()
# 发送文本消息(微信号或微信昵称或备注,需要发送的文本消息)
SendWxMsg('sansure2016','Python发送微信消息')
# 发送文件消息(图片、文档、压缩包等)
SendWxFileMsg('sansure2016',r"C:UsersTANKINGDesktopcbzqx77.jpg")
# 转发群里最新的一条消息(微信号或微信昵称或备注,群名称)
ZhuanfaMsg('sansure2016','里客云科技')