Python因其简洁的语法和丰富的库生态系统,成为自动化任务的理想选择。无论是处理文件、网络请求、数据分析还是日常工作流程,Python都能提供高效的解决方案。本文将分享一些实用的Python自动化技巧,帮助你提高工作效率。
1. 文件操作自动化
处理文件是最常见的自动化任务之一:
批量重命名文件
import os
def batch_rename(directory, old_ext, new_ext):
for filename in os.listdir(directory):
if filename.endswith(old_ext):
new_name = filename.replace(old_ext, new_ext)
os.rename(
os.path.join(directory, filename),
os.path.join(directory, new_name)
)
# 使用示例:将目录中所有.txt文件重命名为.md文件
batch_rename("./documents", ".txt", ".md")
监控文件夹变化
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
if not event.is_directory:
print(f"文件已修改: {event.src_path}")
# 在这里添加你的处理逻辑
observer = Observer()
observer.schedule(MyHandler(), path="./watch_folder", recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
2. 网络请求和API交互
Python的requests库使网络请求变得简单:
定时获取API数据
import requests
import schedule
import time
import json
def fetch_data():
response = requests.get("https://api.example.com/data")
if response.status_code == 200:
data = response.json()
# 保存数据到文件
with open(f"data_{time.strftime('%Y%m%d_%H%M%S')}.json", "w") as f:
json.dump(data, f, indent=4)
print("数据已保存")
else:
print(f"请求失败: {response.status_code}")
# 每小时执行一次
schedule.every().hour.do(fetch_data)
while True:
schedule.run_pending()
time.sleep(60)
自动下载文件
import requests
def download_file(url, local_filename=None):
if local_filename is None:
local_filename = url.split('/')[-1]
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
return local_filename
# 使用示例
download_file("https://example.com/files/document.pdf")
3. 数据处理自动化
Python的pandas库是数据处理的强大工具:
自动合并Excel文件
import pandas as pd
import glob
import os
def merge_excel_files(directory, output_file):
all_files = glob.glob(os.path.join(directory, "*.xlsx"))
# 创建一个空的DataFrame来存储所有数据
all_data = pd.DataFrame()
# 遍历所有Excel文件并合并数据
for file in all_files:
df = pd.read_excel(file)
df['Source'] = os.path.basename(file) # 添加来源文件名
all_data = pd.concat([all_data, df], ignore_index=True)
# 保存合并后的数据
all_data.to_excel(output_file, index=False)
print(f"已合并{len(all_files)}个文件到{output_file}")
# 使用示例
merge_excel_files("./excel_files", "merged_data.xlsx")
定期生成报告
import pandas as pd
import matplotlib.pyplot as plt
import schedule
import time
from datetime import datetime
def generate_report():
# 读取数据
df = pd.read_csv("sales_data.csv")
# 数据分析
monthly_sales = df.groupby('month')['amount'].sum()
# 创建图表
plt.figure(figsize=(10, 6))
monthly_sales.plot(kind='bar')
plt.title('Monthly Sales Report')
plt.xlabel('Month')
plt.ylabel('Sales Amount')
# 保存图表
report_date = datetime.now().strftime('%Y%m%d')
plt.savefig(f"sales_report_{report_date}.png")
# 生成Excel报告
writer = pd.ExcelWriter(f"sales_report_{report_date}.xlsx")
monthly_sales.to_excel(writer, sheet_name='Monthly Sales')
df.pivot_table(values='amount', index='product', columns='month', aggfunc='sum').to_excel(writer, sheet_name='Product Performance')
writer.save()
print(f"报告已生成: sales_report_{report_date}.xlsx")
# 每周一早上9点生成报告
schedule.every().monday.at("09:00").do(generate_report)
while True:
schedule.run_pending()
time.sleep(60)
4. 自动化办公任务
Python可以帮助自动化许多日常办公任务:
自动发送邮件
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import os
def send_email(subject, body, to_email, attachment_path=None):
# 邮件服务器配置
smtp_server = "smtp.example.com"
smtp_port = 587
smtp_user = "your_email@example.com"
smtp_password = "your_password" # 建议使用环境变量存储密码
# 创建邮件
msg = MIMEMultipart()
msg['From'] = smtp_user
msg['To'] = to_email
msg['Subject'] = subject
# 添加邮件正文
msg.attach(MIMEText(body, 'html'))
# 添加附件
if attachment_path and os.path.exists(attachment_path):
with open(attachment_path, 'rb') as file:
attachment = MIMEApplication(file.read())
attachment.add_header(
'Content-Disposition',
'attachment',
filename=os.path.basename(attachment_path)
)
msg.attach(attachment)
# 发送邮件
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_user, smtp_password)
server.send_message(msg)
print(f"邮件已发送至{to_email}")
# 使用示例
send_email(
"每周报告",
"每周销售报告
请查看附件。
",
"recipient@example.com",
"sales_report_20240110.xlsx"
)
自动填写PDF表单
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import NameObject, createStringObject
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
def fill_pdf_form(input_pdf_path, output_pdf_path, data_dict):
# 读取原始PDF
reader = PdfReader(input_pdf_path)
writer = PdfWriter()
# 获取第一页
page = reader.pages[0]
writer.add_page(page)
# 更新表单字段
writer.update_page_form_field_values(
writer.pages[0], data_dict
)
# 保存填写后的PDF
with open(output_pdf_path, "wb") as output_stream:
writer.write(output_stream)
print(f"PDF表单已填写并保存为{output_pdf_path}")
# 使用示例
form_data = {
"name": "张三",
"email": "zhangsan@example.com",
"phone": "123-456-7890",
"address": "北京市海淀区"
}
fill_pdf_form("form_template.pdf", "filled_form.pdf", form_data)
5. 网页自动化
使用Selenium可以自动化浏览器操作:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
def automate_web_task():
# 设置WebDriver
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
try:
# 打开网页
driver.get("https://example.com/login")
# 登录
driver.find_element(By.ID, "username").send_keys("your_username")
driver.find_element(By.ID, "password").send_keys("your_password")
driver.find_element(By.ID, "login-button").click()
# 等待页面加载
time.sleep(2)
# 执行任务(例如下载报告)
driver.find_element(By.LINK_TEXT, "下载报告").click()
# 等待下载完成
time.sleep(5)
print("任务完成")
finally:
# 关闭浏览器
driver.quit()
# 执行自动化任务
automate_web_task()
结语
Python自动化可以为你节省大量时间,让你专注于更有创造性的工作。上述示例只是Python自动化能力的冰山一角。根据你的具体需求,你可以组合这些技术或探索更多Python库来创建自己的自动化解决方案。
记住,好的自动化脚本应该是可靠的、可维护的,并且能够处理异常情况。随着你自动化技能的提升,你可以构建更复杂、更强大的工作流程,进一步提高工作效率。