0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

分享10個(gè)實(shí)用的Python自動(dòng)化腳本

jf_78858299 ? 來(lái)源:Python人工智能編程 ? 作者:Python人工智能編程 ? 2023-01-21 15:58 ? 次閱讀

重復(fù)性任務(wù)總是耗時(shí)且無(wú)聊,想一想你想要一張一張地裁剪 100 張照片或 Fetch API、糾正拼寫(xiě)和語(yǔ)法等工作,所有這些任務(wù)都很耗時(shí),為什么不自動(dòng)化它們呢?在今天的文章中,我將與你分享 10 個(gè) Python 自動(dòng)化腳本。

所以,請(qǐng)你把這篇文章放在你的收藏清單上,以備不時(shí)之需,在IT行業(yè)里,程序員的學(xué)習(xí)永無(wú)止境……

現(xiàn)在,讓我們開(kāi)始吧。

01、 圖片優(yōu)化器

使用這個(gè)很棒的自動(dòng)化腳本,可以幫助把圖像處理的更好,你可以像在 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、視頻優(yōu)化器

通過(guò)使用以下自動(dòng)化腳本,你不僅可以使用 Python 來(lái)優(yōu)化視頻,還可以使用它來(lái)優(yōu)化圖像。該腳本使用 Moviepy 模塊,允許你修剪、添加音頻、設(shè)置視頻速度、添加 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 轉(zhuǎn)圖片

這個(gè)小型自動(dòng)化腳本可以方便地獲取整個(gè) PDF 頁(yè)面并將它們轉(zhuǎn)換為圖像。該腳本使用流行的 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 數(shù)據(jù)

需要從數(shù)據(jù)庫(kù)中獲取 API 數(shù)據(jù)或需要向服務(wù)器發(fā)送 API 請(qǐng)求。那么這個(gè)自動(dòng)化腳本對(duì)你來(lái)說(shuō)是一個(gè)方便的工具。使用 Urllib3 模塊,可讓你獲取和發(fā)布 API 請(qǐng)求。

# 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、電池指示燈

這個(gè)方便的腳本可以讓你設(shè)置你想要得到通知的電池百分比,該腳本使用 Pyler 進(jìn)行通知,使用 Psutil 獲取當(dāng)前的電池百分比。

# 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、語(yǔ)法固定器

厭倦了校對(duì)你的長(zhǎng)文章或文本,然后,你可以試試這個(gè)自動(dòng)化腳本,它將掃描你的文本并糾正語(yǔ)法錯(cuò)誤,這個(gè)很棒的腳本使用 Happtransformer 模塊,這是一個(gè)機(jī)器學(xué)習(xí)模塊,經(jīng)過(guò)訓(xùn)練可以修復(fù)文本中的語(yǔ)法錯(cuò)誤。

# 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、拼寫(xiě)修正

這個(gè)很棒的腳本將幫助你糾正你的文本單詞拼寫(xiě)錯(cuò)誤。你可以在下面找到腳本,將告訴你如何修復(fù)句子中的單個(gè)單詞或多個(gè)單詞。

# 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、互聯(lián)網(wǎng)下載

你們可能使用下載軟件從 Internet 下載照片或視頻,但現(xiàn)在你可以使用 Python IDM 模塊創(chuàng)建自己的下載器。

# 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、獲取世界新聞

使用此自動(dòng)化腳本讓你隨時(shí)了解每日世界新聞,你可以使用任何語(yǔ)言從任何國(guó)家/地區(qū)獲取新聞。這個(gè) API 讓你每天免費(fèi)獲取 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

這個(gè)自動(dòng)化腳本將幫助你使用 PySide2 Gui 模塊創(chuàng)建你的 GUI 應(yīng)用程序。你可以在下面找到開(kāi)始開(kāi)發(fā)體面的現(xiàn)代應(yīng)用程序所需的每種方法。

