Python更换Windows壁纸,问题与解决方案

使用必应 的过程中,发现必应的背景图片其实是很美的。这就产生抓取其背景图片作为桌面的想法。
思路很简单,首先下载必应主页的源码,通过正则表达式搜说出每天的背景图片的偏移地址,然后与主页地址拼接获得完整路径,然后下载下来保存,再设置成壁纸。
在这过程过程中,遇到了一些问题,最后经过搜索与尝试,也都解决了,现在记录下来。

问题一

首先,是安装BeautifulSoup 过程中遇到的问题。由于我安装的是python3.6,安装BS4 的时候,一直提示没有这个模块,可是我明明安装了。最后的解决方案是:
1. 如果是python 2.X,直接安装就好了;
2. 如果是python 3.X,首先要把这里的代码全部由python2格式转换成python3格式。这个工作可以使用python里自带的2to3.py来完成。这个工具在python\Tools\Scripts里。
使用的格式为:
* 2to3.py -w C:\Python33\beautifulsoup4-4.3.2 这样,就可以开始安装了。
* 把下载好的文件夹放到python根目录里,在此路径执行:
* python setup.py build
* python setup.py install

问题二

使用python设置壁纸的时候提示

win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2) 另一个程序正在使用,无法访问
如果出现这个问题,很可能是python在调用这个函数之前还进行了一些需要系统支持的调用,比如我就是使用open()函数没关闭造成这个错误;

问题三

pywintypes.error: (0, ‘SystemParametersInfo’, ‘No error message is available’)
出现这个问题是因为使用win32gui.SystemParametersInfo() 时,图片格式一定是.bmp格式。因此需要使用Image模块将其转换。

 def setWallPaperBMP(imagePath):
    bmpImage = Image.open(imagePath)
    newPath = imagePath.replace('.jpg', '.bmp')
    bmpImage.save(newPath, "BMP")
    setWallpaper(newPath)

问题四

问题四其实也不是什么问题。因为我们使用的python编写的,由于众多的依赖库,导致基本只能是自己的电脑使用,要想分享出去,只能转成.exe格式。这里使用的是pyinstaller .手动配起来还是很麻烦的。好在,我们可以使用pip。在安装的过程中,它提示缺失什么模块,你就直接安装该模块就好了。
在我这,安装了只会,环境变量还是没配好。如果你在命令行使用pip命令时提示没有pip这个命令,那么你需要做 的就是把C:\XXX\Python\Python36-32\Scripts 配置进你的环境变量。至于C:\XXX 具体是什么,就看你安装python时候的路径了。至于打包的命令什么的,pyinstaller官网有挺详细的介绍的,这里就不赘述了。

总结

其实没啥总结的。放出源码吧:

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

from urllib.request import urlopen
from bs4 import BeautifulSoup
from PIL import Image
import win32gui
import win32con
import win32api
import re
import sys
import os

# Author  : ZMYCHOU
# Version :V1.0
# Since   :2017/09/02
# Modified:2017/09/02
# To run this program, you need follow module:
# Python 3.6
# win32 module,website :https://sourceforge.net/projects/pywin32/
# Image module,website :https://pypi.python.org/pypi/Pillow
# BeautifulSoup,website:https://www.crummy.com/software/BeautifulSoup/  
# Navigate to the directory where this source file is located from DOS command and then enter
# follow command:
# python LiveWallPaper.py 

def setWallpaper(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") 
    win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

def setWallPaperBMP(imagePath):
    bmpImage = Image.open(imagePath)
    newPath = imagePath.replace('.jpg', '.bmp')
    bmpImage.save(newPath, "BMP")
    setWallpaper(newPath)

# Get image from Bing
bing = urlopen("http://cn.bing.com/").read()
str = bing.decode("gbk",'ignore')
imgUrl = re.search(r'az/hprichbg/rb(.){1,100}.jpg?', str)
if imgUrl:
    print(imgUrl.group())
imgFile = open(os.getcwd() + r"\img.bmp", "wb+")
img = urlopen("http://cn.bing.com/" + imgUrl.group()).read()
imgFile.write(img)
imgFile.flush()
imgFile.close()
setWallPaperBMP(os.getcwd() + r"\img.bmp")
发布了45 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ZM_Yang/article/details/77801679
今日推荐