点击上方“菜学Python”,选择“星标”公众号

超级无敌干货,第一时间送达!!!

英文 | https://python.plainenglish.io/10-python-scripts-to-automate-your-daily-task-de1496fdf64a | Haider Imtiaz

翻译 | 杨小爱

在这个自动化时代,我们有很多重复无聊的工作要做。 想想这些你不再需要一次又一次地做的无聊的事情,让它自动化,让你的生活更轻松。 那么在本文中,我将向您介绍 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')

好书推荐

区别于市场上同类书,本书不但侧重于理论知识的普及,也将技术融合于Python模块进行实验上的操作与演示。本书主要内容包括:人工智能技术概述,人脸识别技术、物体识别技术,视频识别技术、语音识别技术、文本识别技术,区块链技术等。全书综合了各种模块对人工智能技术的实践,将分散的技术点统一起来,并把抽象的原理与适应读者思维的案例相融合,实现知识点的充分理解。本书适合从事数据科学及AI的读者阅读。

推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

年度爆款文案

  • 1).卧槽!Pdf转Word用Python轻松搞定!

  • 2).学Python真香!我用100行代码做了个网站,帮人PS旅行图片,赚个鸡腿吃

  • 3).首播过亿,火爆全网,我分析了《乘风破浪的姐姐》,发现了这些秘密

  • 4).80行代码!用Python做一个哆来A梦分身

  • 5).你必须掌握的20个python代码,短小精悍,用处无穷

  • 6).30个Python奇淫技巧集

  • 7).我总结的80页《菜鸟学Python精选干货.pdf》,都是干货

  • 8).再见Python!我要学Go了!2500字深度分析!

  • 9).发现一个舔狗福利!这个Python爬虫神器太爽了,自动下载妹子图片

点阅读原文,看B站我的视频!

10 个 Python 脚本来自动化你的日常任务相关推荐

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

    感谢您抽出 在这个自动化时代,我们有很多重复无聊的工作要做. 想想这些你不再需要一次又一次地做的无聊的事情,让它自动化,让你的生活更轻松. 那么在本文中,我将向您介绍 10 个 Python 自动化脚 ...

  2. python脚本——selenium自动化执行一些网页上的操作

    文章目录 一.说明 二.代码 三.用法总结 一.说明 通过python的selenium模块,自动化执行一些网页上的重复的无聊的工作. 二.代码 #! /usr/bin/python3.6 from ...

  3. 自动化办公python脚本_Python自动化办公

    在公司购买的OA系统上,很多功能都是软件商开发好的,如果有什么自定义的需求,也很难实现.现实情况下需要将一个工单的各类信息汇总整理为一份Excel,看似简单的需求,却需要在OA系统上反复点击多次,人工 ...

  4. python脚本实例手机端-Python脚本实现自动化Android手机apk安装实例

    #引入模块 importglobimporttimeimportos#定义全局变量 devices_list_finally =[] file_list_finally=[] chose_file_n ...

  5. 如何编写Metashape(Photoscan) python脚本

    Metashape,之前也叫做Photoscan,提供了python接口,可以使用python脚本来进行自动化处理.但是目前这方面的资料实在是比较少,可能是因为大部分人都习惯使用界面来进行操作吧,但其 ...

  6. 5个常见运维场景,用这几个Python脚本就够了

    许多运维工程师会使用 Python 脚本来自动化运维任务.Python 是一种流行的编程语言,具有丰富的第三方库和强大的自动化能力,适用于许多不同的领域. 在运维领域,Python 脚本可以用来实现各 ...

  7. 用于自动化的 10 个杀手级 Python 脚本

    用于自动化的 10 个杀手级 Python 脚本 您是否厌倦了在繁琐的任务上浪费时间? 您是否梦想着一个计算机为您完成所有工作的世界?别无所求,因为我们有 5 个 Python 脚本,准备好告别体力劳 ...

  8. python代替shell脚本_自动化shell脚本except与python的pexpect模块

    expect脚本 expect是什么 expect是一个免费的编程工具,用来实现自动的交互式任务,而无需人为干预.说白了,expect就是一套用来实现自动交互功能的软件. 在实际工作中,我们运行命令. ...

  9. 一起用Python做个自动化短视频生成脚本,实现热门视频流水线生产!

    前言 前几天有粉丝和我说,最近在网上看到一些视频营销号一天能发布几百条短视频, 感觉都是批量生成的,能不能用Python做个自动化短视频生成脚本呢? 今天就带大家一起用Python做个自动化视频生成脚 ...

最新文章

  1. 千万别让这些举动断送了你的职业前程-好文共分享
  2. 一次关于 Mysql 索引优化的思考
  3. FreeRTOS学习及移植笔记之二:在IAR和STM32F103VET上移植FreeRTOS
  4. canvas 绘制直线 并选中_javascript自学记录:canvas绘图
  5. Spring与SpringMVC的区别
  6. “fatal error C1010”错误解决的三种方法
  7. 洛谷——P1089 [NOIP2004 提高组] 津津的储蓄计划
  8. Ps 初学者教程,如何用文字增强您的照片?
  9. sha256算法细节详解
  10. 金融量化之华泰多因子估值类显著性和IC值计算
  11. 详解CAN 2.0协议
  12. bandwidth看内存带宽性能
  13. 计算机搜索栏历史记录,如何打开搜索历史记录
  14. IT时代,不懂编程,到底能不能学前端 ,自己做网站
  15. 另类搞笑:自我指涉例句不完全收集
  16. Me安装教程(同pr)
  17. C语言数组相似度比对,C语言实验报告:碱基相似度比较
  18. 安卓手机能提取当前页面的链接吗_如何获取一个app内的网页地址?
  19. opencv生成3d模型_OpenCV4.2使用viz模块显示3D图像
  20. java.lang.RuntimeException: Parcel: unable to marshal value com.

热门文章

  1. 互动媒体技术-用p5.js临摹动态图片
  2. 【JavaWeb—HTML标签】
  3. 四维全息算法--一把动态的标准尺
  4. 定时任务-------摸鱼王的日常问题
  5. Revit二次开发之创建房间,根据房间边界创建楼板等
  6. 最新版手机软件App下载排行网站源码/App应用商店源码
  7. ANSYS编程语言APDL的编程经验总结
  8. ROS教程1:安装和配置ROS环境
  9. 就业技术书文件表格_就业协议书填写、盖章注意事项
  10. 煽情的儿子535=随笔