10 个杀手级的 Python 自动化脚本

重复性任务总是耗时且无聊,想一想你想要一张一张地裁剪 100 张照片或 Fetch API、纠正拼写和语法等工作,所有这些任务都很耗时,为什么不自动化它们呢?在今天的文章中,我将与你分享 10 个 Python 自动化脚本。

所以,请你把这篇文章放在你的收藏清单上,以备不时之需,在IT行业里,程序员的学习永无止境……

现在,让我们开始吧。

01、 图片优化器

使用这个很棒的自动化脚本,可以帮助把图像处理的更好,你可以像在 Photoshop 中一样编辑它们。

该脚本使用流行的是 Pillow 模块,你可以在下面找到优化图像所需的大部分方法。

  • 在你的图像编辑项目中使用
  • 在你的 Python 项目中使用它
  • 批量图像编辑
# Image Optimizing
# pip install Pillow
import PIL
# Croping 
im = PIL.Image.open("Image1.jpg")
im = im.crop((34, 23, 100, 100))
# Resizing
im = PIL.Image.open("Image1.jpg")
im = im.resize((50, 50))
# Flipping
im = PIL.Image.open("Image1.jpg")
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
# Rotating
im = PIL.Image.open("Image1.jpg")
im = im.rotate(360)
# Compressing
im = PIL.Image.open("Image1.jpg")
im.save("Image1.jpg", optimize=True, quality=90)
# Bluring
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.BLUR)
# Sharpening
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.SHARPEN)
# Set Brightness
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Brightness(im)
im = im.enhance(1.5)
# Set Contrast
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Contrast(im)
im = im.enhance(1.5)
# Adding Filters
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageOps.grayscale(im)
im = PIL.ImageOps.invert(im)
im = PIL.ImageOps.posterize(im, 4)
# Saving
im.save("Image1.jpg")

02、视频优化器

通过使用以下自动化脚本,你不仅可以使用 Python 来优化视频,还可以使用它来优化图像。该脚本使用 Moviepy 模块,允许你修剪、添加音频、设置视频速度、添加 VFX 等等。

  • 创建完整的视频编辑器
  • 在你的 Python 项目中使用
  • 修剪视频
  • 从图像制作视频
# Video Optimizer
# pip install moviepy
import moviepy.editor as pyedit
# Load the Video
video = pyedit.VideoFileClip("vid.mp4")
# Trimming
vid1 = video.subclip(0, 10)
vid2 = video.subclip(20, 40)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Speed up the video
final_vid = final_vid.speedx(2)
# Adding Audio to the video
aud = pyedit.AudioFileClip("bg.mp3")
final_vid = final_vid.set_audio(aud)
# Reverse the Video
final_vid = final_vid.fx(pyedit.vfx.time_mirror)
# Merge two videos
vid1 = pyedit.VideoFileClip("vid1.mp4")
vid2 = pyedit.VideoFileClip("vid2.mp4")
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Add VFX to Video
vid1 = final_vid.fx(pyedit.vfx.mirror_x)
vid2 = final_vid.fx(pyedit.vfx.invert_colors)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Add Images to Video
img1 = pyedit.ImageClip("img1.jpg")
img2 = pyedit.ImageClip("img2.jpg")
final_vid = pyedit.concatenate_videoclips([img1, img2])
# Save the video
final_vid.write_videofile("final.mp4")

03、PDF 转图片

这个小型自动化脚本可以方便地获取整个 PDF 页面并将它们转换为图像。该脚本使用流行的 PyMuPDF 模块,该模块以其 PDF 文本提取而闻名。

  • 在你的 PDF 项目中使用它
  • 批量 PDF 到图像
# PDF to Images
# 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
# Fetch API data
url = "https://api.github.com/users/psf/repos"
http = urllib3.PoolManager()
response = http.request('GET', url)
print(response.status)
print(response.data)
# Post API data
url = "https://httpbin.org/post"
http = urllib3.PoolManager()
response = http.request('POST', url, fields={'hello': 'world'})
print(response.status)

05、电池指示灯

