Python检测文本文件的相似性

核心功能 - 检测文本文件的相似性

很多时候我们需要来检查两文件的相似性,到底存在着多少的雷同,或许以下的这个脚本文件可以派得上用场:

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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)

Python给照片添加水印

核心功能 给照片加水印

给照片添加水印的代码多种多样,下面这种的或许是最为简单的形式:

代码实现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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','水印文字', pos=(10, 10))

Python邮件发送

邮件发送

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 1、导入模块
import yagmail
 
# 2、设置smtp服务信息
yag = yagmail.SMTP(user="改成自己的邮箱账号@126.com", password="改成自己的邮箱密码", host='smtp.126.com')
 
# 3、设置邮件主题与邮件内容
subject = 'Python邮件测试'
content = ['Python邮件测试 -- 邮件来自黑马程序员Python+大数据']
 
# 4、发送邮件
yag.send('gocndws@126.com', subject, content)

Python批量压缩文件

核心功能 - 批量压缩文件

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import zipfile # zipfile库 压缩文件
import os
import time
 
 
def batch_zip(start_dir):
    start_dir = start_dir #文件路径
    file_news = start_dir + '.zip' # 压缩后文件夹的名字
 
    z = zipfile.ZipFile(file_news, 'w', zipfile.ZIP_DEFLATED)
    for dir_path, dir_names, file_names in os.walk(start_dir):
        #避免从根目录复制
        f_path = dir_path.replace(start_dir, '')
        #压缩所有文件
        f_path = f_path and f_path + os.sep
        for filename in file_names:
            z.write(os.path.join(dir_path, filename), f_path + filename)
     z.close()
     return file_news
 
 
batch_zip('./data/ziptest')

用于清理数据的5个简单但功能强大的Python脚本

用于清理数据的 5 个简单但功能强大的 Python 脚本

面对现实吧。通常,数据清理可能会让人头疼,而不是因为复杂性。通常情况下,我对进行数据清理的想法感到畏缩,因为它变得如此单调。说我已经成为 Python 的“奇才”是轻描淡写的。 Python 非常轻量级,您不需要一个完整的开发服务器来为大多数典型的数据清理任务生成一个快速的 Python 脚本。

Python把PDF转图片

核心功能 - 把PDF转图片

这个小型自动化脚本可以方便地获取整个 PDF 页面并将它们转换为图像。该脚本使用流行的 PyMuPDF 模块,该模块以其 PDF 文本提取而闻名。

在你的 PDF 项目中使用它 批量 PDF 到图像

Python电池指示灯

核心功能 - 电池指示灯

这个方便的脚本可以让你设置你想要得到通知的电池百分比,该脚本使用 Pyler 进行通知,使用 Psutil 获取当前的电池百分比。

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 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)

语法固定器

厌倦了校对你的长文章或文本,然后,你可以试试这个自动化脚本,它将扫描你的文本并纠正语法错误,这个很棒的脚本使用 Happtransformer 模块,这是一个机器学习模块,经过训练可以修复文本中的语法错误。

Python拼写修正

核心功能 - 拼写修正

这个很棒的脚本将帮助你纠正你的文本单词拼写错误。你可以在下面找到脚本,将告诉你如何修复句子中的单个单词或多个单词。

实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 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")

Python自动桌面提示

核心功能 - 自动桌面提示

这个脚本会自动触发windows桌面通知,提示重要事项,比如说:您已工作两小时,该休息了

我们可以设定固定时间提示,比如隔10分钟、1小时等

Python自动生成素描草图

核心功能 - 自动生成素描草图

这个脚本可以把彩色图片转化为铅笔素描草图,对人像、景色都有很好的效果。

而且只需几行代码就可以一键生成,适合批量操作,非常的快捷。