# 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())
聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • API
    API
    +關(guān)注

    關(guān)注

    2

    文章

    1461

    瀏覽量

    61489
  • 自動(dòng)化
    +關(guān)注

    關(guān)注

    28

    文章

    5386

    瀏覽量

    78623
  • python
    +關(guān)注

    關(guān)注

    53

    文章

    4753

    瀏覽量

    84072
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    介紹10個(gè)Python自動(dòng)化腳本

    在這個(gè)自動(dòng)化時(shí)代,我們有很多重復(fù)無(wú)聊的工作要做。想想這些你不再需要一次又一次地做的無(wú)聊的事情,讓它自動(dòng)化,讓你的生活更輕松。那么在本文中,我將向您介紹 10 個(gè)
    發(fā)表于 10-17 09:27 ?894次閱讀

    10個(gè)Python腳本來(lái)自動(dòng)化你的日常任務(wù)

    今天浩道跟大家分享幾個(gè)關(guān)于python自動(dòng)化日常工作的實(shí)用案例腳本!
    發(fā)表于 10-25 09:04 ?690次閱讀

    10個(gè)殺手級(jí)的Python自動(dòng)化腳本

    今天浩道跟大家分享10個(gè)日常工作中用到的python自動(dòng)化腳本。讓你感受一番python簡(jiǎn)單強(qiáng)大
    發(fā)表于 11-28 11:07 ?637次閱讀

    【上?!揩C頭推薦職位-自動(dòng)化測(cè)試工程師(java/python

    獵頭職位:自動(dòng)化測(cè)試工程師(java/python)工作職責(zé):1.負(fù)責(zé)測(cè)試報(bào)告輸出、項(xiàng)目風(fēng)險(xiǎn)評(píng)估,對(duì)測(cè)試結(jié)果負(fù)責(zé);2.負(fù)責(zé)對(duì)產(chǎn)品進(jìn)行接口測(cè)試/自動(dòng)化測(cè)試/性能測(cè)試/安全測(cè)試等工作;3.參與產(chǎn)品需求
    發(fā)表于 06-28 17:37

    自動(dòng)化測(cè)試腳本開(kāi)發(fā)技巧

    開(kāi)發(fā)自動(dòng)化測(cè)試腳本的技巧和心得軟件測(cè)試 增量式調(diào)試腳本 錄制測(cè)試腳本,和其他的軟件開(kāi)發(fā)成果一樣,會(huì)變得非常大。為了可以成功的回放,需要調(diào)試幾百行的代碼,為了參數(shù)
    發(fā)表于 03-26 16:24 ?53次下載

    iOS自動(dòng)化打包腳本

    iOS自動(dòng)化打包腳本,地址:(https://github.com/hades0918/ipapy) iOS項(xiàng)目自動(dòng)打包腳本 1.腳本
    發(fā)表于 10-12 16:47 ?0次下載

    云測(cè)試自動(dòng)化中的Python

      用于測(cè)試的編程需要與開(kāi)發(fā)應(yīng)用程序的編程不同的方法。如果你想要一種專(zhuān)門(mén)用于測(cè)試編碼的簡(jiǎn)單而精簡(jiǎn)的語(yǔ)言,Python 是一個(gè)不錯(cuò)的選擇。Python 測(cè)試自動(dòng)化框架為驗(yàn)證、數(shù)據(jù)生成和
    的頭像 發(fā)表于 12-09 14:53 ?735次閱讀

    如何創(chuàng)建自動(dòng)化腳本

    在前面的 bash 初學(xué)者系列文章中,我們介紹了關(guān)于 bash 的一些基礎(chǔ)知識(shí),在了解了這些基礎(chǔ)知識(shí)之后,可以嘗試創(chuàng)建一些自動(dòng)化腳本,來(lái)完成一些相對(duì)枯燥重復(fù)的管理任務(wù)。
    的頭像 發(fā)表于 01-04 16:50 ?3252次閱讀
    如何創(chuàng)建<b class='flag-5'>自動(dòng)化</b><b class='flag-5'>腳本</b>

    10個(gè)殺手級(jí)的Python自動(dòng)化腳本分享

    重復(fù)性任務(wù)總是耗時(shí)且無(wú)聊,想一想你想要一張一張地裁剪 100 張照片或 Fetch API、糾正拼寫(xiě)和語(yǔ)法等工作,所有這些任務(wù)都很耗時(shí),為什么不自動(dòng)化它們呢?在今天的文章中,我將與你分享 10 個(gè)
    的頭像 發(fā)表于 01-06 15:34 ?662次閱讀

    使用Python腳本實(shí)現(xiàn)自動(dòng)化運(yùn)維任務(wù)

    許多運(yùn)維工程師會(huì)使用 Python 腳本來(lái)自動(dòng)化運(yùn)維任務(wù)。Python 是一種流行的編程語(yǔ)言,具有豐富的第三方庫(kù)和強(qiáng)大的自動(dòng)化能力,適用于許多不同的領(lǐng)域。
    的頭像 發(fā)表于 04-08 10:36 ?1520次閱讀

    Facebook群組自動(dòng)化python – 網(wǎng)絡(luò)自動(dòng)化

    電子發(fā)燒友網(wǎng)站提供《Facebook群組自動(dòng)化python – 網(wǎng)絡(luò)自動(dòng)化.zip》資料免費(fèi)下載
    發(fā)表于 07-05 14:26 ?0次下載
    Facebook群組<b class='flag-5'>自動(dòng)化</b><b class='flag-5'>python</b> – 網(wǎng)絡(luò)<b class='flag-5'>自動(dòng)化</b>

    python自動(dòng)化腳本辦公-文件整理

    今天講解文件整理腳本的實(shí)現(xiàn)過(guò)程。這是一個(gè)很有用的技能,可以幫助你管理你的電腦上的各種文件。需求如下: 需求內(nèi)容:給定一個(gè)打算整理的文件夾目錄,這個(gè)腳本可以將該目錄下的所有文件都揪出來(lái),
    的頭像 發(fā)表于 07-20 09:49 ?791次閱讀

    keil自動(dòng)化編譯腳本

    這是一個(gè) keil 的自動(dòng)化編譯腳本,可被其他腳本或程序調(diào)用,接收參數(shù)并按參數(shù)編譯 keil 工程,而不必打開(kāi) keil 軟件,實(shí)現(xiàn)程序上的自動(dòng)化
    的頭像 發(fā)表于 10-16 17:04 ?1291次閱讀
    keil<b class='flag-5'>自動(dòng)化</b>編譯<b class='flag-5'>腳本</b>

    Python 模擬鍵盤(pán)鼠標(biāo)的方式實(shí)現(xiàn)自動(dòng)化

    實(shí)現(xiàn)自動(dòng)化。 Python中模擬鍵盤(pán)和鼠標(biāo)最著名的模塊是: pymouse 和 pykeyboard 。一次安裝兩個(gè)模塊比較麻煩,而有一個(gè)庫(kù)整合了這兩
    的頭像 發(fā)表于 11-02 14:48 ?1052次閱讀
    <b class='flag-5'>Python</b> 模擬鍵盤(pán)鼠標(biāo)的方式實(shí)現(xiàn)<b class='flag-5'>自動(dòng)化</b>

    如何使用Python編寫(xiě)腳本來(lái)自動(dòng)發(fā)送郵件

    Python是一種非常流行的編程語(yǔ)言,可以用于多種用途,包括自動(dòng)化任務(wù)。其中一個(gè)常見(jiàn)的自動(dòng)化任務(wù)是自動(dòng)發(fā)送郵件。在本文中,我們將介紹如何使用
    的頭像 發(fā)表于 12-07 11:36 ?1053次閱讀