当前位置 : 主页 > 编程语言 > 其它开发 >

Python 之 控制QQ(9.5.8.28186)、微信(3.6.0.18)

来源:互联网 收集:自由互联 发布时间:2022-05-30
Python 之 控制QQ(9.5.8.28186)、微信(3.6.0.18)可能需要使用的库,反正一大堆,部分代码里有用到,部分是我自己用的 from asyncio.windows_events import NULLimport datetimeimport win32guiimport win32conim
Python 之 控制QQ(9.5.8.28186)、微信(3.6.0.18) 可能需要使用的库,反正一大堆,部分代码里有用到,部分是我自己用的
from asyncio.windows_events import NULL
import datetime
import win32gui
import win32con
import win32clipboard as w
import time
from win32api import keybd_event
import pandas as pd
import xlwings as xw
from PIL import ImageGrab,  Image
import shutil
from ctypes import *
from PIL import ImageGrab, Image
from win32com.client import DispatchEx
import pythoncom
import os
import time
from decimal import *
from apscheduler.schedulers.blocking import BlockingScheduler
import pythoncom
import tkinter
import tkinter.messagebox

#下面时我自己写的引用
import DowFile as DF
import module.FileOperations
import module.UpExcel as UE
打开QQ、微信

通过快捷键ctrl + alt + z 打开QQ
通过快捷键ctrl + alt + w 打开微信

# 打开QQ
def OpenQQ(TFdict):
    try:
        keybd_event(win32con.VK_CONTROL, 0, 0, 0)
        keybd_event(win32con.VK_MENU, 0, 0, 0)
        keybd_event(90, 0, 0, 0)
        keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
        keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0)
        keybd_event(90, 0, win32con.KEYEVENTF_KEYUP, 0)
        TFdict['TF'] = 'T'
        return TFdict
    except:
        TFdict['QQReason'] = '打开QQ失败'
        return TFdict

# 打开WeChat
def OpenWeChat(TFdict):
    try:
        keybd_event(win32con.VK_CONTROL, 0, 0, 0)
        keybd_event(win32con.VK_MENU, 0, 0, 0)
        keybd_event(87, 0, 0, 0)
        keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
        keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0)
        keybd_event(87, 0, win32con.KEYEVENTF_KEYUP, 0)
        TFdict['TF'] = 'T'
        return TFdict
    except:
        TFdict['WeChatReason'] = '打开微信失败'
        return TFdict

复制快捷键
# 将消息写入剪贴板
def setText(text):
    try:
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, text)
        w.CloseClipboard()
    except Exception as e:
        print(e)
