Skip to content

Instantly share code, notes, and snippets.

@zhengzhou
Last active August 29, 2015 14:02
Show Gist options
  • Save zhengzhou/4bfba5dc588d1c86eed5 to your computer and use it in GitHub Desktop.
Save zhengzhou/4bfba5dc588d1c86eed5 to your computer and use it in GitHub Desktop.
ubuntu自动切换壁纸
#!/usr/bin/python
# -*- coding:utf8 -*-
# 修改/usr/share/gnome-background-properties/ubuntu-wallpapers.xml加入如下node: 
# <wallpaper deleted="false">
# <name>bing Community Wallpapers</name>
# <filename>/usr/share/backgrounds/BingWallpapers/bing.xml</filename>
# <options>zoom</options>
# </wallpaper>
import os
import sys
from xml.dom.minidom import Document
# 批量格式化文件名
def reName(path,pattern):
files = os.listdir(path)
dot_index = pattern.find('.')
file_prefix = pattern[:dot_index]
file_suffix = pattern[dot_index:]
index =0
for f, pathdirs in files:
index+=1
new_file_name = file_prefix+str(index)+file_suffix
if f.find(file_suffix)>0:
os.rename(f,new_file_name)
print new_file_name
# 生成配置文件 输入所在文件夹路径,生成配置文件的文件名,图片后缀
def create_config_file(path,filename,file_suffix):
doc = Document()
root = creat_root(doc)
mfiles = os.listdir(path)
files=mfiles[:]
for f in files:
if f.find(file_suffix) == -1:
files.remove(f)
continue
for f in files:
root.appendChild(createStaticNode(doc,path+f))
index = files.index(f)
if index<(len(files)-1):
root.appendChild(create_transition_node(doc,path+f,path+files[index+1]))
########### 将DOM对象doc写入文件
print root.toprettyxml()
f = open(filename,'w')
f.write(root.toprettyxml())
# root.writexml(f,' ','\n','utf-8')
f.close()
def creat_root(doc):
background = doc.createElement("background")
starttime = doc.createElement("starttime")
year = doc.createElement("year")
month = doc.createElement("month")
day = doc.createElement("day")
hour = doc.createElement("hour")
minute = doc.createElement("minute")
second = doc.createElement("second")
year_text=doc.createTextNode("2014")
month_text=doc.createTextNode("6")
day_text=doc.createTextNode("1")
hour_text=doc.createTextNode("0")
minute_text=doc.createTextNode("0")
second_text=doc.createTextNode("0")
year.appendChild(year_text)
month.appendChild(month_text)
day.appendChild(day_text)
hour.appendChild(hour_text)
minute.appendChild(minute_text)
second.appendChild(second_text)
background.appendChild(starttime)
starttime.appendChild(year)
starttime.appendChild(month)
starttime.appendChild(day)
starttime.appendChild(hour)
starttime.appendChild(minute)
starttime.appendChild(second)
return background
def createStaticNode(doc,picFilePath):
static = doc.createElement("static")
duration = doc.createElement("duration")
picFile = doc.createElement("file")
duration_text = doc.createTextNode("1795")
picFile_text = doc.createTextNode(picFilePath)
duration.appendChild(duration_text)
picFile.appendChild(picFile_text)
static.appendChild(duration)
static.appendChild(picFile)
return static
def create_transition_node(doc,from_file,to_file):
transition = doc.createElement("transition")
duration = doc.createElement("duration")
pic_from = doc.createElement("from")
pic_to = doc.createElement("to")
duration_text = doc.createTextNode("10")
pic_from_text = doc.createTextNode(from_file)
pic_to_text = doc.createTextNode(to_file)
duration.appendChild(duration_text)
pic_from.appendChild(pic_from_text)
pic_to.appendChild(pic_to_text)
transition.appendChild(duration)
transition.appendChild(pic_from)
transition.appendChild(pic_to)
return transition
#reName( ".","bing.jpg")
if __name__=='__main__':
ABSPATH=os.path.abspath(sys.argv[0])
ABSPATH=os.path.dirname(ABSPATH)+"/"
#默认所有图片在当前脚本路径
print ABSPATH
create_config_file(ABSPATH,'bing.xml','jpg')
@zhengzhou
Copy link
Author

 修改/usr/share/gnome-background-properties/ubuntu-wallpapers.xml加入如下node: 

bing Community Wallpapers
/usr/share/backgrounds/BingWallpapers/bing.xml
zoom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment