用Python自动化你的日常:5个拿来即用的效率脚本(附源码)

张开发
2026/4/20 14:37:24 15 分钟阅读

分享文章

用Python自动化你的日常:5个拿来即用的效率脚本(附源码)
用Python自动化你的日常5个拿来即用的效率脚本附源码每次看到桌面上堆积如山的文件或是重复点击几十次鼠标完成机械操作时总忍不住想——要是能有个小助手自动处理这些杂事该多好。作为已经掌握Python基础语法的你其实完全可以用20行代码打造专属自动化工具。下面这5个脚本都是我日常办公中反复打磨的实战利器从文件整理到智能提醒覆盖最常见的低效场景。1. 智能桌面管家按扩展名自动归档文件杂乱无章的桌面堪称效率杀手。这个脚本能监控指定目录根据文件类型自动创建文件夹并归类import os import shutil def organize_desktop(path~/Desktop): desktop os.path.expanduser(path) for item in os.listdir(desktop): item_path os.path.join(desktop, item) if os.path.isfile(item_path): # 获取文件扩展名并创建对应文件夹 ext os.path.splitext(item)[1][1:].lower() if not ext: ext other target_dir os.path.join(desktop, ext) os.makedirs(target_dir, exist_okTrue) shutil.move(item_path, os.path.join(target_dir, item)) if __name__ __main__: organize_desktop()提示添加watchdog库可以实现实时监控文件新增时自动触发归类关键点解析os.path.expanduser自动处理跨平台的用户目录路径exist_okTrue避免文件夹已存在时报错扩展名处理逻辑兼容无后缀文件进阶改造建议添加白名单机制跳过正在使用的文件对图片/文档等大类进行二次分类增加日志记录功能2. 电商价格追踪器降价自动提醒这个脚本每周帮我省下数百元购物预算核心原理是定时抓取商品页面并解析价格import requests from bs4 import BeautifulSoup import smtplib from email.mime.text import MIMEText def check_price(url, target_price, email): headers {User-Agent: Mozilla/5.0} response requests.get(url, headersheaders) soup BeautifulSoup(response.text, html.parser) # 根据具体网站结构调整选择器 price float(soup.select_one(.price-block).text.strip(¥)) title soup.title.text if price target_price: send_alert(email, f{title} 已降价至 {price}, url) def send_alert(to, content, link): msg MIMEText(f{content}\n购买链接{link}) msg[Subject] 价格提醒 msg[From] your_emailexample.com msg[To] to with smtplib.SMTP(smtp.example.com, 587) as server: server.starttls() server.login(your_emailexample.com, password) server.send_message(msg) # 示例监控某显示器价格 check_price(https://example.com/product/123, 1999, your_emailexample.com)实际应用时需要根据目标网站调整使用浏览器开发者工具分析价格元素选择器部分网站需要处理动态加载内容可添加selenium敏感操作建议设置请求间隔时间3. 日报自动生成器聚合多平台数据市场人员每天需要汇总各平台数据这个脚本可自动抓取关键指标生成可视化报告import pandas as pd from matplotlib import pyplot as plt def generate_daily_report(date): # 模拟从不同API获取数据 weibo_data get_weibo_stats(date) zhihu_data get_zhihu_stats(date) bilibili_data get_bilibili_stats(date) df pd.DataFrame([weibo_data, zhihu_data, bilibili_data]) df.to_excel(freport_{date}.xlsx, indexFalse) # 生成折线图 plt.style.use(ggplot) df.plot(kindbar, xplatform, yengagement) plt.savefig(fengagement_{date}.png) def get_weibo_stats(date): return {platform: 微博, followers: 15000, engagement: 4.2} # 其他平台数据获取函数类似...典型扩展方向添加自动邮件发送功能集成更多数据源微信、抖音等增加异常数据检测逻辑生成PDF格式完整报告4. 智能邮件批处理自动分类与回复处理海量邮件时这个脚本可以自动识别邮件类型并执行预设操作import imaplib import email from email.header import decode_header def process_emails(username, password): mail imaplib.IMAP4_SSL(imap.example.com) mail.login(username, password) mail.select(inbox) _, msgnums mail.search(None, UNSEEN) for num in msgnums[0].split(): _, data mail.fetch(num, (RFC822)) msg email.message_from_bytes(data[0][1]) subject decode_header(msg[Subject])[0][0] if isinstance(subject, bytes): subject subject.decode() # 根据关键词分类 if 会议 in subject: auto_reply(msg, 已收到会议邀请) mail.store(num, FLAGS, \\Flagged) elif 报表 in subject: save_attachment(msg, /reports) def auto_reply(msg, content): # 实现自动回复逻辑 pass def save_attachment(msg, path): # 实现附件保存逻辑 pass安全注意事项使用keyring库安全存储密码处理附件时进行病毒扫描设置执行频率避免被封禁5. 自动化办公助手GUI操作录制与回放对于没有API的老旧系统这个脚本通过记录鼠标键盘操作实现自动化import pyautogui import time import json class ActionRecorder: def __init__(self): self.actions [] def record(self, duration10): start time.time() while time.time() - start duration: pos pyautogui.position() self.actions.append({ time: time.time() - start, position: pos, click: pyautogui.mouseDown() }) time.sleep(0.1) def save(self, path): with open(path, w) as f: json.dump(self.actions, f) def replay(self, path): with open(path) as f: actions json.load(f) for action in actions: pyautogui.moveTo(action[position]) if action[click]: pyautogui.click() time.sleep(action[time]) # 使用示例 recorder ActionRecorder() recorder.record(5) # 录制5秒操作 recorder.save(action.json) recorder.replay(action.json) # 回放操作注意操作间隔建议不小于0.1秒避免执行过快导致系统异常增强功能建议添加异常中断保护机制支持操作步骤编辑增加图像识别定位功能录制键盘输入事件这些脚本最妙的地方在于可以随意组合——比如把价格追踪器和邮件通知结合或者用文件整理脚本处理日报生成器输出的报告。当你在各个场景中不断迭代这些工具时会明显感受到Python从编程语言变成真正的效率倍增器。

更多文章