python pyttsx3库将文本(text)转换为语音

2023-06-1415:00:51后端程序开发Comments931 views字数 1272阅读模式

利用python中的一些库,可以实现文本转音频的功能。这时候,电脑将作为一个AI语音播报助手,直接将输入的文字内容读给你听。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

这里举一个简单的例子。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

1、 首先安装一个pyttsx3库。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

pip install pyttsx3文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

2、 导入需要的库文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

import pyttsx3 as pts文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

pt = pts.init() # 初始化文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

pt.say("Hello. Nice to meet you.") # 设置语音"Hello. Nice to meet you."文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

pt.runAndWait() #使电脑读出语音文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

如果电脑扬声器正常打开的话,就能听到语音了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

3、使用.setProperty()函数设置语音参数,.getProperty()获取参数值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

定义一个函数,根据目前的时间发出不同的问候语。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

import pyttsx3文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

import datetime文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine=pyttsx3.init('sapi5')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

voices=engine.getProperty('voices')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

rate= engine.getProperty('rate')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

volume= engine.getProperty('volume')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine.setProperty('voice',voices[1].id) #voices[0]表示男声,voices[1]表示女声。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine.setProperty('rate',100) #rate设置语速,默认值200。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine.setProperty('volume',0.5) # volume设置音量比例,取值在0到1之间。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

def speak(text):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine.say(text)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    engine.runAndWait()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

def wishMe():文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    hour=datetime.datetime.now().hour文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    if hour>=0 and hour<12:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      speak("Hello,Good Morning")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      print("Hello,Good Morning")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    elif hour>=12 and hour<18:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      speak("Hello,Good Afternoon")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      print("Hello,Good Afternoon")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    else:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      speak("Hello,Good Evening")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

      print("Hello,Good Evening")文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

注意在".say"后面加上".runAndWait()"。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

执行函数:wishMe()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

python pyttsx3库将文本(text)转换为语音文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

4、 保存语音文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

def save_file(text,filename):文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

engine.save_to_file(text, filename)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

    engine.runAndWait()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

hour=datetime.datetime.now().hour文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

save_file("The current time is {} o'clock".format(hour),'{}.wav'.format(hour))文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html

1700:04
读者可根据需要,结合其他工具来实现更多的功能,比如让电脑自动读一首诗,自动进行天气预报等等。
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/47034.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/47034.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定