一、简介
如果每天要启动某个软件还要输入账号密码登录的需求的话,可以参考本文章;
二、Python环境
环境:Python3.11 已经在Windows电脑中配置Python环境变量,且配置了pipd的环境变量;
三、安装模块
安装所需要的模块pyautogui、subprocess、psutil等,可以通过pip安装,下面以安装pyautogui为例cmd命令行中输入
pip install pyautogui
四、编写脚本
新建startQQ.py 文件,输入如下代码:
import pyautogui
import keyring
import time
import subprocess
import sys
import psutil
import osdef run_application_with_account(application_path, account):# 设置密码到系统的安全存储中keyring.set_password("example_application", "user", account)# 启动应用程序subprocess.Popen(application_path)# 等待一段时间,确保应用程序已完全加载time.sleep(5)# 使用pyautogui模拟键盘输入账号pyautogui.write(account)pyautogui.press('enter')def f_exeIsRunning(exe_path):# function: 判断exe可执行文件是否正在运行# in: exe_path:exe可执行文件的路径# out: bool# return: True or False# others: Determines If The EXE Is Running Funcfor proc in psutil.process_iter(['name']):try:if proc.info['name'] == os.path.basename(exe_path):return Trueexcept (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):return Falseif __name__ == "__main__":application_path = "D:\Programs\OrdinaryApps\Tencent\QQ\Bin\QQScLauncher.exe"if f_exeIsRunning(application_path):# 如果已经打开,打印路径print(f"{application_path} 已经打开")sys.exit(0)else:# 如果未打开,则打开print(f"{application_path} 未打开")password = keyring.get_password("example_application", "user")if not password:password = "895860"# password = input("请输入密码:")#打开应用并输入密码或者账号,并模拟enter健登录try:run_application_with_account(application_path, password)except Exception as e:print("发生错误:", e)
run_application_with_account方法为启动软件自动填入密码或账号,并模拟登录
f_exeIsRunning方法主要是判断要启动的软件是否已经启动;
五,运行startQQ.py脚本
cmd命令行中输入注意要输入以上编写的脚本存储的全路径
python D:\Workspace\Python\StartAPP\startQQ.py
如果你在startQQ.py所在目录启动的cmd可以不用带全路径名
python startQQ.py