失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 10个提高工作效率的 Python 自动化脚本 值得收藏!

10个提高工作效率的 Python 自动化脚本 值得收藏!

时间:2023-01-03 07:28:23

相关推荐

10个提高工作效率的 Python 自动化脚本 值得收藏!

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

1、解析和提取 HTML

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

# Parse and Extract HTML# pip install gazpachoimport gazpacho# Extract HTML from URLurl = '/'html = gazpacho.get(url)print(html)# Extract HTML with Headersheaders = {'User-Agent': 'Mozilla/5.0'}html = gazpacho.get(url, headers=headers)print(html)# Parse HTMLparse = gazpacho.Soup(html)# Find single tagstag1 = parse.find('h1')tag2 = parse.find('span')# Find multiple tagstags1 = parse.find_all('p')tags2 = parse.find_all('a')# Find tags by classtag = parse.find('.class')# Find tags by Attributetag = parse.find("div", attrs={"class": "test"})# Extract text from tagstext = parse.find('h1').texttext = parse.find_all('p')[0].text

2、二维码扫描仪

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

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

3、截图

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

# Grab Screenshot# pip install pyautogui# pip install Pillowfrom pyautogui import screenshotimport timefrom PIL import ImageGrab# Grab Screenshot of Screendef grab_screenshot():shot = screenshot()shot.save('my_screenshot.png')# Grab Screenshot of Specific Areadef grab_screenshot_area():area = (0, 0, 500, 500)shot = ImageGrab.grab(area)shot.save('my_screenshot_area.png')# Grab Screenshot with Delaydef grab_screenshot_delay():time.sleep(5)shot = screenshot()shot.save('my_screenshot_delay.png')

4、创建有声读物

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

# Create Audiobooks# pip install gTTS# pip install PyPDF2from PyPDF2 import PdfFileReader as readerfrom gtts import gTTSdef 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')

5、PDF 编辑器

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

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

# PDF Editor# pip install PyPDf4import PyPDF4# Parse the Text from PDFdef parse_text(pdf_file):reader = PyPDF4.PdfFileReader(pdf_file)for page in reader.pages:print(page.extractText())# Remove Page from PDFdef 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 PDFdef 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 Pagesdef 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 PDFsdef 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)

6、迷你 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

7、自动化手机

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

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

# Automate Mobile Phones# pip install opencv-pythonimport subprocessdef main_adb(cm):p = subprocess.Popen(cm.split(' '), stdout=subprocess.PIPE, shell=True)(output, _) = municate()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 Clickingdef tap(x, y):cmd = 'adb shell input tap {} {}'.format(x, y)return main_adb(cmd)# Make a Calldef make_call(number):cmd = f"adb shell am start -a android.intent.action.CALL -d tel:{number}"return main_adb(cmd)# Send SMSdef 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 PCdef download_file(file_name):cmd = 'adb pull /sdcard/{}'.format(file_name)return main_adb(cmd)# Take a screenshotdef screenshot():cmd = 'adb shell screencap -p'return main_adb(cmd)# Power On and Offdef power_off():cmd = '"adb shell input keyevent 26"'return main_adb(cmd)

8、监控 CPU/GPU 温度

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

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

# Get CPU/GPU Temperature# pip install pythonnetimport clrclr.AddReference("OpenHardwareMonitorLib")from OpenHardwareMonitorLib import *spec = Computer()spec.GPUEnabled = Truespec.CPUEnabled = Truespec.Open()# Get CPU Tempdef 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 Tempdef 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))

9、Instagram 上传机器人

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

# Upload Photos and Video on Insta# pip install instabotfrom instabot import Botdef 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 moviepyfrom 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.durationOutput.write_videofile("output.mp4", fps=30, codec='libx264')

学会这10个在自动化办公脚本,能有效提高起码3倍的工作效率。

关于Python的学习指南

最后多说一句,小编是一名Python开发工程师,自己整理了一套最新的Python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。说真的,学好 Python 不论是就业、副业赚钱、还是提升学习、工作效率就业、副业赚钱、还是提升学习、工作效率,都是非常不错的选择,但要有一个系统的学习规划。

如果你是准备学习Python或者正在学习,下面这些你应该能用得上:

① Python所有方向的学习路线图,清楚各个方向要学什么东西

② 100多节Python课程视频,涵盖必备基础、爬虫和数据分析

③ 100多个Python实战案例,学习不再是只会理论

④ 华为出品独家Python漫画教程,手机也能学习

⑤ 历年互联网企业Python面试真题,复习时非常方便

上述这份完整版的Python全套学习资料已经上传CSDN官方,朋友们如果需要可以扫描下方二维码免费获取【保证100%免费】

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、全套PDF电子书

书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。

四、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

五、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

六、清华编程大佬出品《漫画看学Python》

用通俗易懂的漫画,来教你学习Python,让你更容易记住,并且不会枯燥乏味。

七、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

这份完整版的Python全套学习资料已经上传至CSDN官方,朋友们如果需要可以点击下方链接扫描下方二v码都可以免费获取【保证100%免费】

最新全套【Python入门到进阶资料 & 实战源码 &安装工具】

以上全套资料已经为大家打包准备好了,希望对正在学习Python的你有所帮助!

如果觉得《10个提高工作效率的 Python 自动化脚本 值得收藏!》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。