Python模拟发送QQ文字/图片消息
from io import BytesIO
import win32gui
import win32con
import win32clipboard as w
import re
from PIL import Image
def getText():
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_UNICODETEXT)
w.CloseClipboard()
return d
def loadImage(aString):
img = Image.open(aString)
output = BytesIO()
img.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()
return data
def setMsg(aString):
w.OpenClipboard()
w.EmptyClipboard()
if re.match('.+\\.(png|jpg|jpeg)', aString):
w.SetClipboardData(win32con.CF_DIB, loadImage(aString))
else:
w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
w.CloseClipboard()
def send_qq(to_who, msg):
setMsg(msg)
qq = win32gui.FindWindow(None, to_who)
win32gui.ShowWindow(qq, win32con.SW_SHOW)
win32gui.SendMessage(qq, win32con.WM_PASTE, 0, 0)
win32gui.SendMessage(qq, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
win32gui.SendMessage(qq, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
img_path = ''
to_who = ''
msg = ''
send_qq(to_who, msg)
send_qq(to_who, img_path)
参考博客
https://blog.csdn.net/weixin_44401906/article/details/115454487 https://blog.csdn.net/ysy_1_2/article/details/104790079 https://blog.csdn.net/suzyu12345/article/details/52934328 https://blog.csdn.net/dahuae/article/details/43969175
|