15个超级实用的Python脚本,必须收藏!
15个非常实用的Python脚本,覆盖了数据处理、自动化操作、文件操作、爬虫、数据可视化等方面的内容.每个脚本都包含简明的示例代码,帮助你快速上手PYTHON。
1. 文件重命名
重命名文件夹中的所有文件,可以用于批量整理图片或文档.
import os
def rename_files(folder_path, new_prefix):
for count, filename in enumerate(os.listdir(folder_path)):
new_name = f"{new_prefix}_{count + 1}{os.path.splitext(filename)[1]}"
os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_name))
print("重命名完成")
# 使用示例
rename_files("/path/to/your/folder", "new_name")
2. 文件格式转换:文本转PDF
将文本文件转为PDF格式,适用于制作报告或文档归档.
python
from fpdf import FPDF
def text_to_pdf(text_file, pdf_file):
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
with open(text_file, 'r') as file:
for line in file:
pdf.cell(200, 10, txt=line, ln=True)
pdf.output(pdf_file)
print(f"{pdf_file} 已生成")
# 使用示例
text_to_pdf("sample.txt", "output.pdf")
3. 批量下载图片
从一个URL列表批量下载图片.
import requests
import os
def download_images(url_list, save_folder):
os.makedirs(save_folder, exist_ok=True)
for idx, url in enumerate(url_list):
img_data = requests.get(url).content
with open(os.path.join(save_folder, f'image_{idx + 1}.jpg'), 'wb') as img_file:
img_file.write(img_data)
print("图片下载完成")
# 使用示例
url_list = ["https://example.com/image1.jpg", "https://example.com/image2.jpg"]
download_images(url_list, "images_folder")
4. 数据清洗:去除重复项
从CSV文件中读取数据并去重,常用于数据预处理.
import pandas as pd
def remove_duplicates(csv_file, output_file):
df = pd.read_csv(csv_file)
df.drop_duplicates(inplace=True)
df.to_csv(output_file, index=False)
print(f"{output_file} 已生成,无重复项")
# 使用示例
remove_duplicates("input.csv", "output_no_duplicates.csv")
5. 计算文件夹大小
递归计算文件夹大小,适用于磁盘空间管理.
import os
def get_folder_size(folder_path):
total_size = 0
for dirpath, dirnames, filenames in os.walk(folder_path):
for file in filenames:
file_path = os.path.join(dirpath, file)
total_size += os.path.getsize(file_path)
return total_size / (1024 * 1024) # 返回大小为MB
# 使用示例
print(f"文件夹大小: {get_folder_size('/path/to/folder')} MB")
6. 简单的Web爬虫
抓取网页内容并保存到文本文件.
import requests
def simple_scraper(url, output_file):
response = requests.get(url)
with open(output_file, 'w', encoding='utf-8') as file:
file.write(response.text)
print(f"{output_file} 已保存")
# 使用示例
simple_scraper("https://example.com", "web_content.txt")
7. 数据可视化:柱状图
读取CSV数据并生成柱状图,适合于简单的数据分析.
import pandas as pd
import matplotlib.pyplot as plt
def plot_bar_chart(csv_file, x_column, y_column):
df = pd.read_csv(csv_file)
df.plot(kind='bar', x=x_column, y=y_column)
plt.show()
# 使用示例
plot_bar_chart("data.csv", "Category", "Values")
8. 生成随机密码
生成一个复杂的随机密码,提高账户安全性.
import random
import string
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for _ in range(length))
return password
# 使用示例
print(generate_password())
9. 二维码生成
生成文本信息的二维码,适用于链接或文字信息的分享.
import qrcode
def generate_qr_code(text, output_file):
img = qrcode.make(text)
img.save(output_file)
print(f"{output_file} 已生成")
# 使用示例
generate_qr_code("https://example.com", "qr_code.png")
10. PDF合并
合并多个PDF文件,适合文件整理.
from PyPDF2 import PdfMerger
def merge_pdfs(pdf_list, output_file):
merger = PdfMerger()
for pdf in pdf_list:
merger.append(pdf)
merger.write(output_file)
merger.close()
print(f"{output_file} 已合并生成")
# 使用示例
merge_pdfs(["file1.pdf", "file2.pdf"], "merged.pdf")
11. 发送邮件
自动发送带附件的邮件,用于通知或分享文件.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
def send_email_with_attachment(to_address, subject, body, file_path):
from_address = "your_email@example.com"
password = "your_password"
message = MIMEMultipart()
message['From'] = from_address
message['To'] = to_address
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))
with open(file_path, "rb") as attachment:
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {file_path}")
message.attach(part)
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(from_address, password)
server.sendmail(from_address, to_address, message.as_string())
print("邮件发送成功")
# 使用示例
send_email_with_attachment("recipient@example.com", "Test Subject", "This is the body", "path/to/file.txt")
12. 定时任务
创建一个定时任务,每隔指定时间执行一次指定函数.
import time
def scheduled_task(interval, task):
while True:
task()
time.sleep(interval)
# 示例任务函数
def my_task():
print("任务执行中...")
# 每5秒执行一次
scheduled_task(5, my_task)
13. Excel文件读取和写入
读取Excel文件并添加数据.
import pandas as pd
def read_and_append_excel(file_path, new_data, output_file):
df = pd.read_excel(file_path)
new_df = pd.DataFrame(new_data)
df = pd.concat([df, new_df], ignore_index=True)
df.to_excel(output_file, index=False)
print(f"{output_file} 已保存")
# 使用示例
new_data = {'Name': ['John'], 'Age': [28]}
read_and_append_excel("data.xlsx", new_data, "updated_data.xlsx")
14. 简单API请求
向API发送请求并获取数据.
import requests
def fetch_api_data(url):
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print("请求失败")
return None
# 使用示例
api_data = fetch_api_data("https://api.example.com/data")
print(api_data)
15. 计算两个日期的差异
计算两个日期之间的天数差,常用于日程计算.
from datetime import datetime
def days_between_dates(date1, date2):
date_format = "%Y-%m-%d"
delta = datetime.strptime(date2, date_format) - datetime.strptime(date1, date_format)
return delta.days
# 使用示例
print(days_between_dates("2023-01-01", "2023-12-31"))
THE END