10 個實用的 Python 自動化腳本!(文末送書)
10 個實用的 Python 自動化腳本!(文末送書)
gh_1d7504e4dee1
回復(fù):python,領(lǐng)取Python面試題。分享Python教程,Python架構(gòu)師教程,Python爬蟲,Python編程視頻,Python腳本,Pycharm教程,Python微服務(wù)架構(gòu),Python分布式架構(gòu),Pycharm注冊碼。
重復(fù)性任務(wù)總是耗時且無聊,想一想你想要一張一張地裁剪 100 張照片或 Fetch API、糾正拼寫和語法等工作,所有這些任務(wù)都很耗時,為什么不自動化它們呢?在今天的文章中,我將與你分享 10 個 Python 自動化腳本。
所以,請你把這篇文章放在你的收藏清單上,以備不時之需,在IT行業(yè)里,程序員的學(xué)習(xí)永無止境……
現(xiàn)在,讓我們開始吧。
01、 圖片優(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"
)
Python從入門到進階知識手冊
02、視頻優(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"
)
03、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"
)
04、獲取 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)
05、電池指示燈
這個方便的腳本可以讓你設(shè)置你想要得到通知的電池百分比,該腳本使用 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 模塊,這是一個機器學(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)
07、拼寫修正
這個很棒的腳本將幫助你糾正你的文本單詞拼寫錯誤。你可以在下面找到腳本,將告訴你如何修復(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"
)
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、獲取世界新聞
使用此自動化腳本讓你隨時了解每日世界新聞,你可以使用任何語言從任何國家/地區(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())
《細說Python編程:從入門到科學(xué)計算 》
內(nèi)容簡介
《細說Python編程:從入門到科學(xué)計算》以Python 3.x為平臺,由兩位博士執(zhí)筆,詳盡細致地闡述Python編程的基礎(chǔ)知識和高級技巧,并以大量示例代碼進行實踐,同時還介紹人工智能領(lǐng)域廣泛使用的科學(xué)計算工具NumPy。
全書分為兩篇,共19章。第1篇(第1~11章)為基礎(chǔ)知識,主要講解Python的編譯環(huán)境、入門知識、字符串、容器、函數(shù)、循環(huán)、條件選擇、模塊、類、類的特殊成員、文件;第2篇(第12~19章)為高級應(yīng)用,包括異常處理、日期和時間、測試代碼、程序打包、使用數(shù)據(jù)庫、網(wǎng)絡(luò)編程、圖形用戶界面、科學(xué)計算等內(nèi)容。全書知識點豐富,輔之以示例演示,讀者可以邊學(xué)邊練,快速掌握。
點擊下方鏈接,立即購買
-
2023年血糖新標準公布,不是3.9-6.1,快來看看你的血糖正常嗎? 2023-02-07
-
2023年各省最新電價一覽!8省中午執(zhí)行谷段電價! 2023-01-03
-
PPT導(dǎo)出高分辨率圖片的四種方法 2022-09-22
-
2023年最新!國家電網(wǎng)27家省級電力公司負責人大盤點 2023-03-14
-
全國消防救援總隊主官及簡歷(2023.2) 2023-02-10
-
盤點 l 中國石油大慶油田現(xiàn)任領(lǐng)導(dǎo)班子 2023-02-28
-
我們的前輩!歷屆全國工程勘察設(shè)計大師完整名單! 2022-11-18
-
關(guān)于某送變電公司“4·22”人身死亡事故的快報 2022-04-26
