分享五个方便好用的Python自动化脚本

这次我们使用Python来实现几个自动化场景,或许可以用到你的工作中。

1、自动化阅读网页新闻

这个脚本能够实现从网页中抓取文本,然后自动化语音朗读,当你想听新闻的时候,这是个不错的选择。

代码分为两大部分,第一通过爬虫抓取网页文本呢,第二通过阅读工具来朗读文本。

需要的第三方库:

Beautiful Soup - 经典的HTML/XML文本解析器,用来提取爬下来的网页信息

requests - 好用到逆天的HTTP工具,用来向网页发送请求获取数据

Pyttsx3 - 将文本转换为语音,并控制速率、频率和语音

import pyttsx3

import requests

from bs4 import BeautifulSoup

engine = pyttsx3.init(‘sapi5’)

voices = engine.getProperty(‘voices’)

newVoiceRate = 130 ## Reduce The Speech Rate

engine.setProperty(‘rate’,newVoiceRate)

engine.setProperty(‘voice’, voices[1].id)

def speak(audio):

engine.say(audio)

engine.runAndWait()

text = str(input(“Paste article\n”))

res = requests.get(text)

soup = BeautifulSoup(res.text,‘html.parser’)

articles = []

for i in range(len(soup.select(‘.p’))):

article = soup.select(‘.p’)[i].getText().strip()

articles.append(article)

text = " ".join(articles)

speak(text)

engine.save_to_file(text, ‘test.mp3’) ## If you want to save the speech as a audio file

engine.runAndWait()

2、自动生成素描草图

这个脚本可以把彩色图片转化为铅笔素描草图,对人像、景色都有很好的效果。

而且只需几行代码就可以一键生成,适合批量操作,非常的快捷。

需要的第三方库:

Opencv - 计算机视觉工具,可以实现多元化的图像视频处理,有Python接口

“”" Photo Sketching Using Python “”"

import cv2

img = cv2.imread(“elon.jpg”)

Image to Gray Image

gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

Gray Image to Inverted Gray Image

inverted_gray_image = 255-gray_image

Blurring The Inverted Gray Image

blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19,19),0)

Inverting the blurred image

inverted_blurred_image = 255-blurred_inverted_gray_image

Preparing Photo sketching

sketck = cv2.divide(gray_image, inverted_blurred_image,scale= 256.0)

cv2.imshow(“Original Image”,img)

cv2.imshow(“Pencil Sketch”, sketck)

cv2.waitKey(0)

3、自动发送多封邮件

这个脚本可以帮助我们批量定时发送邮件,邮件内容、附件也可以自定义调整,非常的实用。

相比较邮件客户端,Python脚本的优点在于可以智能、批量、高定制化地部署邮件服务。

需要的第三方库:

Email - 用于管理电子邮件消息

Smtlib - 向SMTP服务器发送电子邮件,它定义了一个 SMTP 客户端会话对象,该对象可将邮件发送到互联网上任何带有 SMTP 或 ESMTP 监听程序的计算机

Pandas - 用于数据分析清洗地工具

import smtplib

from email.message import EmailMessage

import pandas as pd

def send_email(remail, rsubject, rcontent):

email = EmailMessage() ## Creating a object for EmailMessage

email[‘from’] = ‘The Pythoneer Here’ ## Person who is sending

email[‘to’] = remail ## Whom we are sending

email[‘subject’] = rsubject ## Subject of email

email.set_content(rcontent) ## content of email

with smtplib.SMTP(host=‘smtp.gmail.com’,port=587)as smtp:

smtp.ehlo() ## server object

smtp.starttls() ## used to send data between server and client

smtp.login(“deltadelta371@gmail.com”,“delta@371”) ## login id and password of gmail

smtp.send_message(email) ## Sending email

print("email send to ",remail) ## Printing success message

if name == ‘main’:

df = pd.read_excel(‘list.xlsx’)

length = len(df)+1

for index, item in df.iterrows():

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

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

二、学习软件

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

三、入门学习视频

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

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 13
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值