



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!
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
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.