转载说明:原创不易,未经授权,谢绝任何形式的转载
用于自动化解决日常生活中的问题的脚本集合
在我们的日常工作中,我们需要做一些无聊且重复的任务,例如复制文件、移动文件、发送 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)
结束
由于文章内容篇幅有限,今天的内容就分享到这里,文章结尾,我想提醒您,文章的创作不易,如果您喜欢我的分享,请别忘了点赞和转发,让更多有需要的人看到。同时,如果您想获取更多前端技术的知识,欢迎关注我,您的支持将是我分享最大的动力。我会持续输出更多内容,敬请期待。