1. 自动化图像处理
调整水印大小、旋转或添加水印。库:Pillowfrom PIL import Image def resize_image(image_path, output_path, size): with Image.open(image_path) as img: img.resize(size).save(output_path)
2. 自动化网站监控
网站更新时通知。库:请求、时间import requests import time def monitor_website(url, interval): prev_content = None while True: response = requests.get(url) if response.text != prev_content: print("Website updated!") prev_content = response.text time.sleep(interval)
3. 自动化数据库备份
备份 MySQL 等数据库。库:subprocessimport subprocess def backup_mysql(user, password, db_name, output): cmd = f"mysqldump -u {user} -p{password} {db_name} > {output}" subprocess.run(cmd, shell=True)
4. 自动化 Slack 通知
以编程方式发送 Slack 消息。库: slack-sdkfrom slack_sdk import WebClient def send_slack_message(token, channel, text): client = WebClient(token=token) client.chat_postMessage(channel=channel, text=text)
5. 自动天气更新
获取天气数据。库:请求import requests def get_weather(api_key, city): url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}" return requests.get(url).json()
6. 自动化文本转语音
将文本转换为语音。库: pyttsx3import pyttsx3 def text_to_speech(text): engine = pyttsx3.init() engine.say(text) engine.runAndWait()
7. 自动货币转换
使用 API 转换货币。库: forex-pythonfrom forex_python.converter import CurrencyRates def convert_currency(amount, from_currency, to_currency): c = CurrencyRates() return c.convert(from_currency, to_currency, amount)
8. 自动化任务调度
计划 Python 任务。库:scheduleimport schedule import time def task(): print("Task running!") schedule.every().day.at("10:00").do(task) while True: schedule.run_pending() time.sleep(1)
9. 自动通知
将通知推送到您的手机。库: pushbulletfrom pushbullet import Pushbullet def send_notification(api_key, title, body): pb = Pushbullet(api_key) pb.push_note(title, body)
10. 自动目录清理
删除目录中的旧文件。库: os, timeimport os import time def cleanup(directory, days): now = time.time() for file in os.listdir(directory): filepath = os.path.join(directory, file) if os.stat(filepath).st_mtime < now - days * 86400: os.remove(filepath)
11. 自动化股价监控
获取股票价格。库: yfinanceimport yfinance as yf def get_stock_price(ticker): stock = yf.Ticker(ticker) return stock.history(period="1d")["Close"]
12. 自动生成二维码
为文本或 URL 生成 QR 码。库: qrcodeimport qrcode def generate_qr(data, filename): qr = qrcode.make(data) qr.save(filename)
13. 自动按键模拟
自动按下键盘。库: pyautoguiimport pyautogui def automate_typing(text): pyautogui.typewrite(text)
14. 自动化 Git 操作
自动化 git push/pull。库:subprocessimport subprocess def git_push(message): subprocess.run(["git", "add", "."]) subprocess.run(["git", "commit", "-m", message]) subprocess.run(["git", "push"])
15. 自动时间跟踪
跟踪您在任务上花费的时间。库:时间import time start_time = time.time() # Do some work print("Time spent:", time.time() - start_time)
这些脚本可以帮助您节省时间并简化重复性任务。将这些与 cron 作业或任务调度程序相结合,以解锁强大的自动化功能!