返回
Python 必备的 10 大经典脚本,帮你提升工作效率!
后端
2022-12-27 12:11:03
10 个实用的 Python 脚本,提高您的工作效率
Python 以其易于使用和功能强大的特性而备受推崇,尤其适合编写脚本。本文将为您介绍 10 个实用的 Python 脚本,帮助您轻松解决日常工作中的难题,提高效率。
1. 下载图像
此脚本可用于下载指定 URL 中的图像,并将其保存在您指定的路径中。
import requests
url = input("请输入图像 URL:")
path = input("请输入保存路径:")
response = requests.get(url)
with open(path, "wb") as f:
f.write(response.content)
print("图像已下载")
2. 分类文件
此脚本可按指定的文件扩展名对目录中的文件进行分类。
import os
path = input("请输入文件所在目录:")
extensions = input("请输入要分类的文件扩展名(以逗号分隔):").split(",")
for extension in extensions:
os.makedirs(os.path.join(path, extension), exist_ok=True)
for file in os.listdir(path):
if os.path.isfile(os.path.join(path, file)):
extension = os.path.splitext(file)[1].lower()
if extension in extensions:
os.rename(os.path.join(path, file), os.path.join(path, extension, file))
print("文件已分类")
3. 发送电子邮件
此脚本可用来发送电子邮件,支持 SMTP 协议。
import smtplib
smtp_server = input("请输入 SMTP 服务器地址:")
smtp_port = int(input("请输入 SMTP 服务器端口:"))
sender_email = input("请输入发件人邮箱地址:")
sender_password = input("请输入发件人邮箱密码:")
recipients = input("请输入收件人邮箱地址(以逗号分隔):").split(",")
subject = input("请输入邮件主题:")
body = input("请输入邮件内容:")
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
for recipient in recipients:
server.sendmail(sender_email, recipient, f"Subject: {subject}\n\n{body}")
server.quit()
print("电子邮件已发送")
4. 转换 JSON 数据
此脚本可将 JSON 数据转换为 CSV 格式。
import json
input_file = input("请输入 JSON 文件路径:")
output_file = input("请输入 CSV 文件路径:")
with open(input_file, "r") as f:
data = json.load(f)
with open(output_file, "w") as f:
f.write(",".join(data[0].keys()) + "\n")
for row in data:
f.write(",".join(row.values()) + "\n")
print("JSON 数据已转换为 CSV 文件")
5. 提取链接
此脚本可从指定的网站中提取所有链接。
import requests
from bs4 import BeautifulSoup
url = input("请输入网站 URL:")
output_file = input("请输入保存链接的文件路径:")
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
links = []
for link in soup.find_all("a"):
links.append(link.get("href"))
with open(output_file, "w") as f:
f.write("\n".join(links))
print("链接已提取")
6. 搜索文件
此脚本可用于在指定目录及其子目录中搜索包含特定字符串的文件。
import os
path = input("请输入要搜索文件的目录:")
string = input("请输入要搜索的字符串:")
for root, dirs, files in os.walk(path):
for file in files:
with open(os.path.join(root, file), "r") as f:
if string in f.read():
print(os.path.join(root, file))
print("文件已找到")
7. 添加水印
此脚本可将指定的图像作为水印添加到另一张图像上。
import cv2
image_path = input("请输入图像路径:")
watermark_path = input("请输入水印图像路径:")
output_path = input("请输入保存水印图像的路径:")
image = cv2.imread(image_path)
watermark = cv2.imread(watermark_path)
alpha = float(input("请输入水印透明度(0.0-1.0):"))
result = cv2.addWeighted(image, 1, watermark, alpha, 0)
cv2.imwrite(output_path, result)
print("水印已添加")
8. 压缩文件
此脚本可用于将指定的文件压缩为 ZIP 文件。
import zipfile
input_file = input("请输入要压缩的文件路径:")
output_file = input("请输入压缩文件路径:")
with zipfile.ZipFile(output_file, "w") as zip_file:
zip_file.write(input_file)
print("文件已压缩")
9. 加密文件
此脚本可使用 AES 加密算法对文件进行加密。
from Crypto.Cipher import AES
key = input("请输入加密密钥:")
input_file = input("请输入要加密的文件路径:")
output_file = input("请输入加密文件路径:")
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC)
with open(input_file, "rb") as f:
data = f.read()
encrypted_data = cipher.encrypt(data)
with open(output_file, "wb") as f:
f.write(encrypted_data)
print("文件已加密")
10. 解密文件
此脚本可使用 AES 解密算法对加密的文件进行解密。
from Crypto.Cipher import AES
key = input("请输入解密密钥:")
input_file = input("请输入要解密的文件路径:")
output_file = input("请输入解密文件路径:")
cipher = AES.new(key.encode("utf-8"), AES.MODE_CBC)
with open(input_file, "rb") as f:
data = f.read()
decrypted_data = cipher.decrypt(data)
with open(output_file, "wb") as f:
f.write(decrypted_data)
print("文件已解密")
常见问题解答
-
Q:这些脚本需要安装哪些依赖项?
- A:这些脚本需要
requests
、BeautifulSoup
、os
、cv2
、zipfile
、PyCrypto
等库。
- A:这些脚本需要
-
Q:如何自定义脚本以满足我的特定需求?
- A:您可以修改脚本中可配置的变量和参数,以适应您的具体情况。
-
Q:这些脚本适用于所有操作系统吗?
- A:大多数脚本在 Windows、Mac 和 Linux 系统上都应该可以正常工作。但是,某些脚本可能需要进行轻微的修改才能与特定操作系统兼容。
-
Q:我可以在哪里找到更多这样的 Python 脚本?
- A:您可以查看 Python 包索引 (PyPI) 或 GitHub 等在线资源来查找其他 Python 脚本。
-
Q:是否可以在商业环境中使用这些脚本?
- A:是的,这些脚本通常可在商业环境中使用,但请注意某些脚本可能需要根据所使用的特定库的许可证条款进行归属。