Board logo

标题: [原创代码] 设置bing.com背景为壁纸,Python版 [打印本页]

作者: ivor    时间: 2018-2-16 01:33     标题: 设置bing.com背景为壁纸,Python版

没啥好说的,论坛很多以bing做壁纸的,再补上Python版本的
  1. #**************
  2. #coding=utf-8  
  3. #by ivor
  4. #设置bing.com背景为壁纸
  5. import re
  6. import requests
  7. import pythoncom  
  8. from win32com.shell import shell, shellcon  
  9. class MyPaper:
  10.    
  11.     def __init__(self):
  12.         self.image = r"C:\paper.jpg"
  13.         self.url = r'https://cn.bing.com/'
  14.         
  15.     def getDeskComObject(self):  
  16.         self.g_desk = None
  17.         if not self.g_desk:  
  18.             self.g_desk = pythoncom.CoCreateInstance(shell.CLSID_ActiveDesktop, \
  19.             None, pythoncom.CLSCTX_INPROC_SERVER, \
  20.             shell.IID_IActiveDesktop)
  21.         return self.g_desk  
  22.     def setWallPaper(self):
  23.         self.desktop = self.getDeskComObject()
  24.         if self.desktop:
  25.             self.desktop.SetWallpaper(self.image, 0)
  26.             self.desktop.ApplyChanges(shellcon.AD_APPLY_ALL)
  27.             
  28.     def addUrlLink(self, lnk):  
  29.         self.desktop = self.getDeskComObject()
  30.         self.desktop.AddUrl(0, lnk, 0, 0)
  31.         
  32.     def imgDownload(self):
  33.         r = requests.get(self.url).content
  34.         pattern = re.compile(r'url: "(.*jpg)?')
  35.         imgUrl = re.findall(pattern,r.decode(encoding="utf-8"))
  36.         imgData = requests.get(self.url + imgUrl[0]).content
  37.         with open(self.image ,"wb") as file:
  38.             file.write(imgData)
  39. setPaper = MyPaper()
  40. setPaper.imgDownload()
  41. setPaper.setWallPaper()
复制代码





欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2