1 問題 用python寫一些方便實用的腳本,方便批量操作一些文件。 2 方法 先學會如何導入,用代碼建立不同軟件,文件與python的聯(lián)系。學會自帶的功能或導入別人制作的優(yōu)秀庫??梢宰约簩W習定義,編寫函數(shù)操作。 代碼清單 1 加水印 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 = '2.png' watermark_Image(img, 'watermarked_2.jpg','Python', pos=(10, 10)) 檢測文本相似性 from difflib import SequenceMatcher def file_similarity_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 = "路徑1" file_2 = "路徑2" file_similarity_checker(file_1, file_2) 加密 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) key = Fernet.generate_key() filename = "file.txt" encrypt(filename, key) 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) import pyAesCrypt def Encryption(input_file_path, output_file_path, key): pyAesCrypt.encryptFile(input_file_path, output_file_path, key) print("File has been decrypted") def Decryption(input_file_path, output_file_path, key): pyAesCrypt.decryptFile(input_file_path, output_file_path, key) print("File has been decrypted") decrypt(filename, key) |
3 結語 需要多學習了解一些有用的擴展庫和模塊。
|