15 Python Automation Scripts for Common Tasks
This article presents fifteen practical Python scripts that automate everyday tasks such as folder backup, email notifications, web content downloading, password management, image downloading, Excel processing, network speed testing, social media posting, scheduled shutdown, CPU monitoring, HTML scraping, batch file renaming, duplicate removal, text‑to‑speech conversion, and PDF merging.
1. Automatic Folder Backup
import shutil
import datetime
def backup_files(source, destination):
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
backup_folder = f"{destination}/{timestamp}"
shutil.copytree(source, backup_folder)
print(f"已备份至: {backup_folder}")
# 示例用法
source_folder = "/path/to/source"
destination_folder = "/path/to/destination"
backup_files(source_folder, destination_folder)2. Send Email Reminder
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = "[email protected]"
msg['To'] = to
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login("[email protected]", "your_password")
server.send_message(msg)
# 示例用法
send_email("任务提醒", "别忘了今天的会议!", "[email protected]")3. Automatic Web Content Downloader
import requests
def download_web_content(url, filename):
response = requests.get(url)
if response.status_code == 200:
with open(filename, 'w', encoding='utf-8') as file:
file.write(response.text)
print(f"内容已保存至 {filename}")
else:
print("请求失败,状态码:", response.status_code)
# 示例用法
url = "https://news.example.com/article"
download_web_content(url, "article.txt")4. Simple Password Manager
import json
def save_credentials(site, username, password, filename="passwords.json"):
data = {site: {"username": username, "password": password}}
with open(filename, 'a+') as file:
file.seek(0)
try:
existing_data = json.load(file)
existing_data.update(data)
file.seek(0)
json.dump(existing_data, file, indent=4)
except json.JSONDecodeError:
json.dump(data, file, indent=4)
def retrieve_credentials(site, filename="passwords.json"):
with open(filename, 'r') as file:
data = json.load(file)
return data.get(site, None)
# 示例用法
save_credentials("example.com", "user1", "pass123")
print(retrieve_credentials("example.com"))5. Image Downloader
import os
import requests
def download_images(urls, folder="images"):
if not os.path.exists(folder):
os.makedirs(folder)
for url in urls:
response = requests.get(url)
if response.status_code == 200:
filename = os.path.join(folder, url.split('/')[-1])
with open(filename, 'wb') as f:
f.write(response.content)6. Automated Excel Data Processing
import pandas as pd
def process_excel(file_path):
# 读取Excel文件
df = pd.read_excel(file_path)
# 数据清洗示例:去除空值行
df.dropna(inplace=True)
# 将处理后的数据保存回Excel
output_file_path = file_path.replace('.xlsx', '_processed.xlsx')
df.to_excel(output_file_path, index=False)
print(f"已处理并保存至: {output_file_path}")
# 示例用法
process_excel("/path/to/excel/file.xlsx")7. Network Speed Test
First install the speedtest-cli library:
pip install speedtest-cliThen use the following script:
import speedtest
def test_network_speed():
st = speedtest.Speedtest()
download_speed = st.download() / 10**6 # Mbps
upload_speed = st.upload() / 10**6 # Mbps
print(f"下载速度: {download_speed:.2f} Mbps")
print(f"上传速度: {upload_speed:.2f} Mbps")
# 示例用法
test_network_speed()8. Automated Social Media Post Publishing
Example for Twitter using Tweepy:
pip install tweepy import tweepy
API_KEY = 'your_api_key'
API_SECRET_KEY = 'your_api_secret_key'
ACCESS_TOKEN = 'your_access_token'
ACCESS_TOKEN_SECRET = 'your_access_token_secret'
def post_tweet(tweet_text):
# 设置认证信息
auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
# 创建API对象
api = tweepy.API(auth)
try:
# 发布推文
api.update_status(tweet_text)
print("推文已成功发布!")
except Exception as e:
print(f"发布推文时出错: {e}")
# 示例用法
post_tweet("这是通过Python自动发布的测试推文!")9. Scheduled Computer Shutdown (Windows)
import os
import time
def schedule_shutdown(minutes):
seconds = minutes * 60
time.sleep(seconds)
os.system("shutdown -s -t 0") # Windows command
# 示例用法
schedule_shutdown(30) # 计划30分钟后关机10. CPU Usage Monitoring
import psutil
def monitor_cpu_usage(interval=1):
while True:
cpu_percent = psutil.cpu_percent(interval=interval)
print(f"当前CPU使用率: {cpu_percent}%")
# 示例用法
monitor_cpu_usage()11. HTML Parsing and Extraction
from bs4 import BeautifulSoup
import requests
def scrape_data(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 提取所需数据...12. Batch Rename Files
import os
def batch_rename_files(directory, prefix):
for idx, filename in enumerate(os.listdir(directory)):
ext = os.path.splitext(filename)[1]
new_name = f"{prefix}_{idx+1}{ext}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
print(f"重命名为: {new_name}")
# 示例用法
batch_rename_files("/path/to/directory", "new_filename")13. Remove Duplicate Files
This script calculates MD5 hashes to identify and delete duplicate files.
import os
import hashlib
def calculate_md5(file_path):
"""计算给定文件的MD5哈希值"""
hash_md5 = hashlib.md5()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
def remove_duplicate_files(directory):
"""在指定目录中查找并删除重复文件"""
file_hashes = {}
duplicates = []
for root, _, files in os.walk(directory):
for filename in files:
file_path = os.path.join(root, filename)
file_hash = calculate_md5(file_path)
if file_hash not in file_hashes:
file_hashes[file_hash] = file_path
else:
duplicates.append(file_path)
# 删除重复文件
for duplicate in duplicates:
os.remove(duplicate)
print(f"已删除重复文件: {duplicate}")
# 示例用法
remove_duplicate_files("/path/to/your/directory")14. Create Audiobook (Text‑to‑Speech)
import pyttsx3
def text_to_speech(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# 示例用法
text_to_speech("这是一个测试句子。")15. PDF Merger
from PyPDF4 import PdfFileMerger
def merge_pdfs(pdf_list, output_path):
merger = PdfFileMerger()
for pdf in pdf_list:
merger.append(pdf)
merger.write(output_path)
merger.close()
print(f"合并完成并保存至: {output_path}")
# 示例用法
merge_pdfs(["file1.pdf", "file2.pdf"], "merged_output.pdf")Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.