Microsoft Windows

Table of contents

The sys and os modules can also be useful for this purpose, so check out a tutorial about them here.

The subprocess module

The subprocess module allows us to run external programs, system commands, and terminal instructions. It can also capture command output and communicate with other processes.


import subprocess

subprocess.run(["echo", "Hello World"]) # running a system command

result = subprocess.run(["ipconfig"], capture_output=True, text=True)
print(result.stdout) # displaying the command output

subprocess.Popen(["notepad.exe"]) # opening a program
                                    

The pyautogui module

The pyautogui module allows us to control the mouse and keyboard. It can be used for GUI automation, clicking buttons, typing text, taking screenshots, and moving the cursor.


import pyautogui

pyautogui.moveTo(500, 300) # moving the cursor to given coordinates
pyautogui.click() # performing a mouse click
pyautogui.write("Hello World!") # typing text using the keyboard

# Taking a screenshot
screenshot = pyautogui.screenshot()
screenshot.save("screen.png")
                                    

The pywin32 module

The pywin32 module provides access to many Windows APIs. It allows us to work with Windows applications, services, dialogs, and system features.


import win32api, win32con

win32api.MessageBox(0, "Hello World!", "Message", win32con.MB_OK) # displaying a message box
print(win32api.GetComputerName()) # getting the computer name
                                    

The textblob module

The textblob module is used for processing textual data. It supports sentiment analysis, translation, spelling correction, noun phrase extraction, and other natural language processing tasks.


from textblob import TextBlob

# Analyzing text sentiment
text = TextBlob("Python is amazing!")
print(text.sentiment)

# Correcting spelling mistakes
text = TextBlob("Pythn is awsome")
print(text.correct())
                                    

The win10toast module

The win10toast module allows us to display Windows toast notifications.


from win10toast import ToastNotifier

toast = ToastNotifier() # creating a notifier object
toast.show_toast("Notification", "Hello World!", duration=5)
                                    

The pyperclip module

The pyperclip module is used to copy text to the clipboard. The copy() function copies a given text to the clipboard, and the paste() function retrieves the most recently copied text. This module is not in the default package, so we have to install it as shown here.


import pyperclip
pyperclip.copy("Hello World!")
print(pyperclip.paste())