(python)文本转语音

 
pyttsx3(离线工作)是python中的文本到语音转换库
 
官方网址:
 
https://pyttsx3.readthedocs.io/en/latest/engine.html#module-pyttsx3.voice
 
安装:
 
pip install pyttsx3
 

说明文档:

      创建对象

    engine = pyttsx3.init()

    获取当前语音速率

    rate = engine.getProperty('rate')

    print(f'语音速率:{rate}')

    设置新的语音速率

    engine.setProperty('rate', 200)

    获取当前语音音量

    volume = engine.getProperty('volume')

    print(f'语音音量:{volume}')

    设置新的语音音量,音量最小为 0,最大为 1

    engine.setProperty('volume', 1.0)

    获取当前语音声音的详细信息

    voices = engine.getProperty('voices')

    print(f'语音声音详细信息:{voices}')

    设置当前语音声音为女性,当前声音不能读中文

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

    设置当前语音声音为男性,当前声音可以读中文

    engine.setProperty('voice', voices[0].id)

    获取当前语音声音

    voice = engine.getProperty('voice')

    print(f'语音声音:{voice}')

    读txt文档

    with open(r"文档地址.txt","r",encoding="utf-8") as f:

        engine.say(f.line())

 

 官方示例

 1.口语文本

import pyttsx3

engine = pyttsx3.init()

engine.say('Sally sells seashells by the seashore.')

engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

 2.将语音保存到文件

import pyttsx3

engine = pyttsx3.init()

engine.save_to_file('Hello World' , 'test.mp3')

engine.runAndWait()

3. 侦听事件

import pyttsx3

def onStart(name):

      print 'starting', name

def onWord(name, location, length):

      print 'word', name, location, length

def onEnd(name, completed):

      print 'finishing', name, completed

engine = pyttsx3.init()

engine.connect('started-utterance', onStart)

engine.connect('started-word', onWord)

engine.connect('finished-utterance', onEnd)

engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

 4.中断话语

import pyttsx3

def onWord(name, location, length):

      print 'word', name, location, length

      if location > 10:

            engine.stop()

engine = pyttsx3.init()

engine.connect('started-word', onWord)

engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

5.改变声音

engine = pyttsx3.init()

voices = engine.getProperty('voices')

for voice in voices:

   engine.setProperty('voice', voice.id)

   engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

6.更改语速

engine = pyttsx3.init()

rate = engine.getProperty('rate')

engine.setProperty('rate', rate+50)

engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

7.更改音量

engine = pyttsx3.init()

volume = engine.getProperty('volume')

engine.setProperty('volume', volume-0.25)

engine.say('The quick brown fox jumped over the lazy dog.')

engine.runAndWait()

8.运行驱动程序事件循环

engine = pyttsx3.init()

def onStart(name):

     print 'starting', name

def onWord(name, location, length):

     print 'word', name, location, length

def onEnd(name, completed):

     print 'finishing', name, completed

     if name == 'fox':

          engine.say('What a lazy dog!', 'dog')

     elif name == 'dog':

          engine.endLoop()

engine = pyttsx3.init()

engine.connect('started-utterance', onStart)

engine.connect('started-word', onWord)

engine.connect('finished-utterance', onEnd)

engine.say('The quick brown fox jumped over the lazy dog.', 'fox')

engine.startLoop()

  9.使用外部事件循环

engine = pyttsx3.init()

engine.say('The quick brown fox jumped over the lazy dog.', 'fox')

engine.startLoop(False)

engine.iterate() must be called inside externalLoop()

externalLoop()

engine.endLoop()

 

无法切换到男声可参考:https://www.cnblogs.com/2944014083-zhiyu/p/14868455.html

 

posted @ 2022-10-22 18:39  十万神马  阅读(739)  评论(0编辑  收藏  举报