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

2024-04-19 19:24:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在试图找到一种方法,这样用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命令一起工作。你知道吗


Tags: the代码byquickenginesallysayprint
1条回答
网友
1楼 · 发布于 2024-04-19 19:24:42

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

相关问题 更多 >