Text to speech conversion with Google Text To Speech (GTTS)

Requirements

Python 3+
pip should be installed (normally installed by default with python 3)
GTTS package (write pip install gtts in terminal/command prompt to install it). Documentation is available here

Code

Imports

In [4]:
from gtts import gTTS
import os

Create or import text to convert into speech

In [17]:
myText = "Welcome on my tutorial on google text to speech"

myTextImport = open("text.txt", "r")

myText = myTextImport.read().replace("\n", " ") #replace line breals by space

Define the language

In [19]:
language = "en" # fr for french

Create output

In [18]:
output = gTTS(text = myText, lang = language, slow = False) #slow = False for normal speech rate

Export audio file

In [20]:
output.save("output.mp3")
myTextImport.close()

Read audio file

In [21]:
os.system("start output.mp3")
Out[21]:
0