звук из текста

Python – getting sound from text

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:

  1. from gtts import gTTS #Connected the library
  2. ru = str('Добрый день!') #Set the text in Russian
  3. de = str('Guten tag!') #Set the text in German (as an example)
  4.  
  5. tts_ru = gTTS(ru, lang='ru') #Designated the language of our text
  6. tts_de = gTTS(de, lang='de') #Designated the language of our text
  7. with open('hello.mp3', 'wb') as f: #Created a file to which we will write sound from text
  8.     tts_ru.write_to_fp(f) #Write the voice acting of the Russian text to the file
  9.     tts_de.write_to_fp(f) #Write the voice acting of the German text to the file

Rejoice ?
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.