Python实现设置windows桌面壁纸代码分享

时间:2021-05-22

每天换一个壁纸,每天好心情。

# -*- coding: UTF-8 -*- from __future__ import unicode_literalsimport Imageimport datetimeimport win32gui,win32con,win32apiimport refrom HttpWrapper import SendRequestStoreFolder = "c:\\dayImage"def setWallpaperFromBMP(imagepath): k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE) win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中 win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0") win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)def setWallPaper(imagePath): """ Given a path to an image, convert it to bmp and set it as wallpaper """ bmpImage = Image.open(imagePath) newPath = StoreFolder + '\\mywallpaper.bmp' bmpImage.save(newPath, "BMP") setWallpaperFromBMP(newPath)def getPicture(): url = "http://photography.nationalgeographic.com/photography/photo-of-the-day/" h = SendRequest(url) if h.GetSource(): r = re.findall('<div class="download_link"><a href="(.*?)">Download',h.GetSource()) if r: return SendRequest(r[0]).GetSource() else: print "解析图片地址出错,请检查正则表达式是否正确" return Nonedef setWallpaperOfToday(): img = getPicture() if img: path = StoreFolder + "\\%s.jpg" % datetime.date.today() f = open(path,"wb") f.write(img) f.close() setWallPaper(path)setWallpaperOfToday()print 'Wallpaper set ok!'


其中的httpwrapper是我写的一个http访问的封装:

#!/usr/bin/env python # -*- coding: UTF-8 -*-#-------------------------------------------------------------------------------# Name: 对http访问的封装## Author: qianlifeng## Created: 10-02-2012#-------------------------------------------------------------------------------import base64import urllibimport urllib2import timeimport reimport sysclass SendRequest: """ 网页请求增强类 SendRequest('http://xxx.com',data=dict, type='POST', auth='base',user='xxx', password='xxx') """ def __init__(self, url, data=None, method='GET', auth=None, user=None, password=None, cookie = None, **header): """ url: 请求的url,不能为空 date: 需要post的内容,必须是字典 method: Get 或者 Post,默认为Get auth: 'base' 或者 'cookie' user: 用于base认证的用户名 password: 用于base认证的密码 cookie: 请求附带的cookie,一般用于登录后的认证 其他头信息: e.g. referer='") print b.GetSource()

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章