10个Python脚本来自动化你的日常任务

这篇具有很好参考价值的文章主要介绍了10个Python脚本来自动化你的日常任务。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在这个自动化时代,我们有很多重复无聊的工作要做。想想这些你不再需要一次又一次地做的无聊的事情,让它自动化,让你的生活更轻松。那么在本文中,我将向您介绍 10 个 Python 自动化脚本,以使你的工作更加自动化,生活更加轻松。因此,没有更多的重复任务将这篇文章放在您的列表中,让我们开始吧。

01、解析和提取 HTML

此自动化脚本将帮助你从网页 URL 中提取 HTML,然后还为你提供可用于解析 HTML 以获取数据的功能。这个很棒的脚本对于网络爬虫和那些想要解析 HTML 以获取重要数据的人来说是一种很好的享受。

# Parse and Extract HTML
# pip install gazpacho
import gazpacho
# Extract HTML from URL
url = 'https://www.example.com/'
html = gazpacho.get(url)
print(html)
# Extract HTML with Headers
headers = {'User-Agent': 'Mozilla/5.0'}
html = gazpacho.get(url, headers=headers)
print(html)
# Parse HTML
parse = gazpacho.Soup(html)
# Find single tags
tag1 = parse.find('h1')
tag2 = parse.find('span')
# Find multiple tags
tags1 = parse.find_all('p')
tags2 = parse.find_all('a')
# Find tags by class
tag = parse.find('.class')
# Find tags by Attribute
tag = parse.find("div", attrs={"class": "test"})
# Extract text from tags
text = parse.find('h1').text
text = parse.find_all('p')[0].text

02、二维码扫描仪

拥有大量二维码图像或只想扫描二维码图像,那么此自动化脚本将帮助你。该脚本使用 Qrtools 模块,使你能够以编程方式扫描 QR 图像。

# Qrcode Scanner
# pip install qrtools
from qrtools import Qr
def Scan_Qr(qr_img):
    qr = Qr()
    qr.decode(qr_img)
    print(qr.data)
    return qr.data
print("Your Qr Code is: ", Scan_Qr("qr.png"))

03、截图

现在,你可以使用下面这个很棒的脚本以编程方式截取屏幕截图。使用此脚本,你可以直接截屏或截取特定区域的屏幕截图。

# Grab Screenshot
# pip install pyautogui
# pip install Pillow
from pyautogui import screenshot
import time
from PIL import ImageGrab
# Grab Screenshot of Screen
def grab_screenshot():
    shot = screenshot()
    shot.save('my_screenshot.png')
# Grab Screenshot of Specific Area
def grab_screenshot_area():
    area = (0, 0, 500, 500)
    shot = ImageGrab.grab(area)
    shot.save('my_screenshot_area.png')
# Grab Screenshot with Delay
def grab_screenshot_delay():
    time.sleep(5)
    shot = screenshot()
    shot.save('my_screenshot_delay.png')

04、创建有声读物

厌倦了手动将您的 PDF 书籍转换为有声读物,那么这是你的自动化脚本,它使用 GTTS 模块将你的 PDF 文本转换为音频。

# Create Audiobooks
# pip install gTTS
# pip install PyPDF2
from PyPDF2 import PdfFileReader as reader
from gtts import gTTS
def create_audio(pdf_file):
    read_Pdf = reader(open(pdf_file, 'rb'))
    for page in range(read_Pdf.numPages):
        text = read_Pdf.getPage(page).extractText()
        tts = gTTS(text, lang='en')
        tts.save('page' + str(page) + '.mp3')
create_audio('book.pdf')

05、PDF 编辑器

使用以下自动化脚本使用 Python 编辑 PDF 文件。该脚本使用 PyPDF4 模块,它是 PyPDF2 的升级版本,下面我编写了 Parse Text、Remove pages 等常用功能。

当你有大量 PDF 文件要编辑或需要以编程方式在 Python 项目中使用脚本时,这是一个方便的脚本。

