This article mainly introduces how to set windows desktop wallpaper using Python. This article provides the implementation code directly. if you need a friend, you can change the wallpaper every day.
#-*-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 stretch to adapt to the desktop, 0 desktop center 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 "bmp Image = image. open (imagePath) newPath = StoreFolder + '\ mywallpaper.bmp' BMP image. 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 ('Download ', h. getSource () if r: return SendRequest (r [0]). getSource () else: print "an error occurred while parsing the image address. check whether the regular expression is correct." 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! '
The httpwrapper is an http access encapsulation I wrote:
#! /Usr/bin/env python #-*-coding: UTF-8-*-# authorization # Name: Encapsulation of http access # Author: qianlifeng # Created: 10-02-2012 # encode import base64import urllibimport urllib2import timeimport reimport sysclass SendRequest: "webpage request enhancement class 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: requested url. The value of date cannot be blank. The value must be the dictionary method: Get or post. the default value is Get auth: 'base' or 'cookier' user: username used for base authentication password used for base authentication cookie: cookie included in the request, usually used for other Authentication Header information after logon: e.g. referer = 'www .sina.com.cn '"" self. url = Url self. data = data self. method = method self. auth = auth self. user = user self. password = password self. cookie = cookie if 'referer' in header: self. referer = header [referer] else: self. referer = None if 'user-agent' in header: self. user_agent = header [user-agent] else: # self. user_agent = 'mozilla/5.0 (Windows NT 5.1; rv: 8.0) Gecko/20100101 Firefox/8.0 'self. user_agent = 'mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16 'self. _ SetupRequest () self. _ SendRequest () def _ SetupRequest (self): if self. url is None or self. url = '': raise 'url cannot be blank! '# If self. method. lower () = 'Post': self. req = urllib2.Request (self. url, urllib. urlencode (self. data) elif self. method. lower () = 'get': if self. data = None: self. req = urllib2.Request (self. url) else: self. req = urllib2.Request (self. url + '? '+ Urllib. urlencode (self. data) # set the authentication information if self. auth = 'base': if self. user = None or self. password = None: raise 'The user or password was not given! 'Else: auth_info = base64.encodestring (self. user + ':' + self. password ). replace ('\ n', '') auth_info = 'basic' + auth_info self. req. add_header ("Authorization", auth_info) elif self. auth = 'cookies': if self. cookie = None: raise 'The cookie was not given! 'Else: self. req. add_header ("Cookie", self. cookie) if self. referer: self. req. add_header ('referer', self. referer) if self. user_agent: self. req. add_header ('User-agent', self. user_agent) def _ SendRequest (self): try: self. res = urllib2.urlopen (self. req) self. source = self. res. read () self. code = self. res. getcode () self. head_dict = self.Res.info (). dict self. res. close () handle T: print "Error: HttpWrapper => _ SendRequest", sys. exc_info () [1] def GetResponseCode (self): "gets the status code returned by the server (200 indicates success, 404 page does not exist)" return self. code def GetSource (self): "to obtain the source code of the webpage. after decoding, use" "if" source "in dir (self): return self. source return u''' def GetHeaderInfo (self): "u' gets the response header information '" "return self. head_dict def GetCookie (self): "Get the Cookie returned by the server, which is generally used for subsequent login operations" if 'set-cookies' in self. head_dict: return self. head_dict ['set-cooker'] else: return None def GetContentType (self): "returned type" if 'content-type' in self. head_dict: return self. head_dict ['content-type'] else: return None def GetCharset (self): "try to get the webpage code. if not, return None" contentType = self. getContentType () if contentType is not None: index = contentType. find ("charset") if index> 0: return contentType [index + 8:] return None def GetExpiresTime (self ): "get webpage Expiration Time" if 'expires' in self. head_dict: return self. head_dict ['expires'] else: return None def GetServerName (self): "get server Name" if 'server' in self. head_dict: return self. head_dict ['server'] else: return None _ all _ = [SendRequest,] if _ name _ = '_ main __': B = SendRequest (" http://www.baidu.com ") Print B. GetSource ()