按键精灵脚本转python

import re
import pyautogui
import time

# 定义映射关系
command_map = {
    'MoveTo': 'pyautogui.moveTo',
    'LeftClick': 'pyautogui.click',
    'LeftDown': 'pyautogui.mouseDown',
    'LeftUp': 'pyautogui.mouseUp',
    'KeyDown': 'pyautogui.keyDown',
    'KeyUp': 'pyautogui.keyUp',
    'Delay': 'time.sleep',
    'MouseWheel': 'pyautogui.scroll',
    'Press': 'pyautogui.press'
}

# 定义正则表达式来匹配按键精灵的命令
command_pattern = re.compile(r"(\w+)\s+(.*)")


def parse_command(command):
    match = command_pattern.match(command)
    if match:
        cmd_type = match.group(1)
        args = match.group(2)
        if cmd_type in command_map:
            if cmd_type == 'MoveTo':
                x, y = map(int, args.split(','))
                return f"{command_map[cmd_type]}({x}, {y})"
            elif cmd_type == 'LeftClick':
                return f"{command_map[cmd_type]}()"
            elif cmd_type == 'LeftDown':
                return f"{command_map[cmd_type]}()"
            elif cmd_type == 'LeftUp':
                return f"{command_map[cmd_type]}()"
            elif cmd_type == 'KeyDown':
                key = args.strip().strip('"')
                return f"{command_map[cmd_type]}('{key}')"
            elif cmd_type == 'KeyUp':
                key = args.strip().strip('"')
                return f"{command_map[cmd_type]}('{key}')"
            elif cmd_type == 'Delay':
                delay = float(args) / 1000  # Convert milliseconds to seconds
                return f"{command_map[cmd_type]}({delay})"
            elif cmd_type == 'MouseWheel':
                direction = int(args)
                return f"{command_map[cmd_type]}({direction})"
            elif cmd_type == 'Press':
                key = args.strip().strip('"')
                return f"{command_map[cmd_type]}('{key}')"
    return None


def convert_script(script_lines):
    python_code = []
    for line in script_lines:
        line = line.strip()
        if line:
            python_line = parse_command(line)
            if python_line:
                python_code.append(python_line)
    return python_code


def read_script_file(file_path):
    with open(file_path, 'r', encoding='gbk') as file:
        script_lines = file.readlines()
    return script_lines


def write_python_file(python_code, output_file):
    with open(output_file, 'w', encoding='utf-8') as file:
        file.write("import pyautogui\n")
        file.write("import time\n\n")
        file.write("pyautogui.PAUSE = 0.01\n\n")
        file.write("def run_macro():\n")
        for line in python_code:
            file.write(f"    {line}\n")
        file.write("\nrun_macro()\n")


def main():
    script_file = r'D:\vx_video\我录制的脚本1_202410141542.Q'  # 按键精灵脚本文件路径
    output_file = 'generated_script.py'  # 生成的Python脚本文件路径

    script_lines = read_script_file(script_file)
    python_code = convert_script(script_lines)
    write_python_file(python_code, output_file)


if __name__ == "__main__":
    main()
posted @ 2024-10-14 15:49  小小咸鱼YwY  阅读(43)  评论(0编辑  收藏  举报