1 问题
用python写一些方便实用的脚本,方便批量操作一些文件。
2 方法
先学会如何导入,用代码建立不同软件,文件与python的联系。学会自带的功能或导入别人制作的优秀库。可以自己学习定义,编写函数操作。
代码清单 1
加水印from PIL import Imagefrom PIL import ImageFontfrom PIL import ImageDrawdef 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 SequenceMatcherdef 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 Fernetdef 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 pyAesCryptdef 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 结语
需要多学习了解一些有用的扩展库和模块。