Python code to automate desktop activities in windows

Viewed 81928

I want to automate desktop activities in Windows environment using Python. How it can be done? Some examples will also be helpful.

By desktop activities, I mean actions such as taking control over mouse and keyboard, access active windows properties, double-click on an icon on the desktop, minimize and maximize windows, enter data to an input popup window through keyboard, etc.

6 Answers

You can use PyAutoGUI which provide a cross-platform Python way to perform GUI automation.

Mouse Control

Here is a simple code to move the mouse to the middle of the screen:

import pyautogui
screenWidth, screenHeight = pyautogui.size()
pyautogui.moveTo(screenWidth / 2, screenHeight / 2)

Related question: Controlling mouse with Python.

Keyboard Control

Example:

pyautogui.typewrite('Hello world!')                 # prints out "Hello world!" instantly
pyautogui.typewrite('Hello world!', interval=0.25)  # prints out "Hello world!" with a quarter second delay after each character

Message Box Functions

It provides JavaScript-style message boxes.

And other.


For other suggestions, check: Python GUI automation library for simulating user interaction in apps.

Take a look at BotCity Framework, an open-source RPA framework. 

It's just python (no intermediary code, no jython, etc).

The example below executes SAP and logs in:

from botcity.core import DesktopBot
from botcity.maestro import AlertType, AutomationTaskFinishStatus, Column

class Bot(DesktopBot):
    def action(self, execution):
        self.execute("saplogon.exe")
        
        # #{image:"login"}
    
        if not self.find( "user", matching=0.97, waiting_time=10000):
            self.not_found("user")
        self.click_relative(172, 5)
        
        self.paste(user)
        self.tab()
        self.paste(pass)
        self.enter()
        
if __name__ == '__main__':
    Bot.main()

As Sikuli, you have a tool to crop elements and have visual clues about the interface and UI elements. But in this case, it's a tool for editing .py files (not intermediary code) so you can use any python lib in your automation.

You can try ClointFusion

It's again a Python based RPA platform which internally makes use of PyAutoGUI among other packages.

It has a friendly Browser based Drag & Drop BOT Builder: DOST

You can find more than 100 easy to use functions:

  1. 6 gui functions to take any input from user
  2. 4 functions on Mouse Operations
  3. 6 functions on Window Operations (works only in Windows OS)
  4. 5 functions on Window Objects (works only in Windows OS)
  5. 8 functions on Folder Operations
  6. 28 functions on Excel Operations
  7. 3 functions on Keyboard Operations
  8. 5 functions on Screenscraping Operations
  9. 11 functions on Browser Operations
  10. 4 functions on Alert Messages
  11. 3 functions on String Operations
  12. Loads of miscellaneous functions related to emoji, capture photo, flash (pop-up) messages etc

Disclaimer: I'm one of developers of ClointFusion

Related