# PDF Editor
# pip install PyPDf4
import PyPDF4
# Parse the Text from PDF
def parse_text(pdf_file):
    reader = PyPDF4.PdfFileReader(pdf_file)
    for page in reader.pages:
        print(page.extractText())
# Remove Page from PDF
def remove_page(pdf_file, page_numbers):
    filer = PyPDF4.PdfReader('source.pdf', 'rb')
    out = PyPDF4.PdfWriter()
    for index in page_numbers:
        page = filer.pages[index] 
        out.add_page(page)
with open('rm.pdf', 'wb') as f:
        out.write(f)
# Add Blank Page to PDF
def add_page(pdf_file, page_number):
    reader = PyPDF4.PdfFileReader(pdf_file)
    writer = PyPDF4.PdfWriter()
    writer.addPage()
    with open('add.pdf', 'wb') as f:
        writer.write(f)
# Rotate Pages
def rotate_page(pdf_file):
    reader = PyPDF4.PdfFileReader(pdf_file)
    writer = PyPDF4.PdfWriter()
    for page in reader.pages:
        page.rotateClockwise(90)
        writer.addPage(page)
    with open('rotate.pdf', 'wb') as f:
        writer.write(f)
# Merge PDFs
def merge_pdfs(pdf_file1, pdf_file2):
    pdf1 = PyPDF4.PdfFileReader(pdf_file1)
    pdf2 = PyPDF4.PdfFileReader(pdf_file2)
    writer = PyPDF4.PdfWriter()
    for page in pdf1.pages:
        writer.addPage(page)
    for page in pdf2.pages:
        writer.addPage(page)
    with open('merge.pdf', 'wb') as f:
        writer.write(f)

06、迷你 Stackoverflow

作为一名程序员,我知道我们每天都需要 StackOverflow,但你不再需要在 Google 上搜索它。现在,在您继续处理项目的同时,在你的 CMD 中获得直接解决方案。通过使用 Howdoi 模块,你可以在命令提示符或终端中获得 StackOverflow 解决方案。你可以在下面找到一些可以尝试的示例。

# Automate Stackoverflow
# pip install howdoi
# Get Answers in CMD
#example 1
> howdoi how do i install python3
# example 2
> howdoi selenium Enter keys
# example 3
> howdoi how to install modules
# example 4
> howdoi Parse html with python
# example 5
> howdoi int not iterable error
# example 6
> howdoi how to parse pdf with python
# example 7
> howdoi Sort list in python
# example 8
> howdoi merge two lists in python
# example 9
>howdoi get last element in list python
# example 10
> howdoi fast way to sort list

07、自动化手机

此自动化脚本将帮助你使用 Python 中的 Android 调试桥 (ADB) 自动化你的智能手机。下面我将展示如何自动执行常见任务,例如滑动手势、呼叫、发送短信等等。

您可以了解有关 ADB 的更多信息,并探索更多令人兴奋的方法来实现手机自动化,让您的生活更轻松。

# Automate Mobile Phones
# pip install opencv-python
import subprocess
def main_adb(cm):
    p = subprocess.Popen(cm.split(' '), stdout=subprocess.PIPE, shell=True)
    (output, _) = p.communicate()
    return output.decode('utf-8')
# Swipe 
def swipe(x1, y1, x2, y2, duration):
    cmd = 'adb shell input swipe {} {} {} {} {}'.format(x1, y1, x2, y2, duration)
    return main_adb(cmd)
# Tap or Clicking
def tap(x, y):
    cmd = 'adb shell input tap {} {}'.format(x, y)
    return main_adb(cmd)
# Make a Call
def make_call(number):
    cmd = f"adb shell am start -a android.intent.action.CALL -d tel:{number}"
    return main_adb(cmd)
# Send SMS
def send_sms(number, message):
    cmd = 'adb shell am start -a android.intent.action.SENDTO -d  sms:{} --es sms_body "{}"'.format(number, message)
    return main_adb(cmd)
# Download File From Mobile to PC
def download_file(file_name):
    cmd = 'adb pull /sdcard/{}'.format(file_name)
    return main_adb(cmd)
