在日常编程中,我们需要一些现成的代码来帮助我们解决日常生活中的问题。本文将向您展示日常生活中 Python 项目甚至用于解决问题的 10 个自动化脚本。所以把它加入书签,让我们开始吧。
👉照片压缩器
这是一个很棒的自动化脚本,可以通过保持质量不变来将您的照片压缩成较小的尺寸。
import PIL
from tkinter.filedialog import *
from PIL import Image
file_loc=askopenfilenames()
img = Image.open(file_loc[0])
img.save("Compressed.jpg", "JPEG", optimize = True, quality = 10)
print("Image is Compressed")
👉图片水印
您可能使用了不同的照片编辑软件来为您的照片添加水印。这个使用PIL 模块的简单 python 脚本将为任何图像添加水印。您可以设置文本、位置,甚至字体。
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def watermark_Image(img_path,output_path, text, pos):
img = Image.open(img_path)
drawing = ImageDraw.Draw(img)
black = (10, 5, 12)
drawing.text(pos, text, fill=black)
img.show()
img.save(output_path)
img = '1.jpg'
watermark_Image(img, 'watermarked.jpg','Python', pos=(0, 0))
👉 InstaDpViewer
这个 Python 自动化脚本将帮助您查看任何 Instagram 用户的DP。脚本使用模块instaloader,它将用户名作为输入并下载DP作为输出。
#pip install instaloader
import instaloader
ig = instaloader.Instaloader()
DP = input("Enter Insta username : ")
ig.download_profile(dp , profile_pic_only=True)
print("Your Image is Downloaded")
👉抄袭检查器
这是一个很棒的脚本,可以帮助您检查两个文件之间的抄袭。现在您不再需要任何软件或网络应用程序来检查抄袭。这将在一秒钟内完成您的工作。
from difflib import SequenceMatcher
def Plagerism_checker(f1, f2):
with open(f1,errors="ignore") as file1,open(f2,errors="ignore") as file2:
f1_data=file1.read()
f2_data=file2.read()
checking=SequenceMatcher(None, f1_data, f2_data).ratio()
print(f"These files are {checking*100} % similar")
file_1=input("Enter file 1 path: ")
file_2=input("Enter file 2 path: ")
Plagerism_checker(file_1, file_2)
👉 YT 视频下载器
这是另一个用于下载 Youtube 视频的简单自动化脚本。现在您不需要任何网络应用程序或软件,只需使用以下代码下载任何视频。
# pip install pytube
import pytube
link = input('Enter Youtube Video URL')
yt = pytube.Youtube(link)
yt.streams.first().download()
print('downloaded', link)
👉 将 PDF 转换为 CSV
有时我们需要将我们的 PDF 数据转换为 CSV 格式,所以对于这种工作,这个 Python 脚本会很方便。我已经提到了完成这项工作的两种方法。
import tabula
import camelot
# Method 1
filename = input("Enter File Path: ")
df = tabula.read_pdf(filename, encoding='utf-8', spreadsheet=True, pages='1')
df.to_csv('output.csv')
# Method 2
tables = camelot.read_pdf('file.pdf')
tables.export('file.csv', f='csv', compress=True)
👉文件加密和解密
想要锁定你的文件,那么这个脚本对你来说很方便。下面我提到可以在任何文件上工作的加密和解密代码。
# pip install cryptography
from cryptography.fernet import Fernet
def encrypt(filename, key):
fernet = Fernet(key)
with open(filename, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(filename, 'wb') as enc_file:
enc_file.write(encrypted)
def decrypt(filename, key):
fernet = Fernet(key)
with open(filename, 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
with open(filename, 'wb') as dec_file:
dec_file.write(decrypted)
# Generate Key
key = Fernet.generate_key()
filename = input("Enter Your filename: ")
encrypt(filename, key)
decrypt(filename, key)
👉电池通知
您在手机上看到了电池通知。那么,如果您的笔记本电脑也通知您电池状态怎么办。这个 Python 脚本将通过使用 3 个模块来完成完全相同的工作,它可以是 Battery Notifier。查看下面的代码。
pip install win10toast
pip install pywin32
pip install pyttsx3
import psutil
import time
import pyttsx3
import threading
from win10toast import ToastNotifier
bot=pyttsx3.init()
bot.setProperty('rate',110)
bot.setProperty('volume',3)
toaster = ToastNotifier()
def display_notification(text):
toaster.show_toast(text, duration=8)
while toaster.notification_active():
time.sleep(0.003)
def Battery_Notification():
while (True):
time.sleep(2)
battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = int(battery.percent)
if percent < 15:
if plugged == False:
processThread = threading.Thread(target=display_notification, args=("Your Battery at "+str(percent)+"% Please Plug the charger",))
processThread.start()
bot.say("Your battery is getting low so plug your charger")
bot.runAndWait()
elif percent >= 99:
if plugged == True:
processThread = threading.Thread(target=display_notification, args=("Charging is getting complete",))
processThread.start()
bot.say("Charging is Completed")
bot.runAndWait()
Battery_Notification()
elif percent >= 99:
if plugged == True:
processThread = threading.Thread(target=display_notification, args=("Charging is getting complete",))
processThread.start()
bot.say("Charging is Completed")
bot.runAndWait()
👉 将图像转换为 PDF
如果您有很多图像并希望将它们转换为单个 Pdf,那么此自动化脚本将对您很方便。
import os
import img2pdf
#Method 1
with open("Output.pdf", "wb") as file:
file.write(img2pdf.convert([i for i in os.listdir('Path of image_Directory') if i.endswith(".jpg")]))
#Method 2
from fpdf import FPDF
Pdf = FPDF()
list_of_images = ["1.jpg", "2.jpg"]
for i in list_of_images: # list of images with filename
Pdf.add_page()
Pdf.image(i,x,y,w,h)
Pdf.output("yourfile.pdf", "F")
👉 文字转语音人工智能机器人
如果您知道 Jarvis AI,那么此脚本将类似地工作。它使用 google Text to Speech API 将您的书面文本转换为 AI 机器人语音。检查下面的代码。
# pip install gTTS
from pygame import mixer
from gtts import gTTS
def main():
tts = gTTS('Learn Python from Medium')
tts.save('python.mp3')
mixer.init()
mixer.music.load('python.mp3')
mixer.music.play()
if __name__ == "__main__":
main()
最后的想法
好吧,这些是一些很棒的自动化 Python 脚本,您可以在项目中使用它们或解决日常生活问题。我希望您觉得这篇文章对您有所帮助且阅读起来很有趣。另外,与您的 Pythoneer 朋友分享❤️这篇文章。快乐编码!
来源原文