百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

分享10个Python自动化脚本,解决日常问题

off999 2024-12-28 14:42 35 浏览 0 评论

转载说明:原创不易,未经授权,谢绝任何形式的转载

用于自动化解决日常生活中的问题的脚本集合

在我们的日常工作中,我们需要做一些无聊且重复的任务,例如复制文件、移动文件、发送 Gmail、进行备份、翻译文本等等,但现在您可以将它们自动化并节省大部分宝贵时间。在这篇文章中,我将向您展示 10 个自动解决问题的 Python 脚本,所以您在等待什么,请添加书签并开始吧。

自动化不仅取代了人力,而且还取代了人力。这将放大人类的潜力

— Satya Nadella

1、发送邮件

需要自动化 Gmail 邮件发送,然后尝试使用此自动化脚本,该脚本使用 Python-Gmail 模块,该模块可让您向多个或单个收件人发送电子邮件。查看下面的代码。

  • 发送批量电子邮件
  • 在您的项目中使用
# Send Gmails
# pip install python-gmail
from gmail.gmail import gmail
def send_gmail(subj, mail, to):
    email = gmail()
    email_id = "xyz@gmail.com"
    email_pw = "xyz"
    email.login(email_id, email_pw)
    email.receiver_mail(to)
    email.send_mail(subj, mail)
    print("Mail sent successfully")
send_gmail("Test", "This is a test mail", "abc@gmail.com")

2、二维码生成

现在,您可以使用这个很棒的 Python 脚本创建您自己的 Qrcode,该脚本将允许您使用 Qrcode 模块生成自定义 Qr 代码。当您需要以 Qrcode 形式共享某些内容或共享文本数据时,此脚本非常有用。

# Generate a QrCode
# pip install qrcode
import qrcode as pyqr
def Qrcode_Maker(data):
    qr =pyqr.QRCode(version=1,box_size=10,border=5)
    qr.add_data(data)
    qr.make(fit=True)
    pic = qr.make_image()
    pic.save('qrcode.png')
Qrcode_Maker("https://www.medium.com/")

3、文字翻译器

想要翻译你的文本,然后尝试这个使用 Deep-translator 模块的 Python 脚本,它可以让你使用任何翻译器,在下面的例子中我使用谷歌翻译来翻译文本。

  • 自由翻译
  • 翻译成任何语言
  • 在翻译器中使用
  • 在您的项目中使用
# Text Translator
# pip install deep-translator
from deep_translator import GoogleTranslator
def Translate(text, lang):
    translated = GoogleTranslator(source='auto', target=lang)
    result = translated.translate(text)
    print(result)
Translate("Hello", "spanish")
Translate("Good day", "french")

4、通过代理请求

需要通过请求轮换您的 IP 地址,那么这里是适合您的脚本。下面是示例代码,您只需将 IP: 端口设置为 HTTP 和 HTTPS 格式即可。

# Request with Proxies
# pip install requests
import requests
proxies = {
    "http": "http://ip:port",
    "https": "http://ip:port",
}
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"
}
response = requests.get("https://httpbin.org/ip", proxies=proxies, headers=headers)
if response.status_code == 200:
    print(response.text)

5、在 Google 云端硬盘上备份

现在即可将文件备份上传到 Google 云端硬盘,轻松创建文件备份。此自动化脚本将向您展示如何使用 Pydrive2 模块,该模块可让您在 Google Drive 上上传、下载和删除文件。查看下面的代码。

  • 上传文件
  • 下载文件
  • 删除文件
  • 获取可共享链接
# Backup on Google Drive
# pip install pydrive2
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
# Connect to Google drive
auth = GoogleAuth()
auth.LocalWebserverAuth()
drive = GoogleDrive(auth)
# Upload the file to Google drive
uploader = drive.CreateFile({'title': 'combined_video.mp4'})
uploader.SetContentFile('combined_video.mp4')
uploader.Upload()
# Share the file with everyone
uploader.InsertPermission({
    'type': 'anyone',
    'value': 'anyone',
    'role': 'reader',
})
# Get the shareable link
print(uploader['alternateLink'])
# Download the file from Google drive
downloader = drive.CreateFile({'id': uploader['id']})
downloader.GetContentFile('combined_video.mp4')
# Delete the file from Google drive
downloader.Delete()

6、使用 Moviepy 进行编辑