这个方便的脚本可以让你设置你想要得到通知的电池百分比,该脚本使用 Pyler 进行通知,使用 Psutil 获取当前的电池百分比。

# Battery Notifier
# pip instal plyer
from plyer import notification
import psutil
from time import sleep
while True:
    battery = psutil.sensors_battery()
    life = battery.percent
&#xA0;&#xA0;&#xA0;&#xA0;if&#xA0;life&#xA0;< 50:         notification.notify(             title ="&#xA0;"Battery&#xA0;Low","             message ="&#xA0;"Please&#xA0;connect&#xA0;to&#xA0;power&#xA0;source","             timeout ="&#xA0;10"         )     sleep(60) < code></ 50:>

06、语法固定器

厌倦了校对你的长文章或文本,然后,你可以试试这个自动化脚本,它将扫描你的文本并纠正语法错误,这个很棒的脚本使用 Happtransformer 模块,这是一个机器学习模块,经过训练可以修复文本中的语法错误。

#&#xA0;Grammer&#xA0;Fixer
#&#xA0;pip&#xA0;install&#xA0;happytransformer
from&#xA0;happytransformer&#xA0;import&#xA0;HappyTextToText&#xA0;as&#xA0;HappyTTT
from&#xA0;happytransformer&#xA0;import&#xA0;TTSettings
def&#xA0;Grammer_Fixer(Text):
&#xA0;&#xA0;&#xA0;&#xA0;Grammer&#xA0;=&#xA0;HappyTTT("T5","prithivida/grammar_error_correcter_v1")
&#xA0;&#xA0;&#xA0;&#xA0;config&#xA0;=&#xA0;TTSettings(do_sample=True,&#xA0;top_k=10,&#xA0;max_length=100)
&#xA0;&#xA0;&#xA0;&#xA0;corrected&#xA0;=&#xA0;Grammer.generate_text(Text,&#xA0;args=config)
&#xA0;&#xA0;&#xA0;&#xA0;print("Corrected&#xA0;Text:&#xA0;",&#xA0;corrected.text)
Text&#xA0;=&#xA0;"This&#xA0;is&#xA0;smple&#xA0;tet&#xA0;we&#xA0;how&#xA0;know&#xA0;this"
Grammer_Fixer(Text)

07、拼写修正

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

#&#xA0;Spell&#xA0;Fixer
#&#xA0;pip&#xA0;install&#xA0;textblob
from&#xA0;textblob&#xA0;import&#xA0;*
#&#xA0;Fixing&#xA0;Paragraph&#xA0;Spells
def&#xA0;fix_paragraph_words(paragraph):
&#xA0;&#xA0;&#xA0;&#xA0;sentence&#xA0;=&#xA0;TextBlob(paragraph)
&#xA0;&#xA0;&#xA0;&#xA0;correction&#xA0;=&#xA0;sentence.correct()
&#xA0;&#xA0;&#xA0;&#xA0;print(correction)
#&#xA0;Fixing&#xA0;Words&#xA0;Spells
def&#xA0;fix_word_spell(word):
&#xA0;&#xA0;&#xA0;&#xA0;word&#xA0;=&#xA0;Word(word)
&#xA0;&#xA0;&#xA0;&#xA0;correction&#xA0;=&#xA0;word.correct()
&#xA0;&#xA0;&#xA0;&#xA0;print(correction)
fix_paragraph_words("This&#xA0;is&#xA0;sammple&#xA0;tet!!")
fix_word_spell("maangoo")

08、互联网下载器

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

  • 下载 Google 相册
  • 在你的项目中使用
  • 下载视频和音乐
#&#xA0;Python&#xA0;Downloader
#&#xA0;pip&#xA0;install&#xA0;internetdownloadmanager
import&#xA0;internetdownloadmanager&#xA0;as&#xA0;idm
def&#xA0;Downloader(url,&#xA0;output):
&#xA0;&#xA0;&#xA0;&#xA0;pydownloader&#xA0;=&#xA0;idm.Downloader(worker=20,
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;part_size=1024*1024*10,
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;resumable=True,)

