<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          10 個 Python 自動化腳本!

          共 7176字,需瀏覽 15分鐘

           ·

          2023-02-28 10:23

          在下方公眾號后臺回復(fù): 面試手冊 ,可獲取 PDF 手冊。


          重復(fù)性任務(wù)總是耗時且無聊,想一想你想要一張一張地裁剪 100 張照片或 Fetch API、糾正拼寫和語法等工作,所有這些任務(wù)都很耗時,為什么不自動化它們呢?

          在今天的文章中,我將與你分享 10 個 Python 自動化腳本。

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

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

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

          使用這個很棒的自動化腳本,可以幫助把圖像處理的更好,你可以像在 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")

          2、視頻優(yōu)化器

          通過使用以下自動化腳本,你不僅可以使用 Python 來優(yōu)化視頻,還可以使用它來優(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")

          3、PDF 轉(zhuǎn)圖片

          這個小型自動化腳本可以方便地獲取整個 PDF 頁面并將它們轉(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")

          4、獲取 API 數(shù)據(jù)

          需要從數(shù)據(jù)庫中獲取 API 數(shù)據(jù)或需要向服務(wù)器發(fā)送 API 請求。那么這個自動化腳本對你來說是一個方便的工具。使用 Urllib3 模塊,可讓你獲取和發(fā)布 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)

          5、電池指示燈

          這個方便的腳本可以讓你設(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)

          6、語法固定器

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

                #?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)

          7、拼寫修正

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

                #?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")

          8、互聯(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")

          9、獲取世界新聞

          使用此自動化腳本讓你隨時了解每日世界新聞,你可以使用任何語言從任何國家/地區(qū)獲取新聞。這個 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 模塊創(chuàng)建你的 GUI 應(yīng)用程序。你可以在下面找到開始開發(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())

          來源:網(wǎng)絡(luò)

          推薦閱讀:

          太強(qiáng)了!利用 Python 寫了一個監(jiān)控服務(wù)器資源利用率的腳本!

          寫了個自動巡檢多個接口地址的腳本!

          拿來就用的腳本案例!

          7 個非常實用的 Shell 拿來就用腳本實例!

          超硬核!11 個非常實用的 Python 和 Shell 拿來就用腳本實例!

          41996df809fcd8b0292e44cb6683fda1.webp

          瀏覽 79
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  蜜 桃 黄 片AV在线观看 91人妻人人澡人人爽人人精品 | 影视先锋成人 | 91麻豆精品国产91久久久久久久久 | 91射射射... | 爱爱视频免费网站 |