【python起床服务】播放起床铃+给老板发邮件辞职
·
【python起床服务】
播放起床铃+给老板发邮件辞职
之前看师兄做过不关闹钟删本地文件的,我胆不够肥,浅浅给老板辞个职。(开玩笑啦)假期有空来动手实现一下,没想到花了一下午,主要问题出在本人对 python 不熟练。还有用到一些没碰过的 module,但是没关系,最后都实现了。BTW 这个真挺好玩的。
pyttsx3 播放音频
1. 播放 mp3 文件
- 遇到的问题
一开始我用了本地的 mp3 文件但是播放失败。 - 解决方案
后来发现因为编码的关系,qq 音乐和网易云下载的音频才可以播放。
2. 人声读文本
- 遇到的问题
我的文本是粤语的,但是系统的语音包只有普通话和英文的。 - 解决方案
因此我得再去下载一份 hk 的语音包,在设置直接找就可以啦。
测试系统的语音包
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print(voice)

可以看见,此时没有粤语的语音包。咱们去下一个

解决了~ 语音包的编号的话,我这里是新下载的,因此编号是 2(从 0 开始)
播放音频的代码
def alarm():
pp = pyttsx3.init()
voices = pp.getProperty('voices')
pp.setProperty('voice', voices[2].id)
alarm_time = input("输入要设置的闹钟时间:HH:MM:SS\n")
alarm_period = input("请输入要设置的时期(AM或PM):\n")
# 获取小时
alarm_hour = alarm_time[0:2]
# 获取分钟
alarm_minute = alarm_time[3:5]
# 多少秒
alarm_seconds = alarm_time[6:8]
alarm_period = alarm_period.upper()
print("设置成功正在运行,祝您休息愉快....zzZZ..")
flag = True
while flag:
now = datetime.now()
current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")
if(alarm_period == current_period):
if(alarm_hour == current_hour):
if(alarm_minute == current_minute):
if(alarm_seconds == current_seconds):
print("Wake Up!!!")
# 铃声名字最好不要带中文,会节码失败
playsound('C:\\Users\\30675\\Desktop\\1.mp3')
# # 延迟一分钟在叫一次
time.sleep(60)
print('时间是 ')
print(int(now.strftime("%M"))-int(alarm_minute))
playsound('C:\\Users\\30675\\Desktop\\1.mp3')
# 再给一分钟机会
time.sleep(60)
pp.say('快啲起身,我宜家去發email,俾人知你咁懒!')
pp.runAndWait()
return 1
return 1
发邮件辞职
开玩笑啦,我这里就自己发给自己啦
1. 发送方邮箱开启smtp服务
网页版登录 qq 邮箱,在 设置 -> 账户里面

发邮件的代码
def mail_qq():
msg_from = 'xxx@qq.com' # 发送方邮箱
passwd = 'xxx'
# 就是上面的授权码
to = ['xxx@qq.com']
# 接受方邮箱
# 设置邮件内容
# MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent = "测试"
# 把内容加进去
msg.attach(MIMEText(conntent, 'plain', 'utf-8'))
# 设置邮件主题
msg['Subject'] = "这个是邮件主题"
# 发送方信息
msg['From'] = msg_from
# 开始发送
# 通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
# 开始发送
s.sendmail(msg_from, to, msg.as_string())
print("邮件发送成功")
所有代码
import time
from datetime import datetime
from playsound import playsound
import pyttsx3
import smtplib
from email.mime.text import MIMEText
# 发送多种类型的邮件
from email.mime.multipart import MIMEMultipart
def alarm():
pp = pyttsx3.init()
voices = pp.getProperty('voices')
pp.setProperty('voice', voices[2].id)
alarm_time = input("输入要设置的闹钟时间:HH:MM:SS\n")
alarm_period = input("请输入要设置的时期(AM或PM):\n")
# 获取小时
alarm_hour = alarm_time[0:2]
# 获取分钟
alarm_minute = alarm_time[3:5]
# 多少秒
alarm_seconds = alarm_time[6:8]
alarm_period = alarm_period.upper()
print("设置成功正在运行,祝您休息愉快....zzZZ..")
flag = True
while flag:
now = datetime.now()
current_hour = now.strftime("%I")
current_minute = now.strftime("%M")
current_seconds = now.strftime("%S")
current_period = now.strftime("%p")
if(alarm_period == current_period):
if(alarm_hour == current_hour):
if(alarm_minute == current_minute):
if(alarm_seconds == current_seconds):
print("Wake Up!!!")
# 铃声名字最好不要带中文,会节码失败
playsound('C:\\Users\\30675\\Desktop\\1.mp3')
# # 延迟一分钟在叫一次
time.sleep(60)
# if(int(now.strftime("%M"))-int(alarm_minute) == 1):
print('时间是 ')
print(int(now.strftime("%M"))-int(alarm_minute))
playsound('C:\\Users\\30675\\Desktop\\1.mp3')
# 再给一分钟机会
time.sleep(60)
pp.say('快啲起身,我宜家去發email,俾人知你咁懒!')
pp.runAndWait()
return 1
return 1
def mail_qq():
msg_from = 'xxx@qq.com' # 发送方邮箱
passwd = 'xxx'
# 就是上面的授权码
to = ['xxx@qq.com']
# 接受方邮箱
# 设置邮件内容
# MIMEMultipart类可以放任何内容
msg = MIMEMultipart()
conntent = "测试"
# 把内容加进去
msg.attach(MIMEText(conntent, 'plain', 'utf-8'))
# 设置邮件主题
msg['Subject'] = "这个是邮件主题"
# 发送方信息
msg['From'] = msg_from
# 开始发送
# 通过SSL方式发送,服务器地址和端口
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
# 登录邮箱
s.login(msg_from, passwd)
# 开始发送
s.sendmail(msg_from, to, msg.as_string())
print("邮件发送成功")
if __name__ == '__main__':
a = alarm()
if a == 1:
# 如果闹钟没关闭就发邮件
mail_qq()
更多推荐



所有评论(0)