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"))

技术交流&材料获取

技术要学会分享、交流,不建议闭门造车。一个人可以走的很快、一堆人可以走的更远。

资料干货、资料分享、数据、技术交流提升,均可加交流群获取,群友已超过2000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友。

方式①、添加微信号:dkl88194,备注:来自CSDN + 资料
方式②、微信搜索公众号:Python学习与数据挖掘,后台回复: 资料

1、数据分析实战宝典
在这里插入图片描述

2、100个超强算法模型

我们打造了《100个超强算法模型》,特点:从0到1轻松学习,原理、代码、案例应有尽有,所有的算法模型都是按照这样的节奏进行表述,所以是一套完完整整的案例库。

很多初学者是有这么一个痛点,就是案例,案例的完整性直接影响同学的兴致。因此,我整理了 100个最常见的算法模型,在你的学习路上助推一把!

在这里插入图片描述

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')

最后的想法

希望你能找到一些新的有趣的东西来让你的日常任务自动化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/249695.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

在Mac系统下为SpringBoot 配置Maven 【避坑完整版】

前提背景 电脑罢工,操作系统重装,不仅有大量的软件需要安装,还有很多开发环境需要配置。 就在今天配置Maven的时候各种坑,写下来供大家参考。 一、在讨论安装Maven前先安装一下JDK,方式很多,我这里有个比较快的办法&am…

gitee(ssh)同步本地

一、什么是码云 gitee Git的”廉价平替” > 服务器在国内,运行不费劲 在国内也形成了一定的规模 git上的一些项目插件等在码云上也可以找得到 二、创建仓库 三、删除仓库 四、仓库与本地同步 > 建立公钥 五、把仓库同步到本地 六、在本地仓库中创建vue项目…

Docker 部署 Lobe Chat 服务

拉取最新版本的 Lobe Chat 镜像: $ sudo docker pull lobehub/lobe-chat:latest使用以下命令来运行 Lobe Chat 容器: $ sudo docker run -d --name lobe-chat -p 10084:3210 -e OPENAI_API_KEYsk-xxxx -e OPENAI_PROXY_URLhttps://api.openai.com/v1 -e ACCESS_CO…

C之switch小问题

执行结果: 为什么会是100呢? 因为C语言会忽视 switch语句与第一个case之间的code,也就是根本不会执行 “num100;

谷歌Gemini API 应用(二):LangChain 加持

昨天我完成了谷歌Gemini API 应用(一):基础应用这篇博客,今天我们要在此基础上实现Gemini模型的Langchian加持,因为Gemini API刚发布没几天,所以langchian还没有来得及将其整合到现有的langchain包的架构内,langchain公…

【SpringBoot篇】Interceptor拦截器 | 拦截器和过滤器的区别

文章目录 🌹概念⭐作用 🎄快速入门⭐入门案例代码实现 🛸拦截路径🍔拦截器interceptor和过滤器filter的区别🎆登录校验 🌹概念 拦截器(Interceptor)是一种软件设计模式,…

Docker构建镜像时空间不足:/var/lib/docker,no space left on device

背景 在一次更新业务服务功能后,重新在服务器上构建微服务镜像,在构建镜像时报错空间不足: /var/lib/docker, no space left on device 赶紧用 df -h 看了下磁盘使用情况,果然, devicemapper 已经满了。。由于需要紧急…

C语言代码实现URL编码

在 Python,只需要导入 urllib.parse,然后使用 quote 函数即可把任意字符串进行 URL 编码 现在使用 C 语言来实现等效的代码,我在网上找到现成的代码,改代码接收命令行输入参数,然后进行 URL 编码并输出: #…

Flask基本用法:一个HelloWorld,搭建服务、发起请求

目录 1、简介 2、安装 3、Flask使用示例 参考 1、简介 官网文档 Flask是一个轻量的web服务框架,我们可以利用它快速搭建一个服务,对外提供接口,其他人可以轻松调用我们的服务。这对算法工程师来说比较关键,我们通常不擅长搞开发…

【Docker四】使用Docker-compose一键部署Wordpress平台

