Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python: Convert Speech to text and text to Speech
Next article icon
pyttsx is a cross-platform text to speech library which is platform independent. The major advantage of using this library for text-to-speech conversion is that it works offline. However,pyttsxsupports only Python 2.x. Hence, we will see pyttsx3 which is modified to work on both Python 2.x and Python 3.x with the same code.Use this command for Installation:
pip install pyttsx3
 Usage -First we need to import the library and then initialise it usinginit() function. This function may take 2 arguments.init(driverName string, debug bool)
  • drivername : [Name of available driver]sapi5on Windows |nssson MacOS
  • debug: to enable or disable debug output
After initialisation, we will make the program speak the text usingsay() function. This method may also take 2 arguments.say(text unicode, name string)
  • text : Any text you wish to hear.
  • name : To set a name for this speech. (optional)
Finally, to run the speech we userunAndWait() All thesay() texts won't be said unless the interpreter encountersrunAndWait().Code #1: Speaking TextPython3 1==
# importing the pyttsx libraryimportpyttsx3# initialisationengine=pyttsx3.init()# testingengine.say("My first code on text-to-speech")engine.say("Thank you, Geeksforgeeks")engine.runAndWait()
 Code #2: Listening for eventsPython3
importpyttsx3defonStart():print('starting')defonWord(name,location,length):print('word',name,location,length)defonEnd(name,completed):print('finishing',name,completed)engine=pyttsx3.init()engine.connect('started-utterance',onStart)engine.connect('started-word',onWord)engine.connect('finished-utterance',onEnd)sen='Geeks for geeks is a computer portal for Geeks'engine.say(sen)engine.runAndWait()
Why pyttsx?It works offline, unlike other text-to-speech libraries. Rather than saving the text as audio file,pyttsxactually speaks it there. This makes it more reliable to use for voice-based projects.

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp