我可以同时使用 print() 函数和 speak 或 say(如在 pyttsx3 模块中)吗?

Can I use a print() function and the speak or say(as in pyttsx3 module) at the same time?

我正在想办法让使用 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 命令协同工作。

在每个句子后使用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)