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

前言
大家好,这里是浩道linux,主要给大家分享linux、python、网络通信相关的it知识平台。
今天浩道跟大家分享10个日常工作中用到的python自动化脚本。让你感受一番python简单强大之处!
 重复性任务总是耗时且无聊,想一想你想要一张一张地裁剪 100 张照片或 fetch api、纠正拼写和语法等工作,所有这些任务都很耗时,看看通过python自动化脚本去实现是怎样的一番工作。 01、 图片优化器 使用这个很棒的自动化脚本,可以帮助把图像处理的更好,你可以像在 photoshop 中一样编辑它们。 该脚本使用流行的是 pillow 模块,你可以在下面找到优化图像所需的大部分方法。
在你的图像编辑项目中使用
在你的 python 项目中使用它
批量图像编辑
更多
# image optimizing# pip install pillowimport pil# croping im = pil.image.open(image1.jpg)im = im.crop((34, 23, 100, 100))# resizingim = pil.image.open(image1.jpg)im = im.resize((50, 50))# flippingim = pil.image.open(image1.jpg)im = im.transpose(pil.image.flip_left_right)# rotatingim = pil.image.open(image1.jpg)im = im.rotate(360)# compressingim = pil.image.open(image1.jpg)im.save(image1.jpg, optimize=true, quality=90)# bluringim = pil.image.open(image1.jpg)im = im.filter(pil.imagefilter.blur)# sharpeningim = pil.image.open(image1.jpg)im = im.filter(pil.imagefilter.sharpen)# set brightnessim = pil.image.open(image1.jpg)im = pil.imageenhance.brightness(im)im = im.enhance(1.5)# set contrastim = pil.image.open(image1.jpg)im = pil.imageenhance.contrast(im)im = im.enhance(1.5)# adding filtersim = pil.image.open(image1.jpg)im = pil.imageops.grayscale(im)im = pil.imageops.invert(im)im = pil.imageops.posterize(im, 4)# savingim.save(image1.jpg)  
02、视频优化器
通过使用以下自动化脚本,你不仅可以使用 python 来优化视频,还可以使用它来优化图像。该脚本使用 moviepy 模块,允许你修剪、添加音频、设置视频速度、添加 vfx 等等。
创建完整的视频编辑器
在你的 python 项目中使用
修剪视频
从图像制作视频
更多
# video optimizer# pip install moviepyimport moviepy.editor as pyedit# load the videovideo = pyedit.videofileclip(vid.mp4)# trimmingvid1 = video.subclip(0, 10)vid2 = video.subclip(20, 40)final_vid = pyedit.concatenate_videoclips([vid1, vid2])# speed up the videofinal_vid = final_vid.speedx(2)# adding audio to the videoaud = pyedit.audiofileclip(bg.mp3)final_vid = final_vid.set_audio(aud)# reverse the videofinal_vid = final_vid.fx(pyedit.vfx.time_mirror)# merge two videosvid1 = pyedit.videofileclip(vid1.mp4)vid2 = pyedit.videofileclip(vid2.mp4)final_vid = pyedit.concatenate_videoclips([vid1, vid2])# add vfx to videovid1 = 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 videoimg1 = pyedit.imageclip(img1.jpg)img2 = pyedit.imageclip(img2.jpg)final_vid = pyedit.concatenate_videoclips([img1, img2])# save the videofinal_vid.write_videofile(final.mp4)  
03、pdf 转图片
这个小型自动化脚本可以方便地获取整个 pdf 页面并将它们转换为图像。该脚本使用流行的 pymupdf 模块,该模块以其 pdf 文本提取而闻名。
在你的 pdf 项目中使用它
批量 pdf 到图像
更多
# pdf to images# pip install pymupdfimport fitzdef pdf_to_images(pdf_file): doc = fitz.open(pdf_file) for p in doc: pix = p.get_pixmap() output = fpage{p.number}.png pix.writepng(output)pdf_to_images(test.pdf)  
04、获取 api 数据
需要从数据库中获取 api 数据或需要向服务器发送 api 请求。那么这个自动化脚本对你来说是一个方便的工具。使用 urllib3 模块,可让你获取和发布 api 请求。
# pip install urllib3import urllib3# fetch api dataurl = https://api.github.com/users/psf/reposhttp = urllib3.poolmanager()response = http.request('get', url)print(response.status)print(response.data)# post api dataurl = https://httpbin.org/posthttp = urllib3.poolmanager()response = http.request('post', url, fields={'hello': 'world'})print(response.status)  
05、电池指示灯
这个方便的脚本可以让你设置你想要得到通知的电池百分比,该脚本使用 pyler 进行通知,使用 psutil 获取当前的电池百分比。
# battery notifier# pip instal plyerfrom plyer import notificationimport psutilfrom time import sleepwhile 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 模块,这是一个机器学习模块,经过训练可以修复文本中的语法错误。
# grammer fixer# pip install happytransformerfrom happytransformer import happytexttotext as happytttfrom happytransformer import ttsettingsdef 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 thisgrammer_fixer(text)  
07、拼写修正
这个很棒的脚本将帮助你纠正你的文本单词拼写错误。你可以在下面找到脚本,将告诉你如何修复句子中的单个单词或多个单词。
# spell fixer# pip install textblobfrom textblob import *# fixing paragraph spellsdef fix_paragraph_words(paragraph): sentence = textblob(paragraph) correction = sentence.correct() print(correction)# fixing words spellsdef fix_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 模块创建自己的下载器。
下载 google 相册
在你的项目中使用
下载视频和音乐
更多
# python downloader# pip install internetdownloadmanagerimport internetdownloadmanager as idmdef 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 篇新闻文章。
# world news fetcher# pip install requestsimport requestsapikey = your_api_keyurl = 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 应用程序。你可以在下面找到开始开发体面的现代应用程序所需的每种方法。
pyside2 还支持跨平台,对开发人员非常友好,请查看下面的代码。
# pyside 2 # pip install pyside2from pyside6.qtwidgets import *from pyside6.qtgui import *import sysapp = qapplication(sys.argv)window = qwidget()# resize the windowwindow.resize(500, 500)# set the window titlewindow.setwindowtitle(pyside2 window)# add buttonsbutton = qpushbutton(click me, window)button.move(200, 200)# add label textlabel = qlabel(hello medium, window)label.move(200, 150)# add input boxinput_box = qlineedit(window)input_box.move(200, 250)print(input_box.text())# add radio buttonsradio_button = qradiobutton(radio button, window)radio_button.move(200, 300)# add checkboxcheckbox = qcheckbox(checkbox, window)checkbox.move(200, 350)# add sliderslider = qslider(window)slider.move(200, 400)# add progress barprogress_bar = qprogressbar(window)progress_bar.move(200, 450)# add image image = qlabel(window)image.setpixmap(qpixmap(image.png))# add message boxmsg = qmessagebox(window)msg.settext(message box)msg.setstandardbuttons(qmessagebox.ok | qmessagebox.cancel)window.show()sys.exit(app.exec())  


首尔半导体2011年营收首见衰退12%
紫光布局晶圆代工,越来越像三星
生物识别技术是否面临洗牌 非接触式生物识别发展强劲
8K电视真的来了
回顾AMD的2018的发展战略之路分析
10个杀手级的Python自动化脚本
绳索攀登机器人的制作
鸿海集团将积极布局新世代显示技术 MicroLED成明日之星
美的集团回应裁员50%的消息,将有序收缩非核心业务
就在明天!Matter开发训练营-从标准知识到设计实作演练
如何得知太阳能组件最大输出功率
华为Mate 10手机发布了手机盾可让金融服务更安全更智慧
高智商机器人背后的喜怒哀乐
发电机功率因数过高或过低的危害
中国电子展人才交流会11月上海启航
高通依托中国市场发展服务器芯片业务难成功
英特尔发布Horse Ridge超低温量子计算控制芯片
为跑分而生? 骁龙865跑分好看却忽视中国市场体验
如何挑选支持PD协议的充电宝?电芯种类和接口功能介绍
石墨烯锂电池概念股