&#xA0;&#xA0;&#xA0;&#xA0;pydownloader&#xA0;.download(url,&#xA0;output)
Downloader("Link&#xA0;url",&#xA0;"image.jpg")
Downloader("Link&#xA0;url",&#xA0;"video.mp4")

09、获取世界新闻

使用此自动化脚本让你随时了解每日世界新闻,你可以使用任何语言从任何国家/地区获取新闻。这个 API 让你每天免费获取 50 篇新闻文章。

#&#xA0;World&#xA0;News&#xA0;Fetcher
#&#xA0;pip&#xA0;install&#xA0;requests
import&#xA0;requests
ApiKey&#xA0;=&#xA0;"YOUR_API_KEY"
url&#xA0;=&#xA0;"https://api.worldnewsapi.com/search-news?text=hurricane&api-key={ApiKey}"
headers&#xA0;=&#xA0;{
&#xA0;&#xA0;'Accept':&#xA0;'application/json'
}
response&#xA0;=&#xA0;requests.get(url,&#xA0;headers=headers)
print("News:&#xA0;",&#xA0;response.json())

10、PySide2 GUI

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

PySide2 还支持跨平台,对开发人员非常友好,请查看下面的代码。

#&#xA0;PySide&#xA0;2&#xA0;
#&#xA0;pip&#xA0;install&#xA0;PySide2
from&#xA0;PySide6.QtWidgets&#xA0;import&#xA0;*
from&#xA0;PySide6.QtGui&#xA0;import&#xA0;*
import&#xA0;sys
app&#xA0;=&#xA0;QApplication(sys.argv)
window&#xA0;=&#xA0;QWidget()
#&#xA0;Resize&#xA0;the&#xA0;Window
window.resize(500,&#xA0;500)
#&#xA0;Set&#xA0;the&#xA0;Window&#xA0;Title
window.setWindowTitle("PySide2&#xA0;Window")
#&#xA0;Add&#xA0;Buttons
button&#xA0;=&#xA0;QPushButton("Click&#xA0;Me",&#xA0;window)
button.move(200,&#xA0;200)
#&#xA0;Add&#xA0;Label&#xA0;Text
label&#xA0;=&#xA0;QLabel("Hello&#xA0;Medium",&#xA0;window)
label.move(200,&#xA0;150)
#&#xA0;Add&#xA0;Input&#xA0;Box
input_box&#xA0;=&#xA0;QLineEdit(window)
input_box.move(200,&#xA0;250)
print(input_box.text())
#&#xA0;Add&#xA0;Radio&#xA0;Buttons
radio_button&#xA0;=&#xA0;QRadioButton("Radio&#xA0;Button",&#xA0;window)
radio_button.move(200,&#xA0;300)
#&#xA0;Add&#xA0;Checkbox
checkbox&#xA0;=&#xA0;QCheckBox("Checkbox",&#xA0;window)
checkbox.move(200,&#xA0;350)
#&#xA0;Add&#xA0;Slider
slider&#xA0;=&#xA0;QSlider(window)
slider.move(200,&#xA0;400)
#&#xA0;Add&#xA0;Progress&#xA0;Bar
progress_bar&#xA0;=&#xA0;QProgressBar(window)
progress_bar.move(200,&#xA0;450)
#&#xA0;Add&#xA0;Image&#xA0;
image&#xA0;=&#xA0;QLabel(window)
image.setPixmap(QPixmap("image.png"))
#&#xA0;Add&#xA0;Message&#xA0;Box
msg&#xA0;=&#xA0;QMessageBox(window)
msg.setText("Message&#xA0;Box")
msg.setStandardButtons(QMessageBox.Ok&#xA0;|&#xA0;QMessageBox.Cancel)
window.show()
sys.exit(app.exec())

最后的想法

今天这篇文章内容就到这里了,我希望你能为你的下一个开发项目找到一些有用的自动化脚本。

Original: https://blog.csdn.net/qiqi1220/article/details/127982349
Author: 宋宋讲编程
Title: 10 个杀手级的 Python 自动化脚本

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/732342/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Pandas两重山之Series(循环)

    啊哦~你想找的内容离你而去了哦 内容不存在,可能为如下原因导致: ① 内容还在审核中 ② 内容以前存在,但是由于不符合新 的规定而被删除 ③ 内容地址错误 ④ 作者删除了内容。 可…

    Python 2023年8月7日
    042
  • sklearn安装后无法调用的问题

    sklearn安装后无法调用的问题 出现的问题 网页上随便复制了一段使用sklearn的代码之后,发现出错了代码跑起来之后是缺少sklearn模块 ; 解决思路 1)按照网上的思路…

    Python 2023年9月30日
    045
  • 详细解释numpy库中slice切片用法 0,1,None

    详解numpy库中切片用法 0,1,None 在图像处理中,常常会对矩阵进行切片操作,例如,通过切片操作拿出ROI区域,或者做一些变换。 [1:3]取矩阵的[1,3)的第一行和第二…

    Python 2023年8月27日
    027
  • Web:移动端阻止默认行为的小坑

    问题 移动端 web 开发中,使用 addEventListener 阻止了 touchstart 事件的默认行为却发现没有生效 描述 再移动端 web 开发中,我们一般会用 ad…

    Python 2023年6月6日
    063
  • SCRUM官方权威指南

    posted @2022-04-18 21:32 HammerZe 阅读(25 ) 评论() 编辑 Original: https://www.cnblogs.com/48xz/p…

    Python 2023年6月10日
    072
  • scrapy爬JS-1

    ps: 楼主是生物类专业的,所以对计算机只会个大概,原理部分还不太懂,难免有小错误QAQ A. 判断一个网页是静态网页还是JS网页 第一种方法: 1. 按F12/Ctrl+Shif…

    Python 2023年10月4日
    033
  • 【完美解析】蓝桥杯 省赛 杨辉三角形 python组 找规律+二分查找+组合数

    题目 看到最后如果还不懂你来打我~ ; 分析 我们看到杨辉三角形很容易想到一个数的值等于它肩膀两个数的和。为此,可以不断通过前一行的数求出后一行的数,重复上面操作,直到找到目标为止…

    Python 2023年8月1日
    076
  • Seasonal-ARIMA模型

    Seasonal-ARIMA模型 Autore gressive Integrated Moving Averages 建立ARIMA 模型的一般过程如下: 1: 模块导入,加载数…

    Python 2023年8月20日
    052
  • FAQ Selenium中提示can not connect to the service chromedriver 的解决方法

    一个同学反馈运行如下代码 from selenium import webdriver    from  time import sleep driver = webdriver….

    Python 2023年6月12日
    052
  • Python3学习笔记

    标准数据类型 Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(…

    Python 2023年5月25日
    063
  • Python中的集合关系都有哪些?

    数学中,两个集合关系的常见操作包括:交集、并集、差集、补集。设A.B是两个集合,集合之间的关系如下。 (1)交集是指属于集合A且属于集合B的元素所组成的集合。 (2)并集是指集合A…

    Python 2023年10月3日
    036
  • Hadoop伪分布式的搭建

    1.准备Linux环境1.1 开启网络,ifconfig指令查看ip 1.2 修改主机名为自己名字(hadoop) 1.3修改主机名和IP的映射关系 1.4关闭防火墙 1.5重启L…

    Python 2023年6月16日
    068
  • lambda表达式和 map,reduce,filter

    map(function, sequence[, sequence, …]) -> list 第一个参数传函数,需要对sequence做什么操作第二个参数为序列(元组,列…

    Python 2023年6月6日
    058
  • Pytest学习-allure报告

    一、Windows安装java和pycharm安装allure插件 pycharm安装allure pip3 install allure-pytest Windows安装java…

    Python 2023年5月24日
    099
  • pandas数据分析总结

    Pandas 是 Python 语言的一个扩展程序库,用于数据分析。其中DataFrame,Json,数据筛选的使用频次很高,值得重点将基本用法学习并总结。 版本信息:python…

    Python 2023年8月8日
    067
  • 线程池的极简用法——内置线程池multiprocessing

    大家好。今天,博主们将分享一个通向线程池的小快捷方式–如何使用内置的线程池。 [En] Hello, everyone. Today, bloggers are goi…

    Python 2023年5月24日
    079
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球