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


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

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

    现在,让我们开始吧。

    01、 图片优化器

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

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

    • 在你的图像编辑项目中使用

    • 在你的 Python 项目中使用它

    • 批量图像编辑

    更多

    1. # Image Optimizing
    2. # pip install Pillow
    3. import PIL
    4. # Croping 
    5. im = PIL.Image.open("Image1.jpg")
    6. im = im.crop((3423100100))
    7. # Resizing
    8. im = PIL.Image.open("Image1.jpg")
    9. im = im.resize((5050))
    10. # Flipping
    11. im = PIL.Image.open("Image1.jpg")
    12. im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
    13. # Rotating
    14. im = PIL.Image.open("Image1.jpg")
    15. im = im.rotate(360)
    16. # Compressing
    17. im = PIL.Image.open("Image1.jpg")
    18. im.save("Image1.jpg", optimize=True, quality=90)
    19. # Bluring
    20. im = PIL.Image.open("Image1.jpg")
    21. im = im.filter(PIL.ImageFilter.BLUR)
    22. # Sharpening
    23. im = PIL.Image.open("Image1.jpg")
    24. im = im.filter(PIL.ImageFilter.SHARPEN)
    25. Set Brightness
    26. im = PIL.Image.open("Image1.jpg")
    27. im = PIL.ImageEnhance.Brightness(im)
    28. im = im.enhance(1.5)
    29. Set Contrast
    30. im = PIL.Image.open("Image1.jpg")
    31. im = PIL.ImageEnhance.Contrast(im)
    32. im = im.enhance(1.5)
    33. # Adding Filters
    34. im = PIL.Image.open("Image1.jpg")
    35. im = PIL.ImageOps.grayscale(im)
    36. im = PIL.ImageOps.invert(im)
    37. im = PIL.ImageOps.posterize(im, 4)
    38. # Saving
    39. im.save("Image1.jpg")

    02、视频优化器

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

    • 创建完整的视频编辑器

    • 在你的 Python 项目中使用

    • 修剪视频

    • 从图像制作视频

    更多

    1. # Video Optimizer
    2. # pip install moviepy
    3. import moviepy.editor as pyedit
    4. # Load the Video
    5. video = pyedit.VideoFileClip("vid.mp4")
    6. # Trimming
    7. vid1 = video.subclip(010)
    8. vid2 = video.subclip(2040)
    9. final_vid = pyedit.concatenate_videoclips([vid1, vid2])
    10. # Speed up the video
    11. final_vid = final_vid.speedx(2)
    12. # Adding Audio to the video
    13. aud = pyedit.AudioFileClip("bg.mp3")
    14. final_vid = final_vid.set_audio(aud)
    15. # Reverse the Video
    16. final_vid = final_vid.fx(pyedit.vfx.time_mirror)
    17. Merge two videos
    18. vid1 = pyedit.VideoFileClip("vid1.mp4")
    19. vid2 = pyedit.VideoFileClip("vid2.mp4")
    20. final_vid = pyedit.concatenate_videoclips([vid1, vid2])
    21. Add VFX to Video
    22. vid1 = final_vid.fx(pyedit.vfx.mirror_x)
    23. vid2 = final_vid.fx(pyedit.vfx.invert_colors)
    24. final_vid = pyedit.concatenate_videoclips([vid1, vid2])
    25. Add Images to Video
    26. img1 = pyedit.ImageClip("img1.jpg")
    27. img2 = pyedit.ImageClip("img2.jpg")
    28. final_vid = pyedit.concatenate_videoclips([img1, img2])
    29. # Save the video
    30. final_vid.write_videofile("final.mp4")

    03、PDF 转图片

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

    • 在你的 PDF 项目中使用它

    • 批量 PDF 到图像

    更多

    1. # PDF to Images
    2. # pip install PyMuPDF
    3. import fitz
    4. def pdf_to_images(pdf_file):
    5.     doc = fitz.open(pdf_file)
    6.     for p in doc:
    7.         pix = p.get_pixmap()
    8.         output = f"page{p.number}.png"
    9.         pix.writePNG(output)
    10. pdf_to_images("test.pdf")

    04、获取 API 数据

    需要从数据库中获取 API 数据或需要向服务器发送 API 请求。那么这个自动化脚本对你来说是一个方便的工具。使用 Urllib3 模块,可让你获取和发布 API 请求。

    1. # pip install urllib3
    2. import urllib3
    3. # Fetch API data
    4. url = "https://api.github.com/users/psf/repos"
    5. http = urllib3.PoolManager()
    6. response = http.request('GET', url)
    7. print(response.status)
    8. print(response.data)
    9. # Post API data
    10. url = "https://httpbin.org/post"
    11. http = urllib3.PoolManager()
    12. response = http.request('POST', url, fields={'hello''world'})
    13. print(response.status)

    05、电池指示灯

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

    1. # Battery Notifier
    2. # pip instal plyer
    3. from plyer import notification
    4. import psutil
    5. from time import sleep
    6. while True:
    7.     battery = psutil.sensors_battery()
    8.     life = battery.percent
    9.     if life < 50:
    10.         notification.notify(
    11.             title = "Battery Low",
    12.             message = "Please connect to power source",
    13.             timeout = 10
    14.         )
    15.     sleep(60)

    06、语法固定器

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

    1. # Grammer Fixer
    2. # pip install happytransformer
    3. from happytransformer import HappyTextToText as HappyTTT
    4. from happytransformer import TTSettings
    5. def Grammer_Fixer(Text):
    6.     Grammer = HappyTTT("T5","prithivida/grammar_error_correcter_v1")
    7.     config = TTSettings(do_sample=Truetop_k=10, max_length=100)
    8.     corrected = Grammer.generate_text(Text, args=config)
    9.     print("Corrected Text: ", corrected.text)
    10. Text = "This is smple tet we how know this"
    11. Grammer_Fixer(Text)

    07、拼写修正

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

    1. # Spell Fixer
    2. # pip install textblob
    3. from textblob import *
    4. # Fixing Paragraph Spells
    5. def fix_paragraph_words(paragraph):
    6.     sentence = TextBlob(paragraph)
    7.     correction = sentence.correct()
    8.     print(correction)
    9. # Fixing Words Spells
    10. def fix_word_spell(word):
    11.     word = Word(word)
    12.     correction = word.correct()
    13.     print(correction)
    14. fix_paragraph_words("This is sammple tet!!")
    15. fix_word_spell("maangoo")

    08、互联网下载器

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

    • 下载 Google 相册

    • 在你的项目中使用

    • 下载视频和音乐

    更多

    1. # Python Downloader
    2. # pip install internetdownloadmanager
    3. import internetdownloadmanager as idm
    4. def Downloader(url, output):
    5.     pydownloader = idm.Downloader(worker=20,
    6.                                 part_size=1024*1024*10,
    7.                                 resumable=True,)
    8.     pydownloader .download(url, output)
    9. Downloader("Link url""image.jpg")
    10. Downloader("Link url""video.mp4")

    09、获取世界新闻

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

    1. # World News Fetcher
    2. # pip install requests
    3. import requests
    4. ApiKey = "YOUR_API_KEY"
    5. url = "https://api.worldnewsapi.com/search-news?text=hurricane&api-key={ApiKey}"
    6. headers = {
    7.   'Accept''application/json'
    8. }
    9. response = requests.get(url, headers=headers)
    10. print("News: ", response.json())

    10、PySide2 GUI

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

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

    1. # PySide 2 
    2. # pip install PySide2
    3. from PySide6.QtWidgets import *
    4. from PySide6.QtGui import *
    5. import sys
    6. app = QApplication(sys.argv)
    7. window = QWidget()
    8. # Resize the Window
    9. window.resize(500500)
    10. Set the Window Title
    11. window.setWindowTitle("PySide2 Window")
    12. Add Buttons
    13. button = QPushButton("Click Me", window)
    14. button.move(200200)
    15. Add Label Text
    16. label = QLabel("Hello Medium", window)
    17. label.move(200150)
    18. Add Input Box
    19. input_box = QLineEdit(window)
    20. input_box.move(200250)
    21. print(input_box.text())
    22. Add Radio Buttons
    23. radio_button = QRadioButton("Radio Button", window)
    24. radio_button.move(200300)
    25. Add Checkbox
    26. checkbox = QCheckBox("Checkbox", window)
    27. checkbox.move(200350)
    28. Add Slider
    29. slider = QSlider(window)
    30. slider.move(200400)
    31. Add Progress Bar
    32. progress_bar = QProgressBar(window)
    33. progress_bar.move(200450)
    34. Add Image 
    35. image = QLabel(window)
    36. image.setPixmap(QPixmap("image.png"))
    37. Add Message Box
    38. msg = QMessageBox(window)
    39. msg.setText("Message Box")
    40. msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
    41. window.show()
    42. sys.exit(app.exec())

    最后的想法

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

  • 相关阅读:
    手写模拟器,解放双手!效果炸裂的生产工具
    【C++代码】二叉树的最大深度,二叉树的最小深度,完全二叉树的节点个数--代码随想录
    SpringBootApplication注解的简介说明
    计算机毕业设计之java+ssm某地区精准扶贫网站
    神经网络(十六)Pytorch实机运行的一些细节
    threeJS与模型交互
    FDTD script command(结构)
    B+树的定义以及查找
    模板方法模式,基于继承实现的简单的设计模式(设计模式与开发实践 P11)
    Spring MVC 入门指南
  • 原文地址:https://blog.csdn.net/qiqi1220/article/details/127982349