1. 为什么需要Python自动化脚本?
每天重复点击鼠标、复制粘贴数据、整理文件...这些机械性工作正在吞噬我们宝贵的时间。作为从业十年的Python开发者,我深刻体会到:能用代码解决的问题,绝不动手操作。Python凭借简洁语法和丰富生态,成为自动化任务的首选工具。
最近整理了10个经过实战检验的脚本,覆盖文件处理、网页操作、办公自动化等高频场景。这些代码都经过生产环境验证,你可以在以下环境直接运行:
- Python 3.6+
- 常见操作系统(Windows/macOS/Linux)
- 无需特殊硬件配置
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 文件管理自动化方案
2.1 智能文件分类器
这个脚本能根据扩展名自动归类下载文件夹中的文件。我经常看到同事手动整理上百个文件,其实用30行代码就能解决:
python复制import os
import shutil
def auto_classify(download_path="~/Downloads"):
file_types = {
'images': ['.jpg', '.png', '.gif'],
'documents': ['.pdf', '.docx', '.txt'],
'archives': ['.zip', '.rar']
}
for filename in os.listdir(download_path):
if os.path.isfile(os.path.join(download_path, filename)):
file_ext = os.path.splitext(filename)[1].lower()
for folder, extensions in file_types.items():
if file_ext in extensions:
target_dir = os.path.join(download_path, folder)
os.makedirs(target_dir, exist_ok=True)
shutil.move(
os.path.join(download_path, filename),
os.path.join(target_dir, filename)
)
break
关键技巧:使用
exist_ok=True避免重复创建文件夹,比先检查再创建更高效
2.2 重复文件清理工具
这个脚本通过MD5校验识别重复文件,我的照片库用它清理出20GB空间:
python复制import hashlib
import os
def find_duplicates(root_dir):
hashes = {}
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
with open(filepath, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()
if file_hash in hashes:
print(f"发现重复文件: {filepath} 与 {hashes[file_hash]}")
else:
hashes[file_hash] = filepath
避坑指南:大文件读取可能内存溢出,可改为分块读取:
python复制def md5_chunked(filepath, chunk_size=8192): hash_md5 = hashlib.md5() with open(filepath, "rb") as f: for chunk in iter(lambda: f.read(chunk_size), b""): hash_md5.update(chunk) return hash_md5.hexdigest()
3. 网页自动化实战技巧
3.1 自动填写网页表单
用Selenium实现问卷自动填写,效率提升10倍:
python复制from selenium import webdriver
from selenium.webdriver.common.by import By
import time
def auto_survey(url):
driver = webdriver.Chrome()
driver.get(url)
# 等待页面加载
time.sleep(2)
# 定位并填写表单
name_field = driver.find_element(By.ID, "name")
name_field.send_keys("张三")
# 单选按钮选择
driver.find_element(By.XPATH, "//input[@value='option1']").click()
# 提交表单
submit_btn = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
submit_btn.click()
driver.quit()
实战经验:使用
WebDriverWait比固定sleep更可靠:python复制from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "dynamic-element")) )
3.2 定时网页截图监控
这个脚本每天定时对关键页面截图存档:
python复制from selenium import webdriver
import schedule
import time
def take_screenshot(url, save_path):
driver = webdriver.Chrome()
driver.get(url)
driver.save_screenshot(save_path)
driver.quit()
# 设置每天9点执行
schedule.every().day.at("09:00").do(
take_screenshot,
url="https://example.com",
save_path="/path/to/screenshot.png"
)
while True:
schedule.run_pending()
time.sleep(60)
4. 办公自动化高效方案
4.1 Excel报表自动生成
用openpyxl自动生成周报,省去每周2小时手工操作:
python复制from openpyxl import Workbook
from openpyxl.styles import Font, Alignment
import datetime
def generate_report(data):
wb = Workbook()
ws = wb.active
# 设置标题样式
title_font = Font(bold=True, size=14)
ws['A1'] = "销售周报"
ws['A1'].font = title_font
# 写入数据
for row in data:
ws.append(row)
# 自动调整列宽
for col in ws.columns:
max_length = 0
for cell in col:
try:
if len(str(cell.value)) > max_length:
max_length = len(cell.value)
except:
pass
adjusted_width = (max_length + 2) * 1.2
ws.column_dimensions[col[0].column_letter].width = adjusted_width
# 保存文件
filename = f"report_{datetime.date.today()}.xlsx"
wb.save(filename)
专业技巧:使用
StyleFrame可以更方便地应用复杂样式:python复制from styleframe import StyleFrame sf = StyleFrame(data_df) sf.apply_headers_style(styler_obj) sf.to_excel('styled_report.xlsx')
4.2 批量PDF转Word
用PyPDF2和python-docx实现文档格式转换:
python复制from PyPDF2 import PdfReader
from docx import Document
def pdf_to_word(pdf_path, docx_path):
pdf = PdfReader(pdf_path)
doc = Document()
for page in pdf.pages:
text = page.extract_text()
doc.add_paragraph(text)
doc.save(docx_path)
注意事项:处理扫描件PDF需要先用OCR识别,推荐
pytesseract:python复制import pytesseract from PIL import Image def ocr_pdf(pdf_path): images = convert_from_path(pdf_path) text = "" for img in images: text += pytesseract.image_to_string(img) return text
5. 系统运维自动化脚本
5.1 服务器日志分析器
这个脚本自动分析Nginx日志并发送异常报告:
python复制import re
from collections import defaultdict
import smtplib
from email.mime.text import MIMEText
def analyze_nginx_log(log_path):
error_pattern = re.compile(r'5\d{2}')
ip_pattern = re.compile(r'\d+\.\d+\.\d+\.\d+')
error_stats = defaultdict(int)
ip_stats = defaultdict(int)
with open(log_path) as f:
for line in f:
if error_pattern.search(line):
error_code = error_pattern.search(line).group()
error_stats[error_code] += 1
ip = ip_pattern.search(line).group()
ip_stats[ip] += 1
# 生成报告
report = "异常状态码统计:\n"
for code, count in error_stats.items():
report += f"{code}: {count}次\n"
report += "\n异常IP统计(前5):\n"
for ip, count in sorted(ip_stats.items(), key=lambda x: x[1], reverse=True)[:5]:
report += f"{ip}: {count}次\n"
send_email(report)
def send_email(content):
msg = MIMEText(content)
msg['Subject'] = '服务器异常报告'
msg['From'] = 'monitor@example.com'
msg['To'] = 'admin@example.com'
with smtplib.SMTP('smtp.example.com') as server:
server.send_message(msg)
性能优化:处理大日志文件时使用生成器:
python复制def read_large_file(file_path): with open(file_path) as f: while True: data = f.read(8192) if not data: break yield data
5.2 自动化备份脚本
这个脚本将关键目录压缩加密后备份到远程服务器:
python复制import os
import tarfile
import datetime
import paramiko
from cryptography.fernet import Fernet
def secure_backup(source_dir, remote_host, remote_user, remote_path):
# 生成加密密钥
key = Fernet.generate_key()
cipher = Fernet(key)
# 创建压缩包
backup_name = f"backup_{datetime.datetime.now().strftime('%Y%m%d')}.tar.gz"
with tarfile.open(backup_name, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
# 加密压缩包
with open(backup_name, 'rb') as f:
encrypted_data = cipher.encrypt(f.read())
# 传输到远程服务器
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(remote_host, username=remote_user)
sftp = ssh.open_sftp()
with sftp.file(os.path.join(remote_path, backup_name), 'wb') as f:
f.write(encrypted_data)
# 保存密钥到安全位置
with open('backup_key.key', 'wb') as f:
f.write(key)
ssh.close()
6. 数据处理自动化方案
6.1 CSV数据清洗脚本
自动处理脏数据并生成清洗报告:
python复制import pandas as pd
import numpy as np
def clean_csv(input_path, output_path):
df = pd.read_csv(input_path)
report = []
# 处理缺失值
missing_counts = df.isnull().sum()
for col, count in missing_counts.items():
if count > 0:
report.append(f"列 {col} 有 {count} 个缺失值")
if df[col].dtype in ['float64', 'int64']:
df[col].fillna(df[col].median(), inplace=True)
else:
df[col].fillna(df[col].mode()[0], inplace=True)
# 处理异常值
numeric_cols = df.select_dtypes(include=[np.number]).columns
for col in numeric_cols:
q1 = df[col].quantile(0.25)
q3 = df[col].quantile(0.75)
iqr = q3 - q1
lower_bound = q1 - 1.5 * iqr
upper_bound = q3 + 1.5 * iqr
outliers = df[(df[col] < lower_bound) | (df[col] > upper_bound)]
if not outliers.empty:
report.append(f"列 {col} 发现 {len(outliers)} 个异常值")
df[col] = np.where(
(df[col] < lower_bound) | (df[col] > upper_bound),
df[col].median(),
df[col]
)
# 保存结果
df.to_csv(output_path, index=False)
with open('cleaning_report.txt', 'w') as f:
f.write("\n".join(report))
高级技巧:使用
pd.NA处理现代缺失值,比np.nan更精确:python复制df['column'] = df['column'].replace('', pd.NA)
6.2 数据库自动同步工具
这个脚本实现两个数据库表的增量同步:
python复制import sqlalchemy as db
from datetime import datetime
def sync_tables(source_uri, target_uri, table_name, key_column):
# 连接数据库
source_engine = db.create_engine(source_uri)
target_engine = db.create_engine(target_uri)
# 获取源表最后更新时间
with source_engine.connect() as conn:
query = f"SELECT MAX(updated_at) FROM {table_name}"
last_update = conn.execute(query).scalar()
# 获取目标表最后记录ID
with target_engine.connect() as conn:
query = f"SELECT MAX({key_column}) FROM {table_name}"
last_id = conn.execute(query).scalar() or 0
# 同步新增或修改的记录
with source_engine.connect() as conn:
query = f"""
SELECT * FROM {table_name}
WHERE (updated_at > :last_update OR {key_column} > :last_id)
"""
new_data = conn.execute(query, {'last_update': last_update, 'last_id': last_id})
if new_data.rowcount > 0:
with target_engine.connect() as target_conn:
for row in new_data:
# 构建UPSERT语句
columns = row.keys()
values = [row[c] for c in columns]
set_clause = ", ".join([f"{c}=excluded.{c}" for c in columns if c != key_column])
insert_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({', '.join(['%s']*len(values))})
ON CONFLICT ({key_column}) DO UPDATE SET {set_clause}
"""
target_conn.execute(insert_sql, values)
print(f"同步完成,新增/更新 {new_data.rowcount} 条记录")
7. 网络自动化实用工具
7.1 网站可用性监控
定时检查网站状态并发送告警:
python复制import requests
import schedule
import time
def check_website(url, timeout=5):
try:
response = requests.get(url, timeout=timeout)
if response.status_code == 200:
print(f"{url} 访问正常")
return True
else:
print(f"{url} 返回异常状态码: {response.status_code}")
send_alert(f"{url} 异常状态码: {response.status_code}")
return False
except Exception as e:
print(f"{url} 访问失败: {str(e)}")
send_alert(f"{url} 访问失败: {str(e)}")
return False
def send_alert(message):
# 实际项目中接入短信/邮件告警
print(f"发送告警: {message}")
# 每5分钟检查一次
schedule.every(5).minutes.do(check_website, url="https://example.com")
while True:
schedule.run_pending()
time.sleep(1)
生产级建议:使用
requests.Session保持连接,添加重试逻辑:python复制from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry session = requests.Session() retries = Retry(total=3, backoff_factor=1) session.mount('http://', HTTPAdapter(max_retries=retries))
7.2 自动化网络爬虫
这个脚本可以定时抓取网页内容并检测变化:
python复制import requests
from bs4 import BeautifulSoup
import hashlib
import time
class WebMonitor:
def __init__(self, url):
self.url = url
self.last_hash = None
def fetch_content(self):
response = requests.get(self.url)
soup = BeautifulSoup(response.text, 'html.parser')
main_content = soup.find('main') or soup.body
return str(main_content)
def check_update(self):
current_content = self.fetch_content()
current_hash = hashlib.md5(current_content.encode()).hexdigest()
if self.last_hash and current_hash != self.last_hash:
print("检测到内容更新!")
# 这里可以添加通知逻辑
return True
self.last_hash = current_hash
return False
# 使用示例
monitor = WebMonitor("https://example.com/news")
while True:
monitor.check_update()
time.sleep(3600) # 每小时检查一次
8. 图像处理自动化脚本
8.1 批量图片压缩工具
用Pillow自动压缩图片大小,保持画质:
python复制from PIL import Image
import os
def compress_images(input_dir, output_dir, quality=85):
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
with Image.open(input_path) as img:
if img.mode in ('RGBA', 'P'):
img = img.convert('RGB')
img.save(
output_path,
quality=quality,
optimize=True,
progressive=True
)
专业建议:针对Web优化使用mozjpeg编码:
python复制img.save(output_path, format='JPEG', quality=quality, subsampling=0, qtables='web_high')
8.2 智能图片水印添加器
自动为图片添加自适应位置的水印:
python复制from PIL import Image, ImageDraw, ImageFont
import os
def add_watermark(input_dir, output_dir, watermark_text):
font = ImageFont.truetype("arial.ttf", 36)
for filename in os.listdir(input_dir):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
img_path = os.path.join(input_dir, filename)
img = Image.open(img_path)
# 根据图片大小计算水印位置
width, height = img.size
draw = ImageDraw.Draw(img)
# 计算文字大小和位置
text_width, text_height = draw.textsize(watermark_text, font)
x = width - text_width - 20
y = height - text_height - 20
# 添加半透明水印
draw.text((x, y), watermark_text, font=font, fill=(255, 255, 255, 128))
output_path = os.path.join(output_dir, filename)
img.save(output_path)
9. 邮件自动化处理方案
9.1 智能邮件分类器
用机器学习自动分类收件箱邮件:
python复制import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
import pickle
import os
class EmailClassifier:
def __init__(self, model_path='email_classifier.pkl'):
self.model_path = model_path
if os.path.exists(model_path):
with open(model_path, 'rb') as f:
self.model = pickle.load(f)
else:
self.model = None
def train(self, emails, labels):
vectorizer = TfidfVectorizer(stop_words='english', max_features=1000)
X = vectorizer.fit_transform(emails)
self.model = MultinomialNB()
self.model.fit(X, labels)
with open(self.model_path, 'wb') as f:
pickle.dump(self.model, f)
self.vectorizer = vectorizer
def predict(self, email):
if not self.model:
raise Exception("模型未训练")
X = self.vectorizer.transform([email])
return self.model.predict(X)[0]
# 使用示例
classifier = EmailClassifier()
# 假设我们有训练数据
emails = ["促销优惠...", "会议通知...", "账单信息..."]
labels = ["promo", "work", "finance"]
classifier.train(emails, labels)
new_email = "双十一特惠活动..."
print(classifier.predict(new_email)) # 输出: promo
9.2 自动邮件回复机器人
基于模板的智能回复系统:
python复制import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import re
class AutoResponder:
def __init__(self, email, password):
self.email = email
self.password = password
self.templates = {
'greeting': "感谢您的来信,我们会在24小时内回复您。",
'order': "您的订单已收到,订单号为: {order_id}",
'complaint': "非常抱歉给您带来不便,我们的客服会尽快联系您。"
}
def analyze_email(self, content):
if re.search(r'订单|购买', content):
return 'order'
elif re.search(r'投诉|不满意', content):
return 'complaint'
else:
return 'greeting'
def send_reply(self, to_email, original_content):
category = self.analyze_email(original_content)
template = self.templates[category]
# 提取订单号
order_id = None
if category == 'order':
match = re.search(r'订单[号|码]?[::]?\s*(\w+)', original_content)
order_id = match.group(1) if match else 'UNKNOWN'
template = template.format(order_id=order_id)
msg = MIMEMultipart()
msg['From'] = self.email
msg['To'] = to_email
msg['Subject'] = "自动回复"
body = f"""
{template}
---
原始邮件内容:
{original_content}
"""
msg.attach(MIMEText(body, 'plain'))
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(self.email, self.password)
server.send_message(msg)
10. 高级自动化技巧
10.1 多任务并行处理器
用concurrent.futures加速批量任务:
python复制from concurrent.futures import ThreadPoolExecutor
import time
def process_item(item):
# 模拟耗时操作
time.sleep(1)
return f"processed_{item}"
def batch_processor(items, max_workers=4):
results = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_item = {executor.submit(process_item, item): item for item in items}
for future in concurrent.futures.as_completed(future_to_item):
item = future_to_item[future]
try:
result = future.result()
results.append(result)
except Exception as e:
print(f"处理 {item} 时出错: {e}")
return results
# 使用示例
items = [f"item_{i}" for i in range(10)]
start = time.time()
results = batch_processor(items)
print(f"处理完成,耗时: {time.time() - start:.2f}秒")
性能提示:CPU密集型任务用
ProcessPoolExecutor,IO密集型用ThreadPoolExecutor
10.2 自动化任务调度系统
用APScheduler构建健壮的任务调度器:
python复制from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
import time
def daily_report():
print("生成每日报告...")
def hourly_check():
print("执行每小时检查...")
scheduler = BackgroundScheduler()
# 添加每天9点的任务
scheduler.add_job(
daily_report,
trigger=CronTrigger(hour=9, minute=0),
id='daily_report'
)
# 添加每小时的任务
scheduler.add_job(
hourly_check,
trigger='interval',
hours=1,
id='hourly_check'
)
scheduler.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
scheduler.shutdown()
生产环境建议:使用数据库存储任务状态:
python复制from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore jobstores = { 'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite') } scheduler = BackgroundScheduler(jobstores=jobstores)
这些脚本都是我多年工作中积累的实用工具,每个都经过实际项目验证。建议从最符合你需求的脚本开始尝试,逐步修改适应你的具体场景。自动化不是要一次性解决所有问题,而是从最耗时的重复工作开始,逐步解放你的生产力。