此 Python 脚本将向您展示如何使用 Moviepy 模块自动进行视频编辑。该视频处理模块可让您修剪、调整屏幕大小、合并视频、添加音频等等。下面我提到了一个示例代码,您可以查看。

# Edit with Moviepy
# pip install moviepy
import moviepy.editor as mp
from moviepy.video.fx import crop, resize
# Load video
vid = mp.VideoFileClip("vid.mp4")
# resize screem
vid = resize(vid, width=426, height=720)
# Crop video
vid = crop(vid, x1=0, x2=426, y1=0, y2=720)
# Trim your video
trimmed = vid.subclip(0, 10)
# Merge your video
final_vid = mp.concatenate_videoclips([trimmed, trimmed])
# Add Audio
audio = mp.AudioFileClip("audio.mp3")
final_vid = final_vid.set_audio(audio)
# Add VFX 
final_vid = final_vid.fx(mp.vfx.invert_colors)
# Save your video
final_vid.write_videofile("final_vid.mp4")

7、React Python

想象一下使用 Python 很好地创建 React 应用程序吗?这可能会发生,因为此脚本将向您展示如何使用 Reactpy 模块以 FastApi 作为后端来创建美丽的 React 应用程序。查看下面的代码。

# React Python
# pip install reactpy
from reactpy import component, run, html
@component
def App():
    return html.div(
        html.h1("Hello World!"),
        html.p("This is a paragraph"),
        html.button("Click me!")
    )
run(App)

8、文件管理

使用下面的自动化脚本自动化您的文件处理工作。在代码下方,我添加了自动化日常工作所需的所有常见文件管理功能。

# Auto File Management
import os
import glob
import shutil as sh
# Copy File 
sh.copy("video1.mp4", "video1_copy.mp4")
# Move File
sh.move("video1.mp4", "video1_copy.mp4")
# Delete File
os.remove("video1.mp4")
# Rename File
os.rename("video1.mp4", "video1_copy.mp4")
# Get File Name
os.path.basename("video1.mp4")
# Get File Extension
os.path.splitext("video1.mp4")
# Create new Directory
os.mkdir("videos")
# Get Current Directory
os.getcwd()
# Delete Directory
os.rmdir("videos")
# Get List of Files in Directory
os.listdir("videos")
# Get specific files in Directory
print(glob.glob("*.mp4"))
# Get File Size
os.path.getsize("video1.mp4")
# Check if File exists
os.path.exists("video1.mp4")
# Check if Directory exists
os.path.isdir("videos")

9、货币汇率查询

如果您希望跟踪每日货币汇率,那么这里是适合您的脚本。该自动化脚本使用CurrencyConverter模块,它可以让您跟踪、转换,甚至获取任何货币的历史汇率数据。

  • 追踪货币
  • 获取最新汇率
  • 在您的项目中使用
  • 您还可以做更多的事情
# pip install CurrencyConverter
from currency_converter import CurrencyConverter
from datetime import date
pycurrency = CurrencyConverter()
# convert 1 usd to 1 gdp
print(pycurrency.convert(1, 'USD', 'GBP'))
# convert 1 usd to euro
print(pycurrency.convert(1, 'USD', 'EUR'))
# get the latest exchange rate
print(pycurrency.convert(1, 'USD', 'EUR', date=date.today()))
# convert 1 usd to gpb on 2019-01-01
print(pycurrency.convert(1, 'USD', 'GBP', date=date(2019, 1, 1)))
# convert 1 usd to gpb on 2019-01-01 using ECB rates
print(pycurrency.convert(1, 'USD', 'GBP', date=date(2019, 1, 1), source='ecb'))

10、获取 Google 自动完成建议

此自动化脚本将允许您获取 Google 自动完成建议。这个很酷的脚本使用请求模块,该模块接受查询并从 Google 搜索页面获取自动完成建议。请查看下面的代码。

# Fetch Google Autocomplete
# pip install requests
import requests
def fetch_autocomplete(query):
    url = f"http://suggestqueries.google.com/complete/search?output=firefox&q={query}"
    r = requests.get(url)
    suggestions = r.json()[1]
    print(suggestions)
# Example usage
query = "programming"
fetch_autocomplete(query)

结束

由于文章内容篇幅有限,今天的内容就分享到这里,文章结尾,我想提醒您,文章的创作不易,如果您喜欢我的分享,请别忘了点赞和转发,让更多有需要的人看到。同时,如果您想获取更多前端技术的知识,欢迎关注我,您的支持将是我分享最大的动力。我会持续输出更多内容,敬请期待。

