Python自动化脚本有哪些

其他教程   发布日期:2023年10月22日   浏览次数:349

本篇内容主要讲解“Python自动化脚本有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python自动化脚本有哪些”吧!

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

01、图像优化器

这个伟大的自动化脚本可以帮助你更好地处理图片,你可以像在Photoshop中一样编辑它们。

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

  1. # 图像优化
  2. # pip install Pillow
  3. import PIL
  4. # 裁剪
  5. im = PIL.Image.open("Image1.jpg")
  6. im = im.crop((34, 23, 100, 100))
  7. # 调整大小
  8. im = PIL.Image.open("Image1.jpg")
  9. im = im.resize((50, 50))
  10. # 翻转
  11. im = PIL.Image.open("Image1.jpg")
  12. im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
  13. # 旋转
  14. im = PIL.Image.open("Image1.jpg")
  15. im = im.rotate(360)
  16. # 压缩
  17. im = PIL.Image.open("Image1.jpg")
  18. im.save("Image1.jpg", optimize=True, quality=90)
  19. # 模糊化
  20. im = PIL.Image.open("Image1.jpg")
  21. im = im.filter(PIL.ImageFilter.BLUR)
  22. # 锐化
  23. im = PIL.Image.open("Image1.jpg")
  24. im = im.filter(PIL.ImageFilter.SHARPEN)
  25. # 设置亮度
  26. im = PIL.Image.open("Image1.jpg")
  27. im = PIL.ImageEnhance.Brightness(im)
  28. im = im.enhance(1.5)
  29. # 设置对比度
  30. im = PIL.Image.open("Image1.jpg")
  31. im = PIL.ImageEnhance.Contrast(im)
  32. im = im.enhance(1.5)
  33. # 添加过滤器
  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. # 保存
  39. im.save("Image1.jpg")

02、视频优化器

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

  1. # 视频优化器
  2. # pip install moviepy
  3. import moviepy.editor as pyedit
  4. # 加载视频
  5. video = pyedit.VideoFileClip("vid.mp4")
  6. # 修剪
  7. vid1 = video.subclip(0, 10)
  8. vid2 = video.subclip(20, 40)
  9. final_vid = pyedit.concatenate_videoclips([vid1, vid2])
  10. # 加快视频的速度
  11. final_vid = final_vid.speedx(2)
  12. # 在视频中添加音频
  13. aud = pyedit.AudioFileClip("bg.mp3")
  14. final_vid = final_vid.set_audio(aud)
  15. # 反转视频
  16. final_vid = final_vid.fx(pyedit.vfx.time_mirror)
  17. # 合并两个视频
  18. vid1 = pyedit.VideoFileClip("vid1.mp4")
  19. vid2 = pyedit.VideoFileClip("vid2.mp4")
  20. final_vid = pyedit.concatenate_videoclips([vid1, vid2])
  21. # 在视频中添加视觉特效
  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. # 在视频中添加图像
  26. img1 = pyedit.ImageClip("img1.jpg")
  27. img2 = pyedit.ImageClip("img2.jpg")
  28. final_vid = pyedit.concatenate_videoclips([img1, img2])
  29. # 保存视频
  30. final_vid.write_videofile("final.mp4")

03、将PDF转换为图像

这个小的自动化脚本可以很容易地检索整个PDF页面并将其转换为图像。该脚本使用了流行的PyMuPDF模块,该模块以其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请求,这个自动化脚本是你的一个便利工具。使用Urlib3模块,你可以获取和发布API请求。

  1. # pip install urllib3
  2. 输入urllib3
  3. # 获取API数据
  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. # 发布API数据
  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. # 电池通知器
  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. #寿命 = 电池百分比
  10. if life < 50:
  11. notification.notify(
  12. title = "Battery Low" #电池电量不足
  13. message = "Please connect to power source",
  14. timeout = 10
  15. )
  16. 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=True, top_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. # 拼写修正器
  2. # pip 安装 textblob
  3. # pip install textblob
  4. from textblob import *
  5. def fix_paragraph_words(paragraph):
  6. sentence = TextBlob(paragraph)
  7. correction = sentence.correct()
  8. print(correction)
  9. # 修复字词拼写
  10. def fix_word_spell(word):
  11. word = Word(word)
  12. 更正 = word.correct()
  13. print(correction)
  14. fix_paragraph_words("this is sammple tet!!")
  15. fix_word_spell("maangoo")

08、互联网下载器

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

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

10、PySide2 GUI

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

  1. # PySide2
  2. # pip install PySide2
  3. from PySide6.QtWidgets import *
  4. from PySide6.QtGui import *
  5. app = QApplication(sys.argv)
  6. window = QWidget()
  7. # 调整窗口的大小
  8. window.resize(500, 500)
  9. # 设置窗口标题
  10. window.setWindowTitle("PySide2 Window")
  11. # 添加按钮
  12. button = QPushButton("Click Me", window)
  13. button.move(200, 200)
  14. # 添加标签文本
  15. label = QLabel("Hello Medium", window)
  16. label.move(200, 150)
  17. # 添加输入框
  18. input_box = QLineEdit(window)
  19. input_box.move(200, 250)
  20. print(input_box.text())
  21. # 添加单选按钮
  22. radio_button = QRadioButton("Radio Button", window)
  23. radio_button.move(200, 300)
  24. # 添加复选框
  25. checkbox = QCheckBox("Checkbox", window)
  26. checkbox.move(200, 350)
  27. # 添加滑块
  28. slider = QSlider(window)
  29. slider.move(200, 400)
  30. # 添加进度条
  31. progress_bar = QProgressBar(window)
  32. progress_bar.move(200, 450)
  33. # 添加图片
  34. image = QLabel(window)
  35. image.setPixmap(QPixmap("image.png"))
  36. # 添加消息框
  37. msg = QMessageBox(window)
  38. msg.setText("Message Box")
  39. msg.setStandardButtons(QMessageBox.OK | QMessageBox.Cancel)
  40. window.show()
  41. sys.exit(app.exec())

以上就是Python自动化脚本有哪些的详细内容,更多关于Python自动化脚本有哪些的资料请关注九品源码其它相关文章!