10 个实用的 Python 自动化脚本!
重复性任务总是耗时且无聊,想一想你想要一张一张地裁剪 100 张照片或 Fetch API、纠正拼写和语法等工作,所有这些任务都很耗时,为什么不自动化它们呢?在今天的文章中,我将与你分享 10 个 Python 自动化脚本。
所以,请你把这篇文章放在你的收躲清单上,以备不时之需,在IT行业里,程序员的学习永无止境……
现在,让我们开始吧。
01、 图片优化器
使用这个很棒的自动化脚本,可以扶助把图像处理的更好,你可以像在 Photoshop 中一样编辑它们。
该脚本使用时髦的是 Pillow 模块
# Image Optimizing
# pip install Pillow
importPIL
# 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 等等。
# Video Optimizer
# pip install moviepy
importmoviepy.editor aspyedit
# 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 to Images
# pip install PyMuPDF
importfitz
defpdf_to_images(pdf_file):
doc = fitz.open(pdf_file)
forp indoc:
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
importurllib3
# Fetch API data
url = ""
response = )
print(response.status)
print(response.data)
# Post API data
url = ""
response = '})
print(response.status)
05、电池指示灯
这个方便的脚本可以让你设置你想要得到通知的电池百分比,该脚本使用 Pyler 进行通知,使用 Psutil 获取当前的电池百分比。
# Battery Notifier
# pip instal plyer
fromplyer importnotification
importpsutil
fromtime importsleep
whileTrue:
battery = psutil.sensors_battery
life = battery.percent
iflife 50:
notification.notify(
title = "Battery Low",
message = "Please connect to power source",
timeout = 10
sleep( 60)
06、语法固定器
厌倦了校对你的长文章或文本,然后,你可以试试这个自动化脚本,它将扫描你的文本并纠正语法错误,这个很棒的脚本使用 Happtransformer 模块,这是一个机器学习模块,经过练习可以修复文本中的语法错误。
# Grammer Fixer
# pip install happytransformer
fromhappytransformer importHappyTextToText asHappyTTT
fromhappytransformer importTTSettings
defGrammer_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、拼写修正
这个很棒的脚本将扶助你纠正你的文本单词拼写错误。你可以在下面找到脚本,将告诉你如何修复句子中的单个单词或多个单词。
# Spell Fixer
# pip install textblob
fromtextblob import*
# Fixing Paragraph Spells
deffix_paragraph_words(paragraph):
sentence = TextBlob(paragraph)
correction = sentence.correct
print(correction)
# Fixing Words Spells
deffix_word_spell(word):
word = Word(word)
correction = word.correct
print(correction)
fix_paragraph_words( "This is sammple tet!!")
fix_word_spell( "maangoo")
08、互联网下载器
你们可能使用下载软件从 Internet 下载照片或视频,但现在你可以使用 Python IDM 模块创建自己的下载器。
# Python Downloader
# pip install internetdownloadmanager
importinternetdownloadmanager asidm
defDownloader(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 篇新闻文章。
# World News Fetcher
# pip install requests
importrequests
ApiKey = "YOUR_API_KEY"
url = "}"
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
fromPySide6.QtWidgets import*
fromPySide6.QtGui import*
importsys
app = QApplication(sys.argv)
window = QWidget
# Resize the Window
window.resize( 500, 500)
# Set the Window Title
window.setWindowTitle( "PySide2 Window")
# Add Buttons
button = QPushButton( "Click Me", window)
button.move( 200, 200)
# Add Label Text
label = QLabel( "Hello Medium", window)
label.move( 200, 150)
# Add Input Box
input_box = QLineEdit(window)
input_box.move( 200, 250)
print(input_box.text)
# Add Radio Buttons
radio_button = QRadioButton( "Radio Button", window)
radio_button.move( 200, 300)
# Add Checkbox
checkbox = QCheckBox( "Checkbox", window)
checkbox.move( 200, 350)
# Add Slider
slider = QSlider(window)
slider.move( 200, 400)
# Add Progress Bar
progress_bar = QProgressBar(window)
progress_bar.move( 200, 450)
# Add Image
image = QLabel(window)
image.setPixmap(QPixmap( "image.png"))
# Add Message Box
msg = QMessageBox(window)
msg.setText( "Message Box")
msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
window.show
sys.exit(app.exec)
进门: 最全的零基础学Python的问题 | 零基础学了8个月的Python |实战项目 | 学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |从万众期待到口碑扑街!唐探3令人失看 | 笑看新倚天屠龙记 | 灯谜答题王 | 用Python做个海量小姐姐素描图 | 碟中谍这么火,我用机器学习做个迷你推举系统电影
趣味:弹球游戏 | 九宫格 | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI:会做诗的机器人 | 给图片上色 | 推测收进 | 碟中谍这么火,我用机器学习做个迷你推举系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf! |再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制造一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐! |
年度爆款文案
1). 卧槽!Pdf转Word用Python轻松搞定 !
2).学Python真香!我用100行代码做了个网站,帮人PS旅行图片,赚个鸡腿食
3).首播过亿,火爆全网,我分析了《乘风破浪的姐姐》,发现了这些秘密
4). 80行代码!用Python做一个哆来A梦分身
5).你必须把握的20个python代码,短小精悍,用处无穷
6). 30个Python奇淫技艺集
7). 我总结的80页《菜鸟学Python精选干货.pdf》,都是干货
8). 再见Python!我要学Go了!2500字深度分析 !
9).发现一个舔狗福利!这个Python爬虫神器太爽了,自动下载妹子图片