# Take a screenshot
def screenshot():
    cmd = 'adb shell screencap -p'
    return main_adb(cmd)
# Power On and Off
def power_off():
    cmd = '"adb shell input keyevent 26"'
    return main_adb(cmd)

08、监控 CPU/GPU 温度

你可能使用 CPU-Z 或任何规格监控软件来捕获你的 Cpu 和 Gpu 温度,但你也可以通过编程方式进行。好吧,这个脚本使用 Pythonnet 和 OpenhardwareMonitor 来帮助你监控当前的 Cpu 和 Gpu 温度。

你可以使用它在达到一定温度时通知自己,也可以在 Python 项目中使用它来简化日常生活。

# Get CPU/GPU Temperature
# pip install pythonnet
import clr
clr.AddReference("OpenHardwareMonitorLib")
from OpenHardwareMonitorLib import *
spec = Computer()
spec.GPUEnabled = True
spec.CPUEnabled = True
spec.Open()
# Get CPU Temp
def Cpu_Temp():
    while True:
        for cpu in range(0, len(spec.Hardware[0].Sensors)):
            if "/temperature" in str(spec.Hardware[0].Sensors[cpu].Identifier):
                print(str(spec.Hardware[0].Sensors[cpu].Value))
# Get GPU Temp
def Gpu_Temp()
    while True:
        for gpu in range(0, len(spec.Hardware[0].Sensors)):
            if "/temperature" in str(spec.Hardware[0].Sensors[gpu].Identifier):
                print(str(spec.Hardware[0].Sensors[gpu].Value))

09、Instagram 上传机器人

Instagram 是一个著名的社交媒体平台,你现在不需要通过智能手机上传照片或视频。你可以使用以下脚本以编程方式执行此操作。

# Upload Photos and Video on Insta
# pip install instabot
from instabot import Bot
def Upload_Photo(img):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_photo(img, caption="Medium Article")
    print("Photo Uploaded")
def Upload_Video(video):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_video(video, caption="Medium Article")
    print("Video Uploaded")
def Upload_Story(img):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_story(img, caption="Medium Article")
    print("Story Photos Uploaded")
Upload_Photo("img.jpg")
Upload_Video("video.mp4")

10、视频水印

使用此自动化脚本为你的视频添加水印,该脚本使用 Moviepy,这是一个方便的视频编辑模块。在下面的脚本中,你可以看到如何添加水印并且可以自由使用它。

# Video Watermark with Python
# pip install moviepy
from moviepy.editor import *
clip = VideoFileClip("myvideo.mp4", audio=True) 
width,height = clip.size  
text = TextClip("WaterMark", font='Arial', color='white', fontsize=28)
set_color = text.on_color(size=(clip.w + text.w, text.h-10), color=(0,0,0), pos=(6,'center'), col_opacity=0.6)
set_textPos = set_color.set_pos( lambda pos: (max(width/30,int(width-0.5* width* pos)),max(5*height/6,int(100* pos))) )
Output = CompositeVideoClip([clip, set_textPos])
Output.duration = clip.duration
Output.write_videofile("output.mp4", fps=30, codec='libx264')

更多精彩教程欢迎B站搜索“千锋教育”

千锋教育Python全套视频教程,轻松掌握Excel、Word、PPT、邮件、爬虫、office办公自动化(宋如宁主讲)文章来源地址https://www.toymoban.com/news/detail-803539.html

到了这里,关于10个Python脚本来自动化你的日常任务的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 10个Python自动化脚本助你工作更加高效

    所以,请你把这篇文章放在你的收藏清单上,以备不时之需,在IT行业里,程序员的学习永无止境…… 现在,让我们开始吧。   使用这个很棒的自动化脚本,可以帮助把图像处理的更好,你可以像在 Photoshop 中一样编辑它们。 该脚本使用流行的是 Pillow 模块 通过使用以下自动

    2024年02月06日
    浏览(40)
  • Ansible自动化:简化你的运维任务

    🌷🍁 博主猫头虎(🐅🐾)带您 Go to New World✨🍁 🦄 博客首页 ——🐅🐾猫头虎的博客🎐 🐳 《面试题大全专栏》 🦕 文章图文并茂🦖生动形象🐅简单易学!欢迎大家来踩踩~🌺 🌊 《IDEA开发秘籍专栏》 🐾 学会IDEA常用操作,工作效率翻倍~💐 🌊 《100天精通Golang(基础

    2024年02月07日
    浏览(36)
  • Shell脚本入门实战:探索自动化任务与实用场景

    引言 Shell脚本作为一种强大的自动化工具,在现代操作系统中具有广泛的应用。无论是简单的文件操作,还是复杂的系统管理,Shell脚本都能提供高效、快速的解决方案。在本文中,我们将探索Shell脚本的基础知识,并通过实战场景示例,一起深入了解Shell脚本编程。 1. Shell脚

    2024年02月02日
    浏览(33)
  • 【速看】如何通过合理的封装,让你的自动化脚本更上一层楼!

    此文章来源于项目官方公众号:“AirtestProject” 版权声明:允许转载,但转载必须保留原链接;请勿用作商业或者非法用途 上一篇推文利用一个在图片范围内实现随机坐标点击的例子,去教会大家如何将自己想要的效果实现出来,受到大家的热情反响,在我们官方讨论群中,

    2024年02月08日
    浏览(36)
  • 发掘Win10神奇工具:计划任务程序的自动化魔力

    在Windows 10系统中,隐藏着许多不为人知的神奇工具,您了解多少呢?想象一下,如果有一种工具,能够像机器人一样在您设定的时间自动执行各种任务,您会不会觉得它是一件非常实用的利器?今天,我将向大家介绍一个名为“计划任务程序”的神奇工具,它可以让您的计算

    2024年02月12日
    浏览(28)
  • [Python自动化]使用Python Pexpect模块实现自动化交互脚本使用心得

    参考文档:https://pexpect.readthedocs.io/en/stable/ 在最近的工作中,需要使用DockerFile构建镜像。在构建镜像的过程中,有一些执行的命令是需要交互的。例如安装 tzdata (apt install tzdata),不过在使用apt安装时,可以直接使用 DEBIAN_FRONTEND=noninteractive 前缀来取消交互(至于是禁止交互还

    2023年04月25日
    浏览(37)
  • Python自动化测试之登录脚本

    前提已经安装好python、pycharm,配置了对应的环境变量。 文件–设置—项目:script----python解释器----+selenium 以谷歌浏览器为例 下载地址:https://chromedriver.chromium.org/downloads (1)先查看谷歌浏览器版本; (2)下载类似版本号的.zip,解压到pyhton环境目录下 (也可以下载到pycharm下

    2024年02月05日
    浏览(43)
  • python医院自动化抢号脚本

    挂号自动化脚本思路 1.登录华西医院网页 。 2.自动登录,向手机发送验证码,等待输入后登录 3.进入倒计时 4.到时进入医生主页 5.确定预约进入预约界面 6.选择健康卡并获取图形码 7.利用ddddocr包识别图形码并输入 8.最后确认并结束 9.完整代码链接如下 python医院挂号自动化脚

    2024年02月11日
    浏览(33)
  • python自动化办公--文件整理脚本详解

    今天讲解文件整理脚本的实现过程。这是一个很有用的技能,可以帮助你管理你的电脑上的各种文件。需求如下: 需求内容:给定一个打算整理的文件夹目录,这个脚本可以将该目录下的所有文件都揪出来,并且根据后缀名归类到不同的文件夹里。 python能力:使用python的内

    2024年02月10日
    浏览(58)
  • selenium(4)-------自动化测试脚本(python)

    webdriverAPI 一)定位元素的方式,必问 1.1)id来定位元素,前提是元素必须具有id属性,因为有的元素是没有id的 1.2)name,元素必须有name,并且必须全局唯一 1.3)tagname,元素是一定有的,但是必须全局唯一才可以定位到元素 1.4)classname,class的名字,必须全局唯一 1.5)link_text,通过

    2024年02月02日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包