Convert text to speech (audio and mp3, wav, etc) with Python.
If you want to convert text to speech (audio and mp3, wav, etc) with Python, you can use the gTTS library. gTTS (Google Text-to-Speech) is a Python library and CLI tool to interface with Google Translate's text-to-speech API. Write spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout. Or simply pre-generate Google Translate TTS request URLs to feed to an external program.
Here's a quick example of how to use gTTS to convert text to speech:
Located in main.py:
import os
from gtts import gTTS
def text_to_speech(text, lang='en'):
file = gTTS(text=text, lang=lang)
filename = 'speech.mp3'
file.save(filename)
# play the audio file
os.system("afplay " + filename)
text_to_speech("Hello, this is a test.")
You can also use gTTS to convert text to speech in other languages:
text_to_speech("Hola, esto es una prueba.", lang='es')
I made an example for you here: https://github.com/erictherobot/text-to-speech
Here's an example of the audio output from the above code which should sound like "Hello, this is a test."
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this useful as is please let me know. If you find any bugs, please feel free to submit a pull request or open an issue. If you have any questions, you can .
Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!