Hello everybody! Today we’ll look at how in Python to solve the problem of getting sound from text. It turns out that this question is solved very simply 🙂
The gtts library comes to the rescue, the documentation for which is available here (we do not consider the issue of installing the library on the computer).
So, we have a task: there is text, you need to get an audio file at the output. This problem is solved very simply:
from gtts import gTTS #Connected the library
ru = str('Добрый день!') #Set the text in Russian
de = str('Guten tag!') #Set the text in German (as an example)
tts_ru = gTTS(ru, lang='ru') #Designated the language of our text
tts_de = gTTS(de, lang='de') #Designated the language of our text
with open('hello.mp3', 'wb') as f: #Created a file to which we will write sound from text
tts_ru.write_to_fp(f) #Write the voice acting of the Russian text to the file
tts_de.write_to_fp(f) #Write the voice acting of the German text to the file
An example of the resulting file can be heard below:
Video (russian languages):
On this topic, getting sound from text using Python is closed. Thanks for attention! In case of questions – write to the email or in Telegram.