How can I convert text to speech (mp3 file) in python?
To generate the Audio file from the text file, i am using this code i hope it can help you
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
infile = "SHIVA.txt"
outfile = "SHIVA-audio.wav"
stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
f = open(infile, 'r')
theText = f.read()
f.close()
engine.speak(theText)
stream.Close()
I do not know about pyttsx, but a while ago I used the Google TTS API to generate MP3s from text.
You can get an idea of how it works from this code snippet. The free version of Google TTS is limited to a certain number of letters for each request, So I'd recommend splitting the text into sentences and creating a file for each sentence.
If you need help with that, please tell me.