10 个实用的 Python 自动化脚本_python 脚本-程序员宅基地

技术标签: python  自动化  开发语言  

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

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

现在,让我们开始吧。

01、 图片优化器

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

该脚本使用流行的是 Pillow 模块

# 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 等等。

# 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 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
    if life < 50:
        notification.notify(
            title = "Battery Low",
            message = "Please connect to power source",
            timeout = 10
        )
    sleep(60)

06、语法固定器

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

# Grammer Fixer
# 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、拼写修正

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

# Spell Fixer
# pip install textblob
from textblob import *
# Fixing Paragraph Spells
def fix_paragraph_words(paragraph):
    sentence = TextBlob(paragraph)
    correction = sentence.correct()
    print(correction)
# Fixing Words Spells
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、互联网下载器

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

# Python Downloader
# 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 篇新闻文章。

# World News Fetcher
# 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()
# 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学习】
学Python的伙伴,欢迎加入新的交流【君羊】:572077127
一起探讨编程知识,成为大神,群里还有软件安装包,实战案例、学习资料 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_56659172/article/details/128160863

智能推荐

C#笔记-----串口类(异步发送,接收)_vs c# 串口非堵塞接收-程序员宅基地

文章浏览阅读3.7k次。using System;using System.Collections.Generic;using System.IO.Ports;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApp1{ class Comm { public..._vs c# 串口非堵塞接收

vasp计算之输入输出文件_vasp自旋极化磁矩为负-程序员宅基地

文章浏览阅读8.6k次,点赞11次,收藏75次。一、vasp文件:INCAR in **STOPCAR instout outPOTCAR in **KPOINTS in **IBZKPT outPOSCAR in **CONTCAR outEXHCAR in (should not be used ..._vasp自旋极化磁矩为负

地大计算机历年分数线,中国地质大学武汉历年分数线 2021中国地质大学武汉录取分数线...-程序员宅基地

文章浏览阅读513次。一、2019年中国地质大学(武汉)各省录取分数线及录取位次统计表1、中国地质大学(武汉)2019年在重庆录取分数线如下:文科录取批次本一批589分,最低录取位次为2331名、理科录取批次本一批590分,最低录取位次为12910名;2、中国地质大学(武汉)2019年在甘肃录取分数线如下:文科录取批次本一批571分,最低录取位次为1600名、理科录取批次本一批545分,最低录取位次为9913名;3、中..._中国地质大学计算机录取分数线

pool win10提示bad_大师讲解win10蓝屏重启代码BAD,POOL,CALLER修复方法-程序员宅基地

文章浏览阅读9k次,点赞2次,收藏2次。大家在操作电脑时一定会遇到很多问题,比如说win10蓝屏重启代码BAD,POOL,CALLER修复方法就是我们经常会遇到的,小编及身边的朋友也遇到过win10蓝屏重启代码BAD,POOL,CALLER修复方法很多次,针对win10蓝屏重启代码BAD,POOL,CALLER修复方法这样的问题,应该怎么解决呢,其实很简单,小编制作了较为简单的教程,我们只需要按照如果我们使用的电脑安装的是win10操作..._bad caller 修复

1-6 输出带框文字_本题要求编写程序,输出指定的带框文字。-程序员宅基地

文章浏览阅读8.9k次。1-6 输出带框文字(5 分) 本题要求编写程序,输出指定的带框文字。 输入格式:本题无输入 输出格式:按照下列格式输出带框文字。************ Welcome************#includeint main(){ printf("************\n"); printf(" Welcome\n"); printf("****_本题要求编写程序,输出指定的带框文字。

相机标定—>标定图片拍摄规范(附棋盘图)_标定棋盘格图片-程序员宅基地

文章浏览阅读2w次,点赞25次,收藏134次。标定图片拍摄规范: 在标定的整个过程中,不能调节相机的光圈、焦距,要保证在标定中摄像头进光量与焦距的一致。 建议在摄像头视野内五个不同位置上(左上、右上、左下、右下、正中心)分别拍摄图片。 拍摄的图片最好为摄像头视野的1/4左右。 不要只拍摄标定板与镜头面平行的图片,也要拍摄一些有倾斜角度的图片。 拍摄过程中可以对标定板适当的进行补光,调节标定板到镜头..._标定棋盘格图片

随便推点

同一局域网下自己能ping通别人,别人ping不通自己_局域网我可以ping a,a ping不了我-程序员宅基地

文章浏览阅读6.8k次,点赞2次,收藏2次。更改自己本地的网络状况为如下图所示:_局域网我可以ping a,a ping不了我

全网最全最细的PLSQL下载、安装、配置、使用指南、问题解答,相关问题已汇总-程序员宅基地

文章浏览阅读2.6w次,点赞92次,收藏401次。双击之后,这里选择安装目录,你安装目录选的哪里,这里就填哪里。后面点下去就可以,弄好之后重新打开PLSQL,就已经是中文界面了。对没有语言包的PLSQL可以直接在软件里面调,有语言包的下载Chinese语言安装包。我下载的是有语言包的,但是怎么配置,我看了其它博主的教学,也不难,找到地方就行。大家都有自己熟悉的快捷键设置,只要不冲突就行,其它快捷键可自行摸索。这个感觉还是有必要的,每次登录都不需要再输口令了,勾选带口令存储。就我个人而言,全屏模式挺友好的,原始界面看着太小,使用起来会眼花。........._plsql下载

OpenGL之纹理过滤的四种方式_opengl 纹理滤波-程序员宅基地

文章浏览阅读8.3k次。I.纹理过滤:当三维空间里面的多边形经过坐标变换、投影、光栅化等过程,变成二维屏幕上的一组象素的时候,对每个象素需要到相应纹理图像中进行采样,这个过程就称为纹理过滤。II.纹理过滤通常分为2种情况:a) 纹理被缩小 GL_TEXTURE_MIN_FILTER 比如说一个8 x 8的纹理贴到一个平行于xy平面的正方形上,最后该正方形在屏幕上只占4 x 4的象素矩阵,这种情况下一个象素对应着多个纹理单元。b) 纹理被_opengl 纹理滤波

LeetCode 119. Pascal's Triangle II(杨辉三角II) -- c语言_力扣119. 杨辉三角|| c语言具体思路-程序员宅基地

文章浏览阅读313次。119. Pascal's Triangle IIGiven a non-negativeindexkwherek≤33, return thekthindex row of the Pascal's triangle.Note that the row index starts from0.In Pascal's triangle, each number is ..._力扣119. 杨辉三角|| c语言具体思路

Python3 中 bytes 和 string 之间的互相转换_bytes to string-程序员宅基地

文章浏览阅读8.7w次,点赞11次,收藏49次。因为这是原来不知道 Markdown 时写的文章,富文本编辑器不知道怎么用脚注,所以参考资料显得有些孤立了,见谅。Table of Contents前言创建 bytes 型数据创建字符串相互转换string to bytes按 utf-8 的方式编码,转成 bytes按 gb2312 的方式编码,转成 bytesbytes to string解码成 stri..._bytes to string

售前工程师、实施工程师、运维、技术支持这些岗位的具体区别是什么?他们的工作内容大致都是什么?_技术支持和交付工程知乎-程序员宅基地

文章浏览阅读2.1w次,点赞27次,收藏102次。作者:大胆草民链接:https://www.zhihu.com/question/23793594/answer/27388387来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。售前:在产品销售过程中,和客户接洽的除了销售人员,就是这个售前工程师了。销售人员,我们从一般来说,门槛都是比较低的,很少会对销售人员的技术能力有所要求。然后问题就出现了,如果客户问到技..._技术支持和交付工程知乎

推荐文章

热门文章

相关标签