3 Practical Python Scripts for Windows Operations!

leo 30/10/2025

1 – Changing Computer Desktop Wallpaper

Tools Used

  • Development Environment: python3.7, Windows10
  • Toolkits: win32api, win32con, win32gui, os, random
  • Installation command for win32 tools: pip install pywin32

Project Analysis
Desktop data information is stored in the registry, specifically in the second item under the Control Panel\Desktop subkey.

Implementation Approach

  • Use win32api to open the registry and select the corresponding configuration subkey
  • Generate the corresponding handle: k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control Panel\Desktop', 0, win32con.KEY_SET_VALUE)
  • Adjust desktop style to stretch mode:
    • 2 = Stretch wallpaper
    • 0 = Center wallpaper
    • 6 = Fit
    • 10 = Fill
  • Prepare the wallpaper images to be modified (wallpaper data can be collected through web scraping)
  • Use win32gui to submit data and change desktop to prepared wallpaper
  • win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)

Source Code

python

import win32api   # Call Windows底层 interface configuration
import win32con   # Modify data
import win32gui   # Submit corresponding data
import os         # Python file management toolkit
import random     # Get random values
import time       # Time management module

def set_wallpaper():
    path = os.listdir(r'image_folder')
    for i in path:
        img_path = r'image_folder' + '\\' + i
        print(img_path)
        # Open registry handle
        k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, 'Control Panel\\Desktop', 0, win32con.KEY_SET_VALUE)
        # 2 = Stretch wallpaper, 0 = Center wallpaper, 6 = Fit, 10 = Fill
        win32api.RegSetValueEx(k, 'WallpaperStyle', 0, win32con.REG_SZ, '2')
        win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, img_path, win32con.SPIF_SENDWININICHANGE)
        time.sleep(10)

set_wallpaper()

2 – Infinite Computer Lock Screen

Tools Used

  • Development Environment: python3.7, Windows10
  • Toolkits: ctypes

Implementation Approach

  • ctypes is Python’s foreign function library that provides C-compatible data types and allows calling functions in DLLs or shared libraries
  • Use the underlying operating system’s user32.dll to achieve lock screen effect

Source Code

python

from ctypes import windll

def lock_windows():
    while True:
        user = windll.LoadLibrary('user32.dll')
        user.LockWorkStation()

lock_windows()

Packaging Method:

bash

pyinstaller -F your_filename.py

After packaging, you can let your friends/colleagues try it (for the sake of friendship, it’s better to add a delay operation)

3 – Infinite Pop-up Windows

You’ve probably heard of Panda Burning Incense (similar concept, any resemblance has nothing to do with me)

Implementation Approach

  • Use os module to execute and open cmd window pages (ensure the options are in environment variables)

Source Code

python

import os

for i in range(2000):
    os.system('start cmd')

Warning: Do not attempt this on low-spec computers!

Windows close window command:

bash

taskkill /f /im cmd.exe /t