粘贴快捷键
keybd_event(17, 0, 0, 0)  # ctrl
keybd_event(86, 0, 0, 0)  # V
keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1.5)
搜索栏搜索指定好友
# qq搜索栏搜索指定好友
def QQsearchUser(name, TFdict):
    try:
        # 鼠标定位qq搜索栏
        hand = win32gui.FindWindow('TXGuiFoundation', 'QQ')
        setText(name)
        win32gui.SendMessage(hand, 770, 0, 0)
        # 表示停止1.5秒再运行(运行太快qq会反应不过来)
        time.sleep(1.5)
        win32gui.SendMessage(hand, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        TFdict['TF'] = 'T'
        TFdict['QQTF'] = 'T'
        TFdict['QQReason'] = 'QQ搜索好友成功'
        return TFdict
    except:
        TFdict['TF'] = 'F'
        TFdict['QQTF'] = 'F'
        TFdict['QQReason'] = 'QQ搜索好友失败'
        return TFdict

# 微信搜索栏搜索指定好友
def WeChatsearchUser(name, TFdict, repetition_num=0):
    try:
        # 鼠标定位微信搜索栏
        # hand = win32gui.FindWindow(None, chatroom)
        keybd_event(win32con.VK_CONTROL, 0, 0, 0)  # ctrl
        keybd_event(70, 0, 0, 0)  # F
        keybd_event(win32con.VK_CONTROL, 0, win32con.KEYEVENTF_KEYUP, 0)
        keybd_event(70, 0, win32con.KEYEVENTF_KEYUP, 0)
        setText(name)

        keybd_event(17, 0, 0, 0)  # ctrl
        keybd_event(86, 0, 0, 0)  # V
        keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
        keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1.5)

        
        keybd_event(40, 0, 0, 0)  # 下键
        keybd_event(40, 0,  win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)

        keybd_event(38, 0, 0, 0)  # 上键
        keybd_event(38, 0,  win32con.KEYEVENTF_KEYUP, 0)
        time.sleep(1)
        
        # 多个好友时,多按几次下键  
        if repetition_num != 0 :
            for i in range(repetition_num):
                keybd_event(40, 0, 0, 0)  # 下键
                keybd_event(40, 0,  win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(1)

        keybd_event(13, 0, 0, 0)  # enter
        keybd_event(13, 0,  win32con.KEYEVENTF_KEYUP, 0)
        # hand = win32gui.FindWindow(None, name)
        # win32gui.SendMessage(hand, 770, 0, 0)
        # 表示停止1.5秒再运行(运行太快qq会反应不过来)
        time.sleep(1)
        # win32gui.SendMessage(hand, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        TFdict['TF'] = 'T'
        TFdict['WeChatTF'] = 'T'
        TFdict['WeChatReason'] = '微信搜索好友成功'
        return TFdict
    except:
        TFdict['TF'] = 'F'
        TFdict['WeChatTF'] = 'F'
        TFdict['WeChatReason'] = '微信搜索好友失败'
        return TFdict

QQ、微信发送消息
# QQ发送信息
def QQsendMsger(TFdict):
    try:
        # 获取当前窗口名称
        qqtitle = win32gui.GetWindowText(
            win32gui.GetForegroundWindow())
        if (coding in qqtitle) == True:
            # 自动定位聊天窗口
            hand = win32gui.FindWindow('TXGuiFoundation', qqtitle)
            time.sleep(0.5)
            setText(msg)
            win32gui.SendMessage(hand, 770, 0, 0)
            win32gui.SendMessage(
                hand, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            time.sleep(0.5)
            hand = win32gui.FindWindow('TXGuiFoundation', qqtitle)
            setImage(ImageAdd)
            win32gui.SendMessage(hand, 770, 0, 0)
            win32gui.SendMessage(
                hand, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
            time.sleep(1)
            win32gui.PostMessage(
                hand, win32con.WM_CLOSE, 0, 0)   # win close
            time.sleep(0.5)
            print("QQ运行完成!")
            TFdict['TF'] = 'T'
            TFdict['QQTF'] = 'T'
            TFdict['QQReason'] = 'QQ成功'
            return TFdict
        elif win32gui.FindWindow('TXGuiFoundation', '查找') != NULL:
            CZhand = win32gui.FindWindow('TXGuiFoundation', '查找')
            win32gui.PostMessage(
                CZhand, win32con.WM_CLOSE)  # win close
            print("QQ运行完成!发送失败")
            TFdict['TF'] = 'F'
            TFdict['QQTF'] = 'F'
            TFdict['QQReason'] = '失败,QQ未找到好友。'
            return TFdict
        else:
            # 自动定位聊天窗口
            hand = win32gui.FindWindow('TXGuiFoundation', qqtitle)
            time.sleep(1)
            win32gui.PostMessage(
                hand, win32con.WM_CLOSE, 0, 0)   # win close
            time.sleep(2)
            # 打开QQ
            TFdict = OpenQQ(TFdict)
            time.sleep(0.5)
            # 搜索好友
            TFdict = QQsearchUser(coding, TFdict)
            time.sleep(0.5)
            # 重新发送信息
            TFdict = QQsendMsger(TFdict)
            time.sleep(0.5)
            print("QQ运行完成-2!")
            TFdict['TF'] = 'T'
            TFdict['QQTF'] = 'T'
            TFdict['QQReason'] = 'QQ成功'
            return TFdict

    except Exception as e:
        CZhand = win32gui.FindWindow('TXGuiFoundation', '查找')
        win32gui.ShowWindow(CZhand, win32con.SW_HIDE)  # 隐藏
        print("QQ运行完成!发送失败,报错:", e)
        TFdict['TF'] = 'F'
        TFdict['QQTF'] = 'F'
        TFdict['QQReason'] = '失败,QQ未找到好友。'
        return TFdict    

# 微信发送信息
    def WeChatsendMsger(TFdict):
        try:
            # 获取当前窗口名称
            wxtitle = win32gui.GetWindowText(win32gui.GetForegroundWindow())
            if wxtitle == '微信':
                time.sleep(1)
                setText(msg)
                time.sleep(1)
                keybd_event(17, 0, 0, 0)  # ctrl
                keybd_event(86, 0, 0, 0)  # V
                keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(1)
                keybd_event(18, 0, 0, 0)  # ctrl
                keybd_event(83, 0, 0, 0)  # s
                keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
                keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(1)
                setImage(ImageAdd)
                time.sleep(1)
                keybd_event(17, 0, 0, 0)  # ctrl
                keybd_event(86, 0, 0, 0)  # V
                keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
                keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(1)
                keybd_event(18, 0, 0, 0)  # ctrl
                keybd_event(83, 0, 0, 0)  # s
                keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
                keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)
                time.sleep(2)
                wx = win32gui.FindWindow('WeChatMainWndForPC', '微信')
                win32gui.PostMessage(wx, win32con.WM_CLOSE)   # win close
                time.sleep(2)
                print("微信运行完成!")

                TFdict['TF'] = 'T'
                TFdict['WeChatTF'] = 'T'
                TFdict['WeChatReason'] = '成功。'

            # 获取当前窗口名称
            # wxtitle = win32gui.GetWindowText(win32gui.GetForegroundWindow())
            # print(wxtitle)
            else:
                syshand = win32gui.FindWindow('Search2Wnd', '搜一搜')
                win32gui.PostMessage(syshand, win32con.WM_CLOSE)   # win close
                time.sleep(2)
                wxhand = win32gui.FindWindow('WeChatMainWndForPC', '微信')
                win32gui.PostMessage(wxhand, win32con.WM_CLOSE)   # win close
                print("微信运行完成!发送失败")
                TFdict['TF'] = 'F'
                TFdict['WeChatTF'] = 'F'
                TFdict['WeChatReason'] = '失败,微信未找到好友。'
            return TFdict

        except Exception as e:
            CZhand = win32gui.FindWindow('Search2Wnd', '搜一搜')
            win32gui.PostMessage(CZhand, win32con.WM_CLOSE, 0, 0)  # 隐藏
            time.sleep(2)
            OpenWeChat(TFdict)
            time.sleep(2)
            print("微信运行完成!发送失败,报错:", e)
            TFdict['TF'] = 'F'
            TFdict['WeChatTF'] = 'F'
            TFdict['WeChatReason'] = '失败,微信未找到好友。'
            return TFdict
隐藏QQ、微信窗口
# 自动定位QQ聊天窗口
hand = win32gui.FindWindow('TXGuiFoundation', qqtitle)
win32gui.PostMessage(hand, win32con.WM_CLOSE, 0, 0)   # win close
# QQ的查找和QQ窗口
CZhand = win32gui.FindWindow('TXGuiFoundation', '查找')
win32gui.PostMessage(CZhand, win32con.WM_CLOSE)  # win close
qq = win32gui.FindWindow('TXGuiFoundation', 'QQ')
win32gui.PostMessage(qq, win32con.WM_CLOSE)   # win close

# 微信的搜一搜和微信窗口
syshand = win32gui.FindWindow('Search2Wnd', '搜一搜')
win32gui.PostMessage(syshand, win32con.WM_CLOSE)   # win close
wxhand = win32gui.FindWindow('WeChatMainWndForPC', '微信')
win32gui.PostMessage(wxhand, win32con.WM_CLOSE)   # win close

通过 Python 的 win32 ,一步一步操作来实现的功能。

上一篇:突发!Spring Cloud 也沦陷了。。。
下一篇:没有了
网友评论