visit
Well, It hasn’t been a long since I joined HackerNoon. And I have been seeing an audio file embedded with each article on the website which is auto-generated. It’s kinda cool. At least I think it is 🤪.
Simply put pyttsx3
cause that’s the only library I could put to work. Others are namely gtts
() and python-espeak
().
I wasn’t able to get gtts
to work and python-espeak
didn’t even install properly on my system.
pip install pyttsx3
pyttsx3
import pyttsx3 # importing the library
engine = pyttsx3.init() # initialising the pyttsx3 engine
# setting engine properties
engine.setProperty('rate',125) # setting the rate of words per minute
text = '''And I have been seeing an audio file embedded with
each article on the website which is auto-generated.
It’s kinda cool. At least I think it is.'''
engine.say(text) # this line queues the text for conversion
engine.runAndWait() # this command processes the commands in the queue
# the say command outputs the speech via the sound output device
Now, let’s save that as an audio file:
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate',125)
text = '''And I have been seeing an audio file embedded with
each article on the website which is auto-generated.
It’s kinda cool. At least I think it is.'''
engine.save_to_file(text,'text.mp3') # this command queues the text to be saved as an audio file
engine.runAndWait()
The above code saves the audio as an .mp3
file with the name text
. .
There is No Female Voice included with the core pyttsx3
package.The following is for linux
users as they can use espeak
(a C library pre-installed on linux).
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate',125)
engine.setProperty('voice','english+f3')
text = '''And I have been seeing an audio file embedded with
each article on the website which is auto-generated.
It’s kinda cool. At least I think it is.'''
engine.save_to_file(text,'text_female.mp3')
engine.runAndWait()
So, we used pyttsx3
for converting text to speech in Python
. We didn’t do much other than using an existing API but it can be used as a tinier application for larger programs if we use our brains right enough for a large scale Python project. See you fellas, later on, it was fun learning all this with you.