重复性任务总是耗时且无聊,例如裁剪 100 张照片、Fetch API 请求或纠正拼写和语法。为什么不自动化它们呢?以下是 10 个 Python 自动化脚本。
01. 图片优化器
使用这个自动化脚本,可以帮助把图像处理的更好,你可以像在 Photoshop 中一样编辑它们。
该脚本使用流行的 Pillow 模块。
import PIL
im = PIL.Image.open('Image1.jpg')
im = im.crop((34, 23, 100, 100))
im = PIL.Image.open('Image1.jpg')
im = im.resize((50, 50))
im = PIL.Image.open('Image1.jpg')
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
im = PIL.Image.open('Image1.jpg')
im = im.rotate(360)
im = PIL.Image.open('Image1.jpg')
im.save('Image1.jpg', optimize=True, quality=90)
im = PIL.Image.open('Image1.jpg')
im = im.filter(PIL.ImageFilter.BLUR)
im = PIL.Image.open('Image1.jpg')
im = im.filter(PIL.ImageFilter.SHARPEN)
im = PIL.Image.open('Image1.jpg')
im = PIL.ImageEnhance.Brightness(im).enhance(1.5)
im = PIL.Image.open('Image1.jpg')
im = PIL.ImageEnhance.Contrast(im).enhance(1.5)
im = PIL.Image.open('Image1.jpg')
im = PIL.ImageOps.grayscale(im)
im = PIL.ImageOps.invert(im)
im = PIL.ImageOps.posterize(im, 4)
im.save('Image1.jpg')
02. 视频优化器
通过使用以下自动化脚本,你不仅可以使用 Python 来优化视频,还可以使用它来优化图像。该脚本使用 Moviepy 模块,允许你修剪、添加音频、设置视频速度、添加 VFX 等等。
import moviepy.editor as pyedit
video = pyedit.VideoFileClip('vid.mp4')
vid1 = video.subclip(0, 10)
vid2 = video.subclip(20, 40)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
final_vid = final_vid.speedx(2)
aud = pyedit.AudioFileClip('bg.mp3')
final_vid = final_vid.set_audio(aud)
final_vid = final_vid.fx(pyedit.vfx.time_mirror)
vid1 = pyedit.VideoFileClip('vid1.mp4')
vid2 = pyedit.VideoFileClip('vid2.mp4')
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
vid1 = final_vid.fx(pyedit.vfx.mirror_x)
vid2 = final_vid.fx(pyedit.vfx.invert_colors)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
img1 = pyedit.ImageClip('img1.jpg')
img2 = pyedit.ImageClip('img2.jpg')
final_vid = pyedit.concatenate_videoclips([img1, img2])
final_vid.write_videofile('final.mp4')
03. PDF 转图片
这个小型自动化脚本可以方便地获取整个 PDF 页面并将它们转换为图像。该脚本使用流行的 PyMuPDF 模块,该模块以其 PDF 文本提取而闻名。
import fitz
def pdf_to_images(pdf_file):
doc = fitz.open(pdf_file)
for p in doc:
pix = p.get_pixmap()
output = f'page{p.number}.png'
pix.writePNG(output)
pdf_to_images('test.pdf')
04. 获取 API 数据
需要从数据库中获取 API 数据或需要向服务器发送 API 请求。那么这个自动化脚本对你来说是一个方便的工具。使用 Urllib3 模块,可让你获取和发布 API 请求。
import urllib3
url = 'https://api.github.com/users/psf/repos'
http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)
print(response.data)
url = 'https://httpbin.org/post'
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print(response.status)
05. 电池指示灯
这个方便的脚本可以让你设置你想要得到通知的电池百分比,该脚本使用 Plyer 进行通知,使用 Psutil 获取当前的电池百分比。
from plyer import notification
import psutil
from time import sleep
while True:
battery = psutil.sensors_battery()
life = battery.percent
if life < 50:
notification.notify(
title='Battery Low',
message='Please connect to power source',
timeout=10
)
sleep(60)
06. 语法固定器
厌倦了校对你的长文章或文本,然后,你可以试试这个自动化脚本,它将扫描你的文本并纠正语法错误,这个很棒的脚本使用 HappyTransformer 模块,这是一个机器学习模块,经过训练可以修复文本中的语法错误。
from happytransformer import HappyTextToText as HappyTTT
from happytransformer import TTSettings
def Grammar_Fixer(Text):
Grammer = HappyTTT('T5', 'prithivida/grammar_error_correcter_v1')
config = TTSettings(do_sample=True, top_k=10, max_length=100)
corrected = Grammer.generate_text(Text, args=config)
print('Corrected Text:', corrected.text)
Text = 'This is simple text we know this'
Grammar_Fixer(Text)
07. 拼写修正
这个很棒的脚本将帮助你纠正你的文本单词拼写错误。你可以在下面找到脚本,将告诉你如何修复句子中的单个单词或多个单词。
from textblob import *
def fix_paragraph_words(paragraph):
sentence = TextBlob(paragraph)
correction = sentence.correct()
print(correction)
def fix_word_spell(word):
word = Word(word)
correction = word.correct()
print(correction)
fix_paragraph_words('This is sample text!!')
fix_word_spell('maangoo')
08. 互联网下载器
你们可能使用下载软件从 Internet 下载照片或视频,但现在你可以使用 Python IDM 模块创建自己的下载器。
import internetdownloadmanager as idm
def Downloader(url, output):
pydownloader = idm.Downloader(worker=20,
part_size=1024*1024*10,
resumable=True,)
pydownloader.download(url, output)
Downloader('Link url', 'image.jpg')
Downloader('Link url', 'video.mp4')
09. 获取世界新闻
使用此自动化脚本让你随时了解每日世界新闻,你可以使用任何语言从任何国家/地区获取新闻。这个 API 让你每天免费获取 50 篇新闻文章。
import requests
ApiKey = 'YOUR_API_KEY'
url = f'https://api.worldnewsapi.com/search-news?text=hurricane&api-key={ApiKey}'
headers = {
'Accept': 'application/json'
}
response = requests.get(url, headers=headers)
print('News:', response.json())
10. PySide6 GUI
这个自动化脚本将帮助你使用 PySide6 GUI 模块创建你的 GUI 应用程序。你可以在下面找到开始开发体面的现代应用程序所需的每种方法。
from PySide6.QtWidgets import *
from PySide6.QtGui import *
import sys
app = QApplication(sys.argv)
window = QWidget()
window.resize(500, 500)
window.setWindowTitle('PySide6 Window')
button = QPushButton('Click Me', window)
button.move(200, 200)
label = QLabel('Hello Medium', window)
label.move(200, 150)
input_box = QLineEdit(window)
input_box.move(200, 250)
print(input_box.text())
radio_button = QRadioButton('Radio Button', window)
radio_button.move(200, 300)
checkbox = QCheckBox('Checkbox', window)
checkbox.move(200, 350)
slider = QSlider(window)
slider.move(200, 400)
progress_bar = QProgressBar(window)
progress_bar.move(200, 450)
image = QLabel(window)
image.setPixmap(QPixmap('image.png'))
msg = QMessageBox(window)
msg.setText('Message Box')
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
window.show()
sys.exit(app.exec())