首页 > 编程语言 >麒麟桌面系统自动化方案 pyautogui+pythonnet

麒麟桌面系统自动化方案 pyautogui+pythonnet

时间:2022-11-24 18:14:37浏览次数:46  
标签:pyautogui 桌面 pythonnet Py ex PythonDLL PythonEngine

麒麟系统模拟鼠标的点击、滑动等操作,键盘输入等操作

pyautogui的安装见上篇文章

可以通过pythonnet封装python脚本,实现点击和输入等操作做到自动化

鼠标移动

        void DoMoveTest()
        {
            try
            {
                if (!PythonEngine.IsInitialized)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        Runtime.PythonDLL = @"python310.dll";
                    }
                    else
                    {
                        Runtime.PythonDLL = @"libpython3.8.so";
                    }
                    PythonEngine.Initialize();
                }
                using (Py.GIL())
                {
                    dynamic pyautogui = Py.Import("pyautogui");
                    pyautogui.moveTo(Convert.ToInt32(X), Convert.ToInt32(Y), 0.5);
                }
               
            }
            catch (Exception ex)
            {
                LogHelper.logHelper.ErrorLog(ex.Message);
            }
        }

键盘输入等

  void DoInputTest()
        {
            try
            {
                if (!PythonEngine.IsInitialized)
                {
                    if (OperatingSystem.IsWindows())
                    {
                        Runtime.PythonDLL = @"python310.dll";
                    }
                    else
                    {
                        Runtime.PythonDLL = @"libpython3.8.so";
                    }
                    PythonEngine.Initialize();
                }
                using (Py.GIL())
                {
                    dynamic pyautogui = Py.Import("pyautogui");
                    pyautogui.moveTo(Convert.ToInt32(X), Convert.ToInt32(Y), 0.5);
                    pyautogui.click();
                    pyautogui.moveRel(100, -590, 0.5);
                    pyautogui.click();
                    dynamic pyperclip = Py.Import("pyperclip");
                    pyperclip.copy("音乐");
                    pyautogui.hotkey("ctrl", 'v');
                }
            }
            catch (Exception ex)
            {
                LogHelper.logHelper.ErrorLog(ex.Message);
            }
        }

以上代码仅用于测试,使用pythonnet执行python脚本,执行完毕后要注意PythonEngine的关闭问题

标签:pyautogui,桌面,pythonnet,Py,ex,PythonDLL,PythonEngine
From: https://www.cnblogs.com/njcxwz/p/16922734.html

相关文章