We can perform text-to-speech in Python using the PyTTSX3 speech synthesis library.
Install the PyTTSX3 library:
$ pip install pyttsx3
The following example script will synthesize the audio for speaking “hello”.
import pyttsx3 synthesizer = pyttsx3.init() synthesizer.say("hello") synthesizer.runAndWait() synthesizer.stop()
To perform speech synthesis with a specific voice, use the following.
This is specific to macOs.
import pyttsx3 synthesizer = pyttsx3.init() voices = synthesizer.getProperty("voices") for voice in voices: print(voice.id) voiceChoice = input("Enter name: ") synthesizer.setProperty("voice", "com.apple.speech.synthesis.voice." + str(voiceChoice)) stringToSay = input("Enter text to read: ") synthesizer.say(stringToSay) synthesizer.runAndWait() synthesizer.stop()
The example run below shows the available voices and the input choosing a specific voice.
The input string is then synthesized as speech.
com.apple.speech.synthesis.voice.Alex com.apple.speech.synthesis.voice.alice com.apple.speech.synthesis.voice.alva ... com.apple.speech.synthesis.voice.yuri com.apple.speech.synthesis.voice.zosia com.apple.speech.synthesis.voice.zuzana Enter name: yuri Enter text to read: this is a fake voice
The output is audio.