Python code to Automate Desktop Activities in Aindows
Last Updated :
07 Aug, 2024
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"]

Notepad 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 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"]

Drawing 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 :

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 Python[/caption]
Similar Reads
How to Create a Desktop Shortcut in Windows?
Desktop shortcuts are a quick link to a particular file found inside the complex directories and drives on our Windows operating systems. Applications can be opened easily with only one click through desktop shortcuts. We can create desktop shortcuts for any type of file. Moreover, using desktop sho
2 min read
How to Automate an Excel Sheet in Python?
Before you read this article and learn automation in Python....let's watch a video of Christian Genco (a talented programmer and an entrepreneur) explaining the importance of coding by taking the example of automation. You might have laughed loudly after watching this video and you surely, you might
8 min read
How to install Python on Windows?
Python is a high-level programming language that has become increasingly popular due to its simplicity, versatility, and extensive range of applications. The process of How to install Python in Windows, operating system is relatively easy and involves a few uncomplicated steps. This article aims to
5 min read
How to Change Desktop Background with Python
Changing the desktop background can be a fun way to personalize the computer. While there are many ways to do this manually, using Python can automate and customize the process further. The problem is to find a way to interact with the operating system's settings using Python to change the desktop b
5 min read
PYGLET â Window Activate Event
In this article, we will see how we can trigger window activate event in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia, etc. A window is a "heavyweight" object occupying operating system resources. Windows may
2 min read
How to Automate the Storage using Dropbox API in Python ?
In this Data Era Storing, Managing, and organizing the Data is an important factor considered by every business. Dropbox is one of the most popular cloud storage systems on the market, and it continues to improve its features. In this article, we will be demonstrating how one can connect to dropbox
2 min read
Automate linkedin connections using Python
Automating LinkedIn connections using Python involves creating a script that navigates LinkedIn, finds users based on specific criteria (e.g., job title, company, or location), and sends personalized connection requests. In this article, we will walk you through the process, using Selenium for web a
5 min read
Python Automation Tutorial: Beginner to Advanced
Python is a very powerful programming language and it's expanding quickly because of its ease of use and straightforward syntax. In this Python Automation Tutorial, we will explore various techniques and libraries in Python to automate repetitive tasks. Automation can save you time and reduce error
10 min read
Open Python Files in IDLE in Windows
IDLE works well for inexperienced and seasoned Python programmers alike as it integrates into the Python interpreter, interactive shell, and debugging tools for learning Python, testing code, and building Python applications. Although some developers may opt for more feature-rich IDEs in their compl
2 min read
How to create a Hotkey in Python?
This article is about How to create a HotKey Using Python. But first, let's discuss what is a Hotkey. A Hotkey is an assigned key or sequence of keys programmed to execute a command or perform a selected task during a software application: For instance, on Windows computers, the hotkey Ctrl+S is oft
3 min read