目录 一、YAML 文件格式及编写注意事项(重要) 1、yaml文件使用时注意事项: 2、yaml文件的基本数据结构: 2.1、声明变量(标量。是单个的不可再分的值,类型:字符串,整数&#xff0c…

Typescript中Omit数据类型的理解

在 TypeScript 中&#xff0c;Omit 是一个内置的工具类型&#xff0c;它用于从对象类型中排除指定的属性&#xff0c;并返回剩余的属性。 Omit 的语法如下所示&#xff1a; type Omit<T, K> Pick<T, Exclude<keyof T, K>>;其中&#xff0c;T 表示原始类型…

强制性产品认证车辆一致性证书二维码解析

目录 说明 界面 下载 强制性产品认证车辆一致性证书二维码解析 说明 二维码扫描出的信息为&#xff1a; qW0qS6aFjU50pMOqis0WupBnM21DnMxy0dGFN/2Mc9gENXhKh0qEBxFgfXSLoR qW0qS6aFjU50pMOqis0WupBnM21DnMxy0dGFN/2Mc9gENXhKh0qEBxFgfXSLoR 解析后的信息为&#xff1a…

佛山IBM System x3550 M4服务器维修检查

案例背景&#xff1a; 一家位于东莞的制造公司&#xff0c;在其佛山分厂中安装了一台IBM X3550 M4服务器作为其关键业务设备。该服务器负责管理和存储公司的生产数据、ERP系统和供应链数据。在生产过程中&#xff0c;该服务器突然发生了故障&#xff0c;导致佛山分厂的生产中断…

深度学习环境配置

一、Anaconda安装 下载&#xff1a;从清华大学开源软件镜像下载 镜像网址 出现base即为安装成功&#xff1a; 检查显卡的驱动是否正确安装&#xff1a; &#xff08;GPU可以显示出名称&#xff09; GPU0是集显集成显卡是主板自带的显卡。 GPU1是独显即独立显卡&#xff0c…

大数据组件:Hadoop

文章目录 1、Hadoop 是什么2、Hadoop 优势3、Hadoop 组成&#xff08;1&#xff09;HDFS&#xff08;2&#xff09;YARN&#xff08;3&#xff09;MapReduce 架构概述&#xff08;4&#xff09;HDFS、YARN、MapReduce 三者关系&#xff08;5&#xff09;大数据技术生态体系&…

【Java】图片资源转为Base64编码并返回

使用JDK把图片资源转为Base64编码并返回(免费分享&#xff0c;皆可复制粘贴) 在Java实际应用开发过程中&#xff0c;我们需要使用指定的背景图案&#xff0c;例如大型游戏中的一些基本图案&#xff0c;例如礼物、场景、武器造型等等&#xff0c;通俗来说就是图片源文件&#x…

Ubuntu系统的基础操作和使用

#ubuntuUbuntu系统的基础操作和使用包括以下几个方面&#xff1a; 1. 安装和启动&#xff1a;首先&#xff0c;需要下载Ubuntu镜像文件并使用虚拟机软件&#xff08;如VirtualBox&#xff09;创建虚拟机&#xff0c;将镜像文件安装在虚拟机中即可启动Ubuntu系统。 2. 桌面环境…

将开源免费进行到底,ThreadX开源电脑端GUIBuilder图形开发工具GUIX Studio

上个月微软刚刚宣布将ThreadX RTOS全家桶贡献给Eclipse基金会&#xff0c;免费供大家商用&#xff0c;宽松的MIT授权方式&#xff0c;就差这个GUIX Studio没有开源了&#xff0c;而且Windows还经常检索不到&#xff0c;并且也不提供离线包。 1、软件包有点大&#xff0c;700MB…

初识Pandas函数是Python的一个库(继续更新...)

学习网页&#xff1a; Welcome to Python.orghttps://www.python.org/https://www.python.org/https://www.python.org/ Pandas函数库 Pandas是一个Python库&#xff0c;提供了大量的数据结构和数据分析工具&#xff0c;包括DataFrame和Series等。Pandas的函数非常丰富&…