我尝试在Python3.9.13 MSC v.1929 64位(AMD64)中使用语音合成。如果我完全理解,pyttxs3
就是专用模块(预先感谢您提供任何可能的选择!),我成功地安装了该模块:
c:\>pip install -U pyttsx3
Requirement already satisfied: pyttsx3 in c:\users\...\python39\site-packages (2.90)
Requirement already satisfied: comtypes in c:\users\...\python39\site-packages (from pyttsx3) (1.1.11)
Requirement already satisfied: pywin32 in c:\users\...\python39\site-packages (from pyttsx3) (304)
Requirement already satisfied: pypiwin32 in c:\users\...\python39\site-packages (from pyttsx3) (223)
但是我不能初始化引擎:当我初始化的时候
>>> e = pyttsx3.init()
Traceback (most recent call last):
File "C:\Users\...\Python39\site-packages\pyttsx3\__init__.py", line 20, in init
eng = _activeEngines[driverName]
File "C:\Program Files\...\lib\weakref.py", line 137, in __getitem__
o = self.data[key]()
KeyError: None
During handling of the above exception, another exception occurred:
...
File "C:\Users\...\Python39\site-packages\pyttsx3\drivers\sapi5.py", line 10, in <module>
import pythoncom
File "C:\Users\...\Python39\site-packages\pythoncom.py", line 2, in <module>
import pywintypes
ModuleNotFoundError: No module named 'pywintypes'
我注意到,部分脚本在我的Users\...\AppData\Local\Packages\PythonSoftwareFoundation...
中,有些在"C:\Program Files\WindowsApps\PythonSoftwareFoundation...
中。我不知道这是否是造成这些错误的原因。
我怎么才能解决这个问题?(我还发现还有其他模块,speech
和pyttsx
,不过它们似乎适用于Python2.7:它们生成的是Python3版本的speech
吗?)
发布于 2022-05-25 17:09:12
pyttxs3有一点故障(实际上非常严重),例如,在我删除了代码的一部分之后,我的代码还在说一些话,我从代码中删除了整个pyttxs3,最后我不得不卸载软件包。我用了这个功能。您需要安装pygame (这是一个游戏制作包,但我们将使用它)和gtts,即google文本到语音。
import os
from pygame import *
from gtts import *
def speak(text: str):
try:
tts_ = gTTS(text=text, lang='en')
ran = random.randint(0, 1000000)
audio_file = 'audio-' + str(ran) + '.mp3'
tts_.save(audio_file)
# Starting the mixer
mixer.init()
# Loading the song
mixer.music.load("C:///Users/roopa/PycharmProjects/pokemon game/" + audio_file)
# Start playing the song
mixer.music.play()
clock = time.Clock()
# infinite loop
while mixer.music.get_busy():
clock.tick(60)
mixer.music.unload()
os.remove(audio_file)
except gTTSError:
print('unknown error')
发布于 2022-05-25 20:26:53
由于"Failed loading libmpg123-0.dll"
错误,Roop的回答对我没有完全奏效,但它使我能够找到以下工作解决方案:
import gtts # text to mp3 file
import random # for random file name generation
import playsound # to play mp3 file
import os # to remove audio file
def say(text: str, block = True, lang = 'en'):
"""Text-to-speech synthesis using google TTS. If block=True,
waits until the text is spoken. If False, return a cleanup
function to delete the temporary audio file."""
audio_file = f'audio-{random.randint(0, 1000000)}.mp3'
gtts.gTTS(text, lang = lang).save(audio_file)
playsound.playsound(audio_file, block = block)
if block:
os.remove(audio_file)
else:
print(f"Playing sound, don't forget to do: os.remove('{audio_file}')"
"\n(You can do so by calling the returned function.)")
return(lambda: os.remove(audio_file))
if __name__=='__main__':
cleanup = say("Good morning, Max!")
if cleanup: # if block = True, function returns None
import time
time.sleep(10)
cleanup()
(这似乎是一个known bug of playsound
,如果脚本结束得太早,声音不会被播放到最后,因此我添加了睡眠(10),但是很明显,您可以做其他的事情。)
尽管如此,有关pyttsx3
和pywin32
的问题仍然悬而未决。
https://stackoverflow.com/questions/72381569
复制相似问题