解放双手,10个解决重复任务的Python自动化脚本

本文将介绍10个厉害的Python自动化脚本。

微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩

图片

重复性的任务总是耗时且枯燥。想象一下一张一张地剪切100张照片,或者执行诸如获取API、纠正拼写和语法等任务,这些都需要大量时间。为什么不将它们自动化呢?在今天的文章中,将分享10个解决重复任务的Python自动化脚本。

因此,请将本文加入收藏,以备将来参考。现在,跟随本文开始吧。

图片

01、图像优化器

这个优秀的自动化脚本可以帮助你更好地处理图像,你可以像在Photoshop中一样进行编辑。

该脚本使用了流行的Pillow模块。

# 图像优化
# pip install 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)
im = im.enhance(1.5)
# 设置对比度
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Contrast(im)
im = 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模块,允许进行裁剪、添加音频、设置视频速度、添加视觉效果等操作。

# 视频优化器
# pip install moviepy
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文本提取功能而闻名。

# 将PDF转换为图像
# pip install PyMuPDF
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请求的获取和发送。

# pip install urllib3
import urllib3
# 获取API数据
url = "https://api.github.com/users/psf/repos"
http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)
print(response.data)
# 发布API数据
url = "https://httpbin.org/post"
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print(response.status)

05、电池指示灯

这个方便的脚本允许设置希望收到通知的电池百分比。该脚本使用Pyler来进行通知,并使用Psutil来获取当前电池电量百分比。

# 电池通知器
# pip instal plyer
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、语法修正器

厌倦了校对长篇文章或文本吗?那么,你可以尝试这个自动化脚本,它可以扫描你的文本并纠正语法错误。这个优秀的脚本使用了Happtransformer模块,这是一个经过训练的机器学习模块,用于修正文本中的语法错误。

# 语法修正器
# pip install happytransformer
from happytransformer import HappyTextToText as HappyTTT
from happytransformer import TTSettings
def Grammer_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 smple tet we how know this"
Grammer_Fixer(Text)

07、拼写纠正

这个很棒的脚本可以帮助你纠正文本中的拼写错误。可以在下面找到这个脚本,它将告诉你如何修正句子中的单个或多个单词。

# 拼写修正器
# pip install textblob
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 sammple tet!!")
fix_word_spell("maangoo")

08、互联网下载器

你可能会使用下载软件从互联网上下载照片或视频,但现在你可以使用Python IDM模块创建自己的下载器。

# Python下载器
# pip install internetdownloadmanager
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篇新闻文章。

# 世界新闻获取器
# pip install requests
import requests
ApiKey = "YOUR_API_KEY"
url = "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、PySide2 GUI

这个自动化脚本将帮助你使用PySide2 Gui模块创建GUI应用程序。可以在下面找到开始开发现代应用程序前端所需的所有方法。

# PySide 2 
# pip install PySide2
from PySide6.QtWidgets import *
from PySide6.QtGui import *
import sys
app = QApplication(sys.argv)
window = QWidget()
# 调整窗口大小
window.resize(500, 500)
# 设置窗口标题
window.setWindowTitle("PySide2 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())

本文的10个Python自动化脚本分享就到这里。

推荐书单

《Python从入门到精通(微课精编版)》

《Python从入门到精通(微课精编版)》使用通俗易懂的语言、丰富的案例,详细介绍了Python语言的编程知识和应用技巧。全书共24章,内容包括Python开发环境、变量和数据类型、表达式、程序结构、序列、字典和集合、字符串、正则表达式、函数、类、模块、异常处理和程序调试、进程和线程、文件操作、数据库操作、图形界面编程、网络编程、Web编程、网络爬虫、数据处理等,还详细介绍了多个综合实战项目。其中,第24章为扩展项目在线开发,是一章纯线上内容。全书结构完整,知识点与示例相结合,并配有案例实战,可操作性强,示例源代码大都给出详细注释,读者可轻松学习,快速上手。本书采用O2O教学模式,线下与线上协同,以纸质内容为基础,同时拓展更多超值的线上内容,读者使用手机微信扫一扫即可快速阅读,拓展知识,开阔视野,获取超额实战体验。

《Python从入门到精通(微课精编版)》icon-default.png?t=N7T8https://item.jd.com/13524355.html

图片

精彩回顾

《4个鲜为人知的Python迭代过滤函数》

《10个优化Python代码的小技巧,使代码运行速度提升5倍!》

《20个不可不知的Python概念(下)》

《20个不可不知的Python概念(上)》

《19个最佳自动化工作的Python脚本(下)》

《19个最佳自动化工作的Python脚本(上)》

微信搜索关注《Python学研大本营》,加入读者群,分享更多精彩

访问【IT今日热榜】,发现每日技术热点

  • 41
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值