分享4个Python中的非常好用的自动化脚本

作者:川川菜鸟 时间:2023-12-03 09:59:17 

这里有一些很棒的自动化脚本,你可以在你的 Python 项目中使用它们。在做项目的时候,我们需要一些现成的代码来帮助我们解决日常生活中的问题。本文为你的 Python 项目提供了4个自动化脚本,可以解决这些问题。

照片压缩器

这会将您的照片压缩成较小的尺寸,而j尽量的保证质量相同。

from PIL import Image
from tkinter.filedialog import *
fl=askopenfilenames()  # 打开文件夹
img = Image.open(fl[0])
img.save("result.jpg", "JPEG", optimize = True, quality = 100)

图片水印

这个简单的脚本将为任何图像添加水印。你可以设置文本、位置和字体。

from PIL import Image
from PIL import ImageDraw, ImageFont

def watermark_img(img_path, res_path, text, pos):
   font = ImageFont.truetype("简启体.TTF", 20)
   img = Image.open(img_path)
   wm = ImageDraw.Draw(img)
   wm.text(pos, text, align='center', font=font, fill='red')
   img.show()
   img.save(res_path)

img = '4.png'
watermark_img(img, 'result2.jpg', '川川菜鸟', pos=(5, 5))

如下:

分享4个Python中的非常好用的自动化脚本

抄袭检查器

此脚本检查两个文件之间的抄袭。首先我创建一个word文件如下:

分享4个Python中的非常好用的自动化脚本

第二个文件如下,我只改变的数字:

分享4个Python中的非常好用的自动化脚本

脚本如下:

from difflib import SequenceMatcher

f1 = input("输入文件一的路径: ")
f2 = input("输入文件二的路径: ")

def plagiarism_checker(f1, f2):
   with open(f1, errors="ignore") as file1, open(f2, errors="ignore") as file2:
       f1_data = file1.read()
       f2_data = file2.read()
       res = SequenceMatcher(None, f1_data, f2_data).ratio()
       print("两个文件相似度为:",res)

plagiarism_checker(f1, f2)

测试结果:

分享4个Python中的非常好用的自动化脚本

文件加解密

一个可以加密/解密任何文件的小脚本。先编写解密和加密函数。

from cryptography.fernet import Fernet
import re
# 加密
def encrypt(f_name, key):
   fernet = Fernet(key)

with open(f_name, 'rb') as file:
       original = file.read()

encrypted = fernet.encrypt(original)

with open(f_name, 'wb') as enc_file:
       enc_file.write(encrypted)

# 解密
def decrypt(f_name, key):
   fernet = Fernet(key)

with open(f_name, 'rb') as enc_file:
       encrypted = enc_file.read()

decrypted = fernet.decrypt(encrypted)
   with open(f_name, 'wb') as dec_file:
       dec_file.write(decrypted)

执行加密函数:

# 加密部分  每次密码都是不同,所以保存起来,免得解密失败
key = Fernet.generate_key()
with open('加密的密码.txt', 'w') as f:
   f.write(str(key))
   f.close()
f_name = input("Enter Your filename: ")
encrypt(f_name, key) # 执行加密

首先我执行加密函数,对1.docx加密,我再去打开它是一片空白:

分享4个Python中的非常好用的自动化脚本

打开保存的密码如下:

b'b3LaZZPrLeQFtN8JYUfz3n_uWntQ0x0zH0i5-9hzBLU='

执行解密:

f_name2 =input("Enter Your filename: ")
key2=open('加密的密码.txt').read()
secret = re.findall(r'[\'](.*?)[\']',key2)[0] # 通过正则匹配出密码内容
decrypt(f_name2, secret ) # 执行解密

执行后就能再打开了。

来源:https://chuanchuan.blog.csdn.net/article/details/127694373

标签:Python,自动化,脚本
0
投稿

猜你喜欢

  • Vue使用Echarts图表多次初始化报错问题的解决方法

    2023-07-02 16:49:54
  • python程序运行进程、使用时间、剩余时间显示功能的实现代码

    2023-04-14 11:16:33
  • python Popen 获取输出,等待运行完成示例

    2022-06-22 20:43:58
  • 用Python Flask创建简洁高效的URL短链接服务

    2022-10-12 16:21:49
  • Python自动化办公之创建PPT文件

    2022-01-16 19:31:49
  • python多进程共享变量

    2023-08-30 07:40:53
  • PHP中Static(静态)关键字功能与用法实例分析

    2024-05-03 15:53:52
  • Python中对象迭代与反迭代的技巧总结

    2023-06-08 04:25:33
  • asp中用insert into语句向数据库插入记录(添加信息)的方法

    2011-02-05 10:46:00
  • 简单理解Python中的装饰器

    2021-06-18 01:44:21
  • CSS注意事项: 针对 Safari(WebKit)

    2008-10-29 11:57:00
  • 如何查看SQLSERVER中某个查询用了多少TempDB空间

    2024-01-16 06:40:21
  • go doudou应用中使用枚举类型教程示例

    2024-03-17 19:50:00
  • 浅谈对Python变量的一些认识理解

    2021-06-10 05:06:52
  • python中的bisect模块与二分查找详情

    2021-07-23 05:17:56
  • 用Dreamweaver实现飘浮光球特效

    2008-03-03 12:28:00
  • Python调用Prometheus监控数据并计算

    2023-12-01 02:18:39
  • Python中glob库实现文件名的匹配

    2022-08-06 21:50:19
  • 在Python 的线程中运行协程的方法

    2021-03-10 18:06:38
  • python 制作简单的音乐播放器

    2022-09-12 14:30:44
  • asp之家 网络编程 m.aspxhome.com