我可以同时使用print()函数和语音或语音(如pyttsx3模块中一样)吗?

时间:2019-06-09 12:53:01

标签: python pyttsx

我正在尝试找出一种方法,以便使用print()函数在屏幕上打印的文本也可以同时说出来。我目前正在使用pyttsx3模块,但看不到该怎么做。

对于这种情况下的尝试我并不了解。下面的代码只是一个示例代码。

import pyttsx
engine = pyttsx.init()

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

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

engine.runAndWait()

我希望printengine.say命令一起工作。

1 个答案:

答案 0 :(得分:1)

在每个句子后使用runAndWait()

如果您为此目的定义一个函数,然后遍历要打印和说出的句子列表,则代码将是这样:

import pyttsx3

engine = pyttsx3.init() 

def print_and_speak(text):
    print(text)
    engine.say(text)
    engine.runAndWait()

text_list = ['Sally sells seashells by the seashore.',
             'The quick brown fox jumped over the lazy dog.']

for t in text_list:
    print_and_speak(t)