how to print current wallpaper path in windows 10 using python?

Viewed 438

python program to print the current wallpaper path as output

i have tried this code but its not working

pip install py-wallpaper
from wallpaper import set_wallpaper, get_wallpaper
print(get_wallpaper())
set_wallpaper("location/to/image.jpg")

This shows the error that win-wallpaper is not regognised as internal or external command

how to print the path of current wallpaper path in windows using python?

1 Answers

The library have a bug. To overcome follow these steps.

1. Open wallpaper library directory and create a file name win-wallpaper.py.
2. Add following code into newly create module .

import ctypes,win32con
def getWallpaper():
    ubuf = ctypes.create_unicode_buffer(512)
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_GETDESKWALLPAPER,len(ubuf),ubuf,0)
    return ubuf.value

print(getWallpaper())

3. Then use pyinstaller to convert py to exe file.
Note: Don't use cmd for compile purpose .

pyinstaller --onefile -w 'win-wallpaper.py'

4. From dist folder take win-wallpaper.exe file to main directory. Now, it is good to go.

But one thing you should not here, this library still not contain proper implementation of set_wallpaper(path) function. This contain lots of bug so please use last code stuff.


If you don't want to use this library then use this code.

import ctypes,win32con

def getWallpaper():
    ubuf = ctypes.create_unicode_buffer(512)
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_GETDESKWALLPAPER,len(ubuf),ubuf,0)
    return ubuf.value

def setWallpaper(path):
    changed = win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE
    ctypes.windll.user32.SystemParametersInfoW(win32con.SPI_SETDESKWALLPAPER,0,path,changed)
Related