python 开机自动切换必应壁纸
需求
pip install requests pypiwin32
Python 脚本
import requests, json
import time, os, sys, getpass
import win32api, win32con, win32gui
BASE_URL='https://cn.bing.com'
API='https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN'
CMD = False
def log( msg:str ):
if(CMD):
print( msg )
else:
with open('D:/Documents/soft/bingWallpaper/bingWallpaper.log', mode='a+') as f:
data = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f.write('[ %s ] : %s \n'%(data, msg))
f.close()
def getFileName()->str:
name = time.strftime("%Y-%m-%d", time.localtime())
file_name = 'D:/Pictures/bingWallpaper/' + name + '.png'
return file_name
def getUrl()->str:
r = requests.get(API)
data = json.loads(r.text)
url = data['images'][0]['url']
return BASE_URL + url
def saveImage(url:str)->str:
file_name = getFileName()
log('保存文件路径' + file_name)
with open(file_name, mode = 'wb') as f:
data = requests.get(url)
log('图片下载情况:' + str(data.status_code))
f.write(data.content)
f.close()
return file_name
def setWallPaper(pic:str):
# open register
regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
# refresh screen
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
file_name = getFileName()
if(os.path.exists(file_name)):
log('今天已经设置过壁纸,跳过设置')
setWallPaper(file_name)
exit()
saveImage(getUrl())
setWallPaper(file_name)
开机启动 + 隐藏终端
- 新建一个 vbs 文件,写入以下内容
Set ws = CreateObject("Wscript.Shell")
ws.run "D:/Documents/soft/bingWallpaper/bingWallpaper.py",vbhide
- 将这个 vbs 复制到
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
。复制的时候火绒可能会提示
本来想用 C++ 写一个,但是我写 C++ 的环境在 Linux 上,win 下安装一个 C++ 环境太大了,索性用 Py写一个。
本来 MS 商店里的“Bing 壁纸下载”也挺好的,但是开机自启后不会自动退出,太麻烦了。相比 KDE 而言,会后台自动切换壁纸
后台运行参见:
https://blog.csdn.net/mochou111/article/details/100703183