Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

How to Convert Text to Speech in Python, Lecture notes of Computer Programming

In this tutorial, we will learn how to convert the human language text into human-like speech. Sometimes we prefer listening to the content instead of reading. We can do multitasking while listening to the critical file data. Python provides many APIs to convert text to speech. The Google Text to Speech API is popular and commonly known as the gTTS API. It is very easy to use the tool and provides many built-in functions which used to save the text file as an mp3 file.

Typology: Lecture notes

2021/2022

Available from 10/27/2022

tantan-bernabe
tantan-bernabe 🇵🇭

5 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python Program
University of the Philippines
How to Convert Text to Speech in Python
In this tutorial, we will learn how to convert the human language text into human-like speech.
Sometimes we prefer listening to the content instead of reading. We can do multitasking while listening
to the critical file data. Python provides many APIs to convert text to speech. The Google Text to Speech
API is popular and commonly known as the gTTS API.
It is very easy to use the tool and provides many built-in functions which used to save the text file as an
mp3 file.
You don't need to train a model with a neural network to convert files to speech. This is also difficult to
achieve. Instead, use these APIs to complete your tasks.
The gTTS API provides functionality to convert text files to different languages such as English, Hindi,
German, Tamil, French, etc. Voice speech can also be played in fast or slow mode.
As a final update however, it is not possible to change the language file. This is system generated and
cannot be changed.
To convert text files, use another offline library called pyttsx3.
Installation of gTTS API
Type the following command in the terminal to install the gTTS API.
Then, install the additional module to work with the gTTS.
and then install pyttsx3.
Let's understand the working of gTTS API
As you can see, it's very easy to use. We need to import it and pass it a gTTS object, which is an interface
to the Google Translator API.
pf3
pf4
pf5

Partial preview of the text

Download How to Convert Text to Speech in Python and more Lecture notes Computer Programming in PDF only on Docsity!

Python Program University of the Philippines How to Convert Text to Speech in Python In this tutorial, we will learn how to convert the human language text into human-like speech. Sometimes we prefer listening to the content instead of reading. We can do multitasking while listening to the critical file data. Python provides many APIs to convert text to speech. The Google Text to Speech API is popular and commonly known as the gTTS API. It is very easy to use the tool and provides many built-in functions which used to save the text file as an mp3 file. You don't need to train a model with a neural network to convert files to speech. This is also difficult to achieve. Instead, use these APIs to complete your tasks. The gTTS API provides functionality to convert text files to different languages such as English, Hindi, German, Tamil, French, etc. Voice speech can also be played in fast or slow mode. As a final update however, it is not possible to change the language file. This is system generated and cannot be changed. To convert text files, use another offline library called pyttsx3. Installation of gTTS API Type the following command in the terminal to install the gTTS API. Then, install the additional module to work with the gTTS. and then install pyttsx3. Let's understand the working of gTTS API As you can see, it's very easy to use. We need to import it and pass it a gTTS object, which is an interface to the Google Translator API.

In the above line, we have sent the data in text and received the actual audio speech. Now, save this an audio file as welcome.mp3. It will save it into a directory, we can listen this file as follow: Output: Turn on the system volume and listen to the previously saved text. Defines a complete text-to-speech Python program.

Output: Explanation: The code above imports the API and uses gTTS functions. gTTS() function that takes 3 arguments

  • The first argument is a text value that we want to convert into a speech.
  • The second argument is a specified language. It supports many languages. We can convert the text into the audio file.
  • The third argument represents the speed of the speech. We have passed slow value as false; it means the speech will be at normal speed We saved this file as Exam.py, which I could access anytime, and used the playsound() function to listen to the audio file at runtime. The list of available languages To get the available languages, use the following functions Output: We have mentioned few important languages and their code. You can find almost every language in this library. Offline API You've used the Google API, but what if you want to convert text to speech offline? Python provides the pyttsx3 library, which looks up the TTS engine pre-installed on the platform. Let's understand how to use pyttsx3 library: Example:

In the code above, we used the say() method and passed the text as an argument. Used to add spoken words to the queue while the runAndWait() method runs the actual event loop until all commands are queued. It also provides some additional properties that you can use as needed. Get Talking Speed Details: Output: If we pass the 100 then it will be slower. Now, we can hear the text file in the voices. Output: This tutorial showed how to convert a text file to speech using a third-party library. We also discussed offline libraries. This allows you to set up your own virtual assistance.