一、 前提需要先打开好友窗口(独立的),再自动发送消息
import os
import win32gui #pywin32-221.win-amd64-py3.7.exe
import win32con
from ctypes import *
import win32clipboard as w
import time
from PIL import Image #pip install pillow
#pip install -i https://pypi.douban.com/simple pillow
#import win32api
#发送文字
def setText(info):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, info)
w.CloseClipboard()
#发送图片
def setImage(imgpath):
im = Image.open(imgpath)
im.save('1.bmp')
aString = windll.user32.LoadImageW(0, r"1.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE)
print(aString)
if aString != 0: ## 由于图片编码问题 图片载入失败的话 aString 就等于0
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_BITMAP, aString)
w.CloseClipboard()
#指定窗口(QQ昵称备注)
def sendByUser(uname):
hwnd = win32gui.FindWindow('TXGuiFoundation', uname)
#hwnd = win32gui.FindWindow('ChatWnd', uname)
win32gui.SendMessage(hwnd, 258, 22, 2080193)
win32gui.SendMessage(hwnd, 770, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
setText('给GANGAN神说个事情')
sendByUser('GANGAN神')
time.sleep(0.5)
setText('ChyiChin永远的歌神')
sendByUser('ChyiChin')
time.sleep(0.5)
setText('理发界三巨头之一')
sendByUser('Tony老师')
time.sleep(0.5)
'''
print(os.listdir('imgs/'))#['ChyiChin.jpg', 'GANGAN神.jpg', 'Tony老师.jpg']
print(os.path.basename('imgs/GANGAN.jpg'))#GANGAN.jpg
print(os.path.splitext('imgs/GANGAN.jpg')[0])#imgs/GANGAN
print(os.path.splitext('GANGAN.jpg')[0])#GANGAN
print(os.path.basename(os.path.splitext('imgs/GANGAN.jpg')[0]))
'''
#获取无后缀的图片名称
def getNosuffixImgName(imgname):
return os.path.splitext(imgname)[0]
imgdir='imgs/'
imgs=os.listdir(imgdir)
for img in imgs:
setImage(imgdir+img)
sendByUser(getNosuffixImgName(img))
time.sleep(0.5)
二、改进版本,自动搜索需要发送的好友,自动弹出再自动发送消息和图片
import os
import win32gui #pywin32-221.win-amd64-py3.7.exe
import win32con
from ctypes import *
import win32clipboard as w
import time
from PIL import Image #pip install pillow
#pip install -i https://pypi.douban.com/simple pillow
#import win32api
#发送文字
def setText(info):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, info)
w.CloseClipboard()
#发送图片
def setImage(imgpath):
im = Image.open(imgpath)
im.save('1.bmp')
aString = windll.user32.LoadImageW(0, r"1.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE)
if aString != 0: ## 由于图片编码问题 图片载入失败的话 aString 就等于0
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_BITMAP, aString)
w.CloseClipboard()
#定位QQ窗口,进行昵称备注的搜索,再回车弹出此好友窗口
def searchByUser(uname):
hwnd = win32gui.FindWindow('TXGuiFoundation', 'QQ')
#hwnd = win32gui.FindWindow('ChatWnd', uname)
setText(uname)
win32gui.SendMessage(hwnd, 258, 22, 2080193)
win32gui.SendMessage(hwnd, 770, 0, 0)
time.sleep(0.5)
win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
#定位好友窗口,昵称备注
def sendByUser(uname):
hwnd = win32gui.FindWindow('TXGuiFoundation', uname)
#hwnd = win32gui.FindWindow('ChatWnd', uname)
win32gui.SendMessage(hwnd, 258, 22, 2080193)
win32gui.SendMessage(hwnd, 770, 0, 0)
win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.SendMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
#发送完信息之后关闭窗口(新的窗口的标题将不是昵称)
def closeByUser(uname):
hwnd = win32gui.FindWindow('TXGuiFoundation', uname)
win32gui.SendMessage(hwnd, win32con.WM_CLOSE, 0, 0)
'''
searchByUser('GANGAN神')
setText('~~~给GANGAN神说个事情~~~')
sendByUser('GANGAN神')
time.sleep(3)
closeByUser('GANGAN神')
time.sleep(1)
searchByUser('Tony老师')
setText('~~~~理发界三巨头之一~~~')
sendByUser('Tony老师')
time.sleep(3)
closeByUser('Tony老师')
'''
#获取无后缀的图片名称
def getNosuffixImgName(imgname):
return os.path.splitext(imgname)[0]
imgdir='imgs/'
imgs=os.listdir(imgdir)
for img in imgs:
searchByUser(getNosuffixImgName(img))
setImage(imgdir+img)
sendByUser(getNosuffixImgName(img))
time.sleep(1)
closeByUser(getNosuffixImgName(img))
time.sleep(1)
其中需要用到的库win32gui的安装包文件下载地址:python3.7和3.8版本对应的pywin32
pip install -i https://pypi.douban.com/simple pyinstaller
pyinstaller -F -w -i xxx.ico yy.py
在dist目录下面生成exe文件