在日常工作与学习中,我们经常会遇到重复繁琐的任务,比如批量处理文件、自动整理数据、定时发送消息等。Python凭借其简洁的语法和丰富的第三方库,成为实现自动化的绝佳工具。本文将分享5个高频实用的Python自动化脚本,涵盖文件处理、数据整理、网络请求等多个场景,你可以直接复用或根据需求微调,轻松提升工作效率。
一、批量文件重命名脚本
1. 功能介绍
批量将指定文件夹内的文件按照“前缀+序号”的格式重命名,支持过滤特定后缀的文件(如只重命名.jpg、.txt文件),适用于照片整理、文档分类等场景。
2. 实现代码
import os def batch_rename_files(folder_path, prefix, file_suffix=None, start_num=1): """ 批量重命名文件 :param folder_path: 文件夹路径 :param prefix: 重命名后的前缀 :param file_suffix: 需过滤的文件后缀(如'.jpg'),None则匹配所有文件 :param start_num: 序号起始值 """ # 检查文件夹路径是否存在 if not os.path.exists(folder_path): print(f"错误:文件夹路径 '{folder_path}' 不存在") return # 遍历文件夹内的文件 for file_name in os.listdir(folder_path): # 跳过子文件夹,只处理文件 file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): # 过滤指定后缀的文件 if file_suffix is None or file_name.endswith(file_suffix): # 获取文件后缀 suffix = os.path.splitext(file_name)[1] # 构建新文件名 new_file_name = f"{prefix}_{start_num}{suffix}" new_file_path = os.path.join(folder_path, new_file_name) # 重命名文件 os.rename(file_path, new_file_path) print(f"已重命名:{file_name} -> {new_file_name}") start_num += 1 print("批量重命名完成!") # 示例:将./photos文件夹内的.jpg文件重命名为"旅行_1.jpg"、"旅行_2.jpg"... if __name__ == "__main__": batch_rename_files( folder_path="./photos", prefix="旅行", file_suffix=".jpg", start_num=1 )3. 使用说明
1. 修改folder_path为目标文件夹路径(相对路径或绝对路径均可);2. 设置prefix为自定义前缀;3. 若需过滤文件类型,指定file_suffix(如.txt、.png),不指定则重命名文件夹内所有文件;4. 运行脚本即可完成批量重命名。
二、Excel数据批量整理脚本
1. 功能介绍
自动读取多个Excel文件中的指定sheet,合并数据并去重,筛选出符合条件的数据(如某列值大于指定阈值),最终保存为新的Excel文件。适用于报表合并、数据统计等场景,依赖pandas库。
2. 实现代码
import pandas as pd import os def batch_process_excel(source_folder, target_file, sheet_name="Sheet1", filter_col=None, filter_threshold=None): """ 批量处理Excel数据:合并、去重、筛选 :param source_folder: 源Excel文件所在文件夹 :param target_file: 输出的目标Excel文件路径 :param sheet_name: 需读取的sheet名称 :param filter_col: 筛选列名(None则不筛选) :param filter_threshold: 筛选阈值(filter_col不为None时生效) """ # 存储所有读取的数据 all_data = [] # 遍历文件夹内的Excel文件 for file_name in os.listdir(source_folder): if file_name.endswith((".xlsx", ".xls")): file_path = os.path.join(source_folder, file_name) try: # 读取指定sheet的数据 df = pd.read_excel(file_path, sheet_name=sheet_name) # 添加来源文件名列(可选) df["来源文件"] = file_name all_data.append(df) print(f"已读取:{file_name}") except Exception as e: print(f"读取{file_name}失败:{str(e)}") # 合并所有数据 if not all_data: print("错误:未找到可读取的Excel文件") return merged_df = pd.concat(all_data, ignore_index=True) # 去重(根据所有列去重,可根据需求调整subset参数) merged_df = merged_df.drop_duplicates() # 数据筛选 if filter_col and filter_threshold is not None: if filter_col in merged_df.columns: merged_df = merged_df[merged_df[filter_col] > filter_threshold] print(f"已筛选出{filter_col}大于{filter_threshold}的数据") else: print(f"警告:筛选列'{filter_col}'不存在,跳过筛选") # 保存到目标Excel文件 merged_df.to_excel(target_file, index=False) print(f"数据处理完成!已保存到:{target_file}") print(f"最终数据行数:{len(merged_df)}") # 示例:合并./excel_files文件夹内所有Excel的Sheet1,筛选出"销售额"大于1000的数据,保存到./合并结果.xlsx if __name__ == "__main__": # 安装依赖:pip install pandas openpyxl(openpyxl用于读写xlsx文件) batch_process_excel( source_folder="./excel_files", target_file="./合并结果.xlsx", sheet_name="Sheet1", filter_col="销售额", filter_threshold=1000 )3. 使用说明
1. 先安装依赖:pip install pandas openpyxl;2. 将需要合并的Excel文件放入source_folder指定的文件夹;3. 设置target_file为输出文件路径;4. 若需筛选数据,指定filter_col(列名)和filter_threshold(阈值),不筛选则设为None;5. 运行脚本即可完成数据合并与整理。
三、定时发送邮件脚本
1. 功能介绍
自动发送邮件,支持添加文本内容、附件,可设置定时发送(如每天固定时间发送)。适用于工作汇报、定时提醒等场景,使用Python内置的smtplib和email库,支持QQ邮箱、163邮箱等。
2. 实现代码
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header import time from datetime import datetime def send_email(sender, sender_pwd, receiver, subject, content, attachment_path=None, smtp_server="smtp.qq.com", smtp_port=465): """ 发送邮件(支持附件) :param sender: 发件人邮箱 :param sender_pwd: 发件人邮箱授权码(非登录密码) :param receiver: 收件人邮箱(可多个,用列表表示) :param subject: 邮件主题 :param content: 邮件正文(支持HTML格式) :param attachment_path: 附件路径(None则无附件) :param smtp_server: SMTP服务器地址 :param smtp_port: SMTP服务器端口 """ # 构建邮件对象 msg = MIMEMultipart() msg["From"] = Header(sender, "utf-8") msg["To"] = Header(",".join(receiver), "utf-8") if isinstance(receiver, list) else Header(receiver, "utf-8") msg["Subject"] = Header(subject, "utf-8") # 添加邮件正文 msg.attach(MIMEText(content, "html", "utf-8")) # 添加附件 if attachment_path and os.path.exists(attachment_path): with open(attachment_path, "rb") as f: attachment = MIMEText(f.read(), "base64", "utf-8") attachment["Content-Type"] = "application/octet-stream" # 附件文件名 file_name = os.path.basename(attachment_path) attachment["Content-Disposition"] = f'attachment; filename="{file_name}"' msg.attach(attachment) print(f"已添加附件:{file_name}") # 发送邮件 try: with smtplib.SMTP_SSL(smtp_server, smtp_port) as server: server.login(sender, sender_pwd) server.sendmail(sender, receiver, msg.as_string()) print(f"邮件发送成功!发送时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") except Exception as e: print(f"邮件发送失败:{str(e)}") def timed_send_email(hour, minute, *args, **kwargs): """ 定时发送邮件 :param hour: 定时小时(24小时制) :param minute: 定时分钟 :param args: send_email的位置参数 :param kwargs: send_email的关键字参数 """ while True: now = datetime.now() # 判断是否到达指定时间 if now.hour == hour and now.minute == minute: send_email(*args, **kwargs) # 发送完成后,等待24小时再继续检测(避免重复发送) time.sleep(24 * 60 * 60) # 每分钟检测一次 time.sleep(60) # 示例:每天18:00发送邮件,附带工作汇报附件 if __name__ == "__main__": # 注意:发件人邮箱需开启SMTP服务,获取授权码(如QQ邮箱在设置-账户中开启) sender_info = { "sender": "your_email@qq.com", "sender_pwd": "your_auth_code", # 授权码而非登录密码 "receiver": ["receiver1@xxx.com", "receiver2@xxx.com"], "subject": "每日工作汇报", "content": "<p>各位领导、同事:</p><p>以下是今日工作汇报,请查收。</p>", "attachment_path": "./每日工作汇报.docx", "smtp_server": "smtp.qq.com", # QQ邮箱SMTP服务器 "smtp_port": 465 } # 每天18:00发送 timed_send_email(hour=18, minute=0, **sender_info)3. 使用说明
1. 发件人邮箱需开启SMTP服务(如QQ邮箱在“设置-账户”中开启),获取授权码(替代登录密码);2. 修改sender_info中的发件人、收件人、邮件主题、正文等信息;3. 若需添加附件,指定attachment_path;4. 设置timed_send_email的hour和minute为定时时间;5. 运行脚本,保持脚本运行状态即可实现定时发送。
四、网页内容爬取与保存脚本
1. 功能介绍
爬取指定网页的文本内容(如新闻、博客),自动去除多余空格和标签,保存为本地txt文件。支持批量爬取多个网页,依赖requests和BeautifulSoup库,适用于资料收集、内容备份等场景。
2. 实现代码
import requests from bs4 import BeautifulSoup import os def crawl_web_content(url_list, save_folder="./爬取内容"): """ 批量爬取网页内容并保存为txt文件 :param url_list: 网页URL列表 :param save_folder: 保存文件夹路径 """ # 创建保存文件夹 if not os.path.exists(save_folder): os.makedirs(save_folder) # 模拟浏览器请求头(避免被反爬) headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" } for idx, url in enumerate(url_list, start=1): try: # 发送请求获取网页内容 response = requests.get(url, headers=headers, timeout=10) response.raise_for_status() # 抛出HTTP错误 response.encoding = response.apparent_encoding # 自动识别编码 # 解析网页内容,提取文本 soup = BeautifulSoup(response.text, "html.parser") # 去除script、style标签 for script in soup(["script", "style"]): script.decompose() # 获取文本内容,去除多余空格 text_content = soup.get_text(strip=True, separator="\n") # 构建保存文件名 file_name = f"网页内容_{idx}.txt" file_path = os.path.join(save_folder, file_name) # 保存到txt文件 with open(file_path, "w", encoding="utf-8") as f: f.write(f"网页URL:{url}\n") f.write("-" * 50 + "\n") f.write(text_content) print(f"爬取成功:{url} -> 保存为 {file_name}") except Exception as e: print(f"爬取{url}失败:{str(e)}") print("批量爬取完成!") # 示例:爬取3个指定网页的内容 if __name__ == "__main__": # 安装依赖:pip install requests beautifulsoup4 target_urls = [ "https://www.example.com/article1", "https://www.example.com/article2", "https://www.example.com/article3" ] crawl_web_content(url_list=target_urls, save_folder="./网页资料")3. 使用说明
1. 安装依赖:pip install requests beautifulsoup4;2. 将需要爬取的网页URL放入target_urls列表;3. 设置save_folder为保存文件夹路径;4. 运行脚本即可批量爬取内容并保存为txt文件。注意:爬取前请遵守目标网站的robots协议,避免过度爬取。
五、文件夹自动备份脚本
1. 功能介绍
import zipfile import os import time from datetime import datetime, timedelta def backup_folder(source_folder, backup_path, keep_days=7): """ 文件夹备份:打包为zip,删除旧备份 :param source_folder: 需要备份的源文件夹 :param backup_path: 备份文件保存路径 :param keep_days: 保留备份的天数,超过则删除 """ # 检查源文件夹是否存在 if not os.path.exists(source_folder): print(f"错误:源文件夹 '{source_folder}' 不存在") return # 创建备份路径(若不存在) if not os.path.exists(backup_path): os.makedirs(backup_path) # 构建备份文件名(包含时间戳,避免重复) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") folder_name = os.path.basename(source_folder) backup_filename = f"{folder_name}_backup_{timestamp}.zip" backup_filepath = os.path.join(backup_path, backup_filename) # 打包文件夹为zip try: with zipfile.ZipFile(backup_filepath, "w", zipfile.ZIP_DEFLATED) as zipf: # 遍历源文件夹内的所有文件和子文件夹 for root, dirs, files in os.walk(source_folder): for file in files: file_path = os.path.join(root, file) # 计算文件在zip中的相对路径(避免包含完整源路径) arcname = os.path.relpath(file_path, source_folder) zipf.write(file_path, arcname=arcname) print(f"备份成功:{source_folder} -> {backup_filename}") except Exception as e: print(f"备份失败:{str(e)}") return # 删除超过keep_days天的旧备份 now = datetime.now() for file_name in os.listdir(backup_path): if file_name.startswith(f"{folder_name}_backup_") and file_name.endswith(".zip"): file_path = os.path.join(backup_path, file_name) # 获取文件创建时间 create_time = datetime.fromtimestamp(os.path.getctime(file_path)) # 计算时间差 time_diff = now - create_time if time_diff > timedelta(days=keep_days): os.remove(file_path) print(f"已删除旧备份:{file_name}(创建时间:{create_time.strftime('%Y-%m-%d %H:%M:%S')})") print("备份清理完成!") # 示例:备份./important_files文件夹到./backups,保留7天备份 if __name__ == "__main__": backup_folder( source_folder="./important_files", backup_path="./backups", keep_days=7 ) # 若需定时备份,可结合定时任务工具(如Windows任务计划程序、Linux crontab)自动将指定文件夹打包为zip压缩包,备份到目标路径(支持本地路径或网络共享路径),并删除超过指定天数的旧备份文件,避免占用过多存储空间。适用于重要文件的定期备份场景。
2. 实现代码
3. 使用说明
1. 修改source_folder为需要备份的文件夹路径;2. 设置backup_path为备份文件保存路径;3. 调整keep_days为保留备份的天数(默认7天);4. 运行脚本即可完成备份。若需定期自动备份,可配合系统定时任务工具(Windows任务计划程序、Linux crontab)设置定时运行脚本。
总结
以上5个Python自动化脚本覆盖了文件处理、数据整理、邮件发送、网页爬取和文件夹备份等常见场景,代码简洁易懂且可灵活调整。通过自动化这些重复任务,能够大幅节省时间和精力。在使用过程中,可根据实际需求修改脚本参数,或扩展功能(如添加日志记录、异常报警等)。如果需要其他场景的自动化脚本,也可以基于Python的相关库进行开发。