Windows10、虚拟机Ubuntu20.04
本文juejin:https://juejin.cn/post/7129080519945355277/
本文52pojie:https://www.52pojie.cn/thread-1672344-1-1.html
本文CSDN:https://blog.csdn.net/hans774882968/article/details/126215309
作者:hans774882968以及hans774882968
查阅资料可知,控制壁纸幻灯片的文件是:/usr/share/backgrounds/contest/focal.xml
。
如果权限不够,就修改一下:
sudo chmod 777 focal.xml
原有内容结构如下:
<background>
<starttime>
<year>2020year>
<month>04month>
<day>01day>
<hour>00hour>
<minute>00minute>
<second>00second>
starttime>
background>
观察原有内容,我们归纳得出,控制一张图片的xml如下:
<static>
<duration>25.0duration>
<file>/home/xxx/图片/壁纸1.pngfile>
static>
<transition>
<duration>2.0duration>
<from>/home/xxx/图片/壁纸1.pngfrom>
<to>/home/xxx/图片/壁纸2.pngto>
transition>
因此,我们只需要提供:static持续的时间、transition持续的时间(均以秒为单位)、当前图片和下一张图片。
关于脚本:
file_template.xml
的主体部分,用%s
表示。求好主体部分imgs_xml
后,用file_template % imgs_xml
来添加。utf-8
。minidom
再实现一次,对比实现难度。使用时:
files
数组。/usr/share/backgrounds/contest/focal.xml
的原有内容。def get_file_template():
import sys
try:
with open('file_template.xml', 'r', encoding='utf-8') as f:
return f.read().strip()
except Exception as e:
print(e)
sys.exit(-1)
def get_imgs_xml():
static_duration = 25.0
transition_duration = 2.0
files = [
# 你的图片的绝对路径,如'/home/xxx/图片/1.png'
]
one_img_template = '''
{static_duration}
{cur_path}
{transition_duration}
{cur_path}
{next_path}
'''.lstrip('\n')
a = []
for i, cur_path in enumerate(files):
next_path = files[(i + 1) % len(files)]
a.append(one_img_template.format(
cur_path=cur_path, next_path=next_path,
static_duration=static_duration,
transition_duration=transition_duration
))
return ''.join(a)
if __name__ == '__main__':
file_template = get_file_template()
imgs_xml = get_imgs_xml()
content = file_template % imgs_xml
with open('focal.xml', 'w', encoding='utf-8') as f:
f.write(content)
命令式地操作xml,不符合直觉,感觉更难写!
from xml.dom.minidom import Document
def txt_node(txt):
return doc.createTextNode(str(txt))
def sub_node(name, text):
txt_nd = txt_node(text)
node = doc.createElement(name)
node.appendChild(txt_nd)
return node
doc = Document()
root = doc.createElement('background')
doc.appendChild(root)
start_time = doc.createElement('starttime')
time_arr = [
('year', '2020'), ('month', '04'), ('day', '01'),
('hour', '00'), ('minute', '00'), ('second', '00')
]
for t in time_arr:
txt = txt_node(t[1])
t_node = doc.createElement(t[0])
t_node.appendChild(txt)
start_time.appendChild(t_node)
root.appendChild(start_time)
static_duration = 25.0
transition_duration = 2.0
files = [
# 你的图片的绝对路径,如'/home/xxx/图片/1.png'
]
for i, cur_path in enumerate(files):
# static node
next_path = files[(i + 1) % len(files)]
duration_node = sub_node('duration', static_duration)
file_node = sub_node('file', cur_path)
static_node = doc.createElement('static')
static_node.appendChild(duration_node)
static_node.appendChild(file_node)
# transition node
duration_node = sub_node('duration', transition_duration)
from_node = sub_node('from', cur_path)
to_node = sub_node('to', next_path)
transition_node = doc.createElement('transition')
transition_node.appendChild(duration_node)
transition_node.appendChild(from_node)
transition_node.appendChild(to_node)
root.appendChild(static_node)
root.appendChild(transition_node)
with open('focal_minidom.xml', 'w', encoding='utf-8') as f:
f.write(doc.toprettyxml(indent=' ', encoding='utf-8').decode('utf-8'))