Open In App

Python code to Automate Desktop Activities in Aindows

Last Updated : 07 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Python's simplicity makes it an excellent choice for automating tasks on the desktop. Using pyautogui library, developers can control mouse movements, simulate mouse clicks, send keyboard inputs, and interact with application windows. This capability opens up various possibilities for automating tasks such as data entry, file operations, web browsing, and much more.

By automating certain desktop activities, developers can save time, reduce errors, and can enhance productivity to some extent. Repetitive operations that would normally take a significant amount of time and effort can now be completed with a few lines of Python code. The ability to programmatically interact with apps and modify the Windows environment provides the developers with the tools they need to construct efficient and reliable automation solutions.

  • pyautogui Library
  • Mouse Control
  • Keyboard control
  • Timing and delay control
  • Application Window Controller

Required Modules

pip install pyautogui 

Python code to automate desktop activities in Windows Examples

Automating Notepad Using Python

In this example, we are using pyautogui and time library to automate mouse and keyboard controls. We are simulating the Windows button to open a start menu followed by typing a Notepad and pressing the enter button. After the opening of the notepad, we write the text in the notepad.

[tabby title="Python3"][sourcecode language="python3"]import pyautogui import time pyautogui.press('win') time.sleep(1) pyautogui.typewrite('Notepad') pyautogui.press('enter') time.sleep(1) pyautogui.write("The text is written in Notepad using pyautogui.") [/sourcecode][tabbyending]

Output :

[caption width="800"]Automating Notepad Using PythonNotepad using pyautogui[/caption]

Automating Browser using Python

In this example, we are using pyautogui and time library to automate mouse and keyboard controls. We are simulating the Windows button to open a start menu followed by typing Edge and pressing the enter button. After the opening of the Edge, we write the link in the URL and press Enter.

[tabby title="Python3"][sourcecode language="python3"]import pyautogui import time pyautogui.press('win') pyautogui.typewrite('Edge') pyautogui.press('enter') time.sleep(2) pyautogui.typewrite('https://www.geeksforgeeks.org') pyautogui.press('enter') [/sourcecode][tabbyending]

Output :

Automating Browser Using Python

Automating Paint using Python

In this example, we are using pyautogui and time library to automate mouse and keyboard controls. We are simulating the Windows button to open a start menu followed by typing Paint and pressing the enter button. After opening the Paint, we are moving and drag the mouse pointer.

[tabby title="Python3"][sourcecode language="python3"]import pyautogui import time time.sleep(2) pyautogui.press('win') pyautogui.typewrite('Paint') pyautogui.press('enter') time.sleep(2) pyautogui.moveTo(400, 400, duration=1) pyautogui.dragTo(400,600, duration=1) pyautogui.dragTo(600,600, duration=1) pyautogui.dragTo(600,400, duration=1) pyautogui.dragTo(400,400, duration=1) time.sleep(1) [/sourcecode][tabbyending]

Output

[caption width="800"]Automating Paint using PythonDrawing a square using pyautogui[/caption]

Automate mouse control with Python

In this example, we're using pyautogui library for automating mouse control. This library provides us with various functions to move the mouse to specific coordinates using (moveTo) like moveTo(200,200) in this example, we can perform click operation with the (click) function, for double-clicking at a particular position we have (doubleClick) like doubleClick(400,400), to drag the mouse from one position to another we can use (dragTo) like dragTo(600,600), for scrolling the mouse up and down we use (scroll) like scroll(20) or scroll, and to get the current mouse position we use (position) function.

[tabby title="Python3"][sourcecode language="python3"]import pyautogui # To Move the mouse to specific coordinates pyautogui.moveTo(200, 200, duration=2) # Move the mouse to (200, 200) # To Click the mouse at current position pyautogui.click() # To Double-click the mouse at specific position pyautogui.doubleClick(400, 400) # To Drag the mouse from one position to another # Drag the mouse to (600, 600) over 1 second pyautogui.dragTo(600, 600, duration=1) # To Scroll the mouse up and down pyautogui.scroll(20) # Scroll up by 20 units pyautogui.scroll(-20) # Scroll down by 20 units # Get the current mouse position x, y = pyautogui.position() print(f"Current mouse position: ({x}, {y})")[/sourcecode][tabbyending]

Output :

mouse.png

Automate Social Media with Python

In this example, we are using pyautogui and time library to automate mouse and keyboard controls. We are simulating the Windows button to open a start menu followed by typing Edge and pressing the enter button. After the opening of the Edge, we write our social media link in the typewrite('URL') and press Enter after a delay of 2 sec. After that, we find the position of the like/unlike (Hide post in this case) button by trial and error method and move our mouse to that position and then perform the click operation.

[tabby title="Python3"][sourcecode language="python3"]import pyautogui import time # Delay before starting automation time.sleep(2) #Open Edge browser pyautogui.press('win') pyautogui.typewrite('Edge') pyautogui.press('enter') time.sleep(2) #Open your Socia media pyautogui.typewrite('www.quora.com') time.sleep(2) pyautogui.press('enter') time.sleep(5) #Calculate the Hide button position using trial and error Hide_button_position = (1125,345) pyautogui.moveTo(*Hide_button_position ) time.sleep(2) pyautogui.click()[/sourcecode][tabbyending]

Output

[caption width="800"]Automate Social Media with PythonAutomate Social Media with Python[/caption]

Next Article
Article Tags :
Practice Tags :

Similar Reads