Python更換windows桌面
目錄- Python更換windows桌面
- 前言
- 準備作業
- 代碼
- 效果展示
- Tips-如何更有儀式感
前言
每天下班,有時候會留下一些事情需要明天更進
為了防止忘記,之前會寫在txt里面

就算txt放在顯眼的位置,有時候還是會忘記
所以想要將文本輸出到桌面壁紙上,加粗高亮,這樣就不會忘了
準備作業
有一些庫的下載可能會很慢,所以推薦使用阿里云鏡像
對于網路好的用戶,這一步可以跳過
升級pip版本
python -m pip install --upgrade pip
設定阿里云鏡像
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
取消使用阿里云鏡像
pip config unset global.index-url
代碼
Python官方檔案
https://docs.python.org/3/library/ctypes.html
ctypes是 Python 的外部函式庫,它提供 C 兼容的資料型別,并允許呼叫 DLL 或共享庫中的函式,它可用于將這些庫包裝在純 Python 中,
pip install pillow
import ctypes
from PIL import Image, ImageFont, ImageDraw
def change_wallpaper():
# path 桌面壁紙路徑
path = "C:\Windows\Web\Wallpaper\Theme1\img2.jpg"
img = Image.open(path)
# 選擇字體樣式 大小 顏色
font = ImageFont.truetype("C:\Windows\Fonts\msyh.ttc", 155)
color = (52, 51, 85)
# txt_path txt檔案位置 我放在桌面 r表示不認為\為轉義符
txt_path = r"C:\Users\Zzz\Desktop\rember.txt"
# 記錄txt中內容 放在list中
word = []
with open(txt_path, encoding='utf-8') as file:
for line in file.readlines():
word.append(line)
draw = ImageDraw.Draw(img)
width = img.width
fixed_with = width / 5
total = 1
# fixed_with 文字所在x軸起始位置
for message in word:
start_height = 100
# changed_height 文字所在y軸高度
# 155 : 文字之間間隔 可以參考font字體大小
changed_height = start_height + total * 155
# 將txt中文字寫入圖片 x,y軸位置
position = (fixed_with, changed_height)
draw.text(position, message, color, font=font)
# 寫一行 空一行
position = (fixed_with, changed_height + 155)
draw.text(position, "", color, font=font)
total = total + 2
# file_name 生成圖片存放位置
file_name = r"C:\Users\Zzz\Desktop\background.png"
img.save(file_name)
# 設定壁紙
ctypes.windll.user32.SystemParametersInfoW(20, 0, file_name, 0) # 設定桌面壁紙
if __name__ == '__main__':
change_wallpaper()
效果展示
- 檔案中資訊

- 壁紙效果

Tips-如何更有儀式感
- 桌面上新建一個txt

- 寫入兩行腳本
cmd /c python D:\a_projects\python\day_1\com\lizi\background.py
del C:\Users\Zzz\Desktop\background.png
cmd /c : 打開cmd視窗 運行完關閉
python D:\a_projects\python\day_1\com\lizi\background.py :用python執行檔案
del C:\Users\Zzz\Desktop\background.png : 洗掉生成的圖片
3. 將txt后綴 改為bat

每天更新完rember.txt后 雙擊bat腳本 就能自動替換桌面壁紙了
本文來自博客園,作者:貍子橘花茶,轉載請注明原文鏈接:https://www.cnblogs.com/yusishi/p/15953044.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/436952.html
標籤:Python
