我正在尝试找出一种方法,以便使用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()
我希望print
和engine.say
命令一起工作。
答案 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)