相关推荐

阿里云国际站ECS:阿里云ECS如何提高网站的访问速度?

TG:@yunlaoda360引言:速度即体验,速度即业务在当今数字化的世界中,网站的访问速度已成为决定用户体验、用户留存乃至业务转化率的关键因素。页面加载每延迟一秒,都可能导致用户流失和收入损失。对...

高流量大并发Linux TCP性能调优_linux 高并发网络编程

其实主要是手里面的跑openvpn服务器。因为并没有明文禁p2p(哎……想想那么多流量好像不跑点p2p也跑不完),所以造成有的时候如果有比较多人跑BT的话,会造成VPN速度急剧下降。本文所面对的情况为...

性能测试100集(12)性能指标资源使用率

在性能测试中,资源使用率是评估系统硬件效率的关键指标,主要包括以下四类:#性能测试##性能压测策略##软件测试#1.CPU使用率定义:CPU处理任务的时间占比,计算公式为1-空闲时间/总...

Linux 服务器常见的性能调优_linux高性能服务端编程

一、Linux服务器性能调优第一步——先搞懂“看什么”很多人刚接触Linux性能调优时,总想着直接改配置,其实第一步该是“看清楚问题”。就像医生看病要先听诊,调优前得先知道服务器“哪里...

Nginx性能优化实战:手把手教你提升10倍性能!

关注△mikechen△,十余年BAT架构经验倾囊相授!Nginx是大型架构而核心,下面我重点详解Nginx性能@mikechen文章来源:mikechen.cc1.worker_processe...

高并发场景下,Spring Cloud Gateway如何抗住百万QPS?

关注△mikechen△,十余年BAT架构经验倾囊相授!大家好,我是mikechen。高并发场景下网关作为流量的入口非常重要,下面我重点详解SpringCloudGateway如何抗住百万性能@m...

Kubernetes 高并发处理实战(可落地案例 + 源码)

目标场景:对外提供HTTPAPI的微服务在短时间内收到大量请求(例如每秒数千至数万RPS),要求系统可弹性扩容、限流降级、缓存减压、稳定运行并能自动恢复。总体思路(多层防护):边缘层:云LB...

高并发场景下,Nginx如何扛住千万级请求?

Nginx是大型架构的必备中间件,下面我重点详解Nginx如何实现高并发@mikechen文章来源:mikechen.cc事件驱动模型Nginx采用事件驱动模型,这是Nginx高并发性能的基石。传统...

Spring Boot+Vue全栈开发实战,中文版高清PDF资源

SpringBoot+Vue全栈开发实战,中文高清PDF资源,需要的可以私我:)SpringBoot致力于简化开发配置并为企业级开发提供一系列非业务性功能,而Vue则采用数据驱动视图的方式将程序...

Docker-基础操作_docker基础实战教程二

一、镜像1、从仓库获取镜像搜索镜像:dockersearchimage_name搜索结果过滤:是否官方:dockersearch--filter="is-offical=true...

你有空吗?跟我一起搭个服务器好不好?

来人人都是产品经理【起点学院】,BAT实战派产品总监手把手系统带你学产品、学运营。昨天闲的没事的时候,随手翻了翻写过的文章,发现一个很严重的问题。就是大多数时间我都在滔滔不绝的讲理论,却很少有涉及动手...

部署你自己的 SaaS_saas如何部署

部署你自己的VPNOpenVPN——功能齐全的开源VPN解决方案。(DigitalOcean教程)dockovpn.io—无状态OpenVPNdockerized服务器,不需要持久存储。...

Docker Compose_dockercompose安装

DockerCompose概述DockerCompose是一个用来定义和管理多容器应用的工具,通过一个docker-compose.yml文件,用YAML格式描述服务、网络、卷等内容,...

京东T7架构师推出的电子版SpringBoot,从构建小系统到架构大系统

前言:Java的各种开发框架发展了很多年,影响了一代又一代的程序员,现在无论是程序员,还是架构师,使用这些开发框架都面临着两方面的挑战。一方面是要快速开发出系统,这就要求使用的开发框架尽量简单,无论...

Kubernetes (k8s) 入门学习指南_k8s kubeproxy

Kubernetes(k8s)入门学习指南一、什么是Kubernetes?为什么需要它?Kubernetes(k8s)是一个开源的容器编排系统,用于自动化部署、扩展和管理容器化应用程序。它...

取消回复欢迎 发表评论: