In this article i want to show you How To Convert Text To Speech In Python, so in the previous articles we had some topics on Python Speech Recognition, it was converting Audio To Text. but in this article we are going to learn Converting Text To Speech, also you can watch the complete video for this article at the end.
Read More Articles
1: Python Speech Recognition With Google Speech
2: Python Speech Recognition Open Website By Speech
3: Python How To Convert Recorded Audio To Text
So there are two ways that you can convert your Text to Audio or Speech, the first way is using Google Text To Speech (gTTS) library and the second way is usin pyttx3 library.
What is Google Text To Speech (gTTS) ?
gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate’s text-to-speech API. writes spoken mp3
data to a file, a file-like object (bytestring) for further audio manipulation, or stdout.
Features
- Customizable speech-specific sentence tokenizer that allows for unlimited lengths of text to be read, all while keeping proper intonation, abbreviations, decimals and more.
- Customizable text pre-processors which can, for example, provide pronunciation corrections.
- Automatic retrieval of supported languages.
How to Install Google Text To Speech
You can simply install gTTS by using pip.
1 |
pip install gTTS |
This is the complete source code for How To Convert Text To Speech In Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Import the gTTS module from the gtts package from gtts import gTTS # Create a gTTS object with the text "hello freinds # how are you" and language 'en' (English) tts = gTTS(text="hello freinds how are you", lang='en') # Save the synthesized speech as an MP3 file # named "record2.mp3" tts.save("record2.mp3") # Print a success message indicating that the # text has been converted successfully print("Text Converted Successfully") |
This code utilizes the gTTS (Google Text-to-Speech) library to convert the text “hello freinds how are you” into speech in English. After that ut saves the synthesized speech as an MP3 file named “record2.mp3”. And lastly it prints a success message indicating that the text has been converted successfully. OK now run the complete code, you will see the converted Text To Audio.
The second way is using pyttsx3 library
What is pyttsx3 Library?
pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline, and is compatible with both Python 2 and 3. you can simple install by using pip install pyttsx3 .
If you receive errors such as No module named win32com.client, No module named win32, or No module named win32api, you will need to additionally install pypiwin32.
So now this is the complete code for pyttsx3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Import the pyttsx3 module import pyttsx3 # Initialize the pyttsx3 engine engine = pyttsx3.init() # Use the engine to say "hello freinds how are you" engine.say("hello freinds how are you ") # Set the speech rate to 120 words per minute engine.setProperty('rate', 120) # Set the volume to 90% engine.setProperty('volume', 0.9) # Run the engine and wait for speech to finish engine.runAndWait() |
This code uses the pyttsx3 library to convert the text “hello freinds how are you” into speech. It initializes the speech engine, sets the speech rate to 120 words per minute, and adjusts the volume to 90%. Then, it runs the engine to speak the text and waits for the speech to finish before continuing.
FAQs:
Q: How to do text to speech on Python?
AL Text-to-speech (TTS) in Python can be achieved using libraries such as pyttsx3, gTTS, or pyttsx. These libraries provide APIs for synthesizing speech from text, and it allows Python programs to speak textual content.
Q: How to convert text to speech?
A: To convert text to speech in Python, you can use the say() function provided by TTS libraries such as pyttsx3. Simply initialize the TTS engine, specify the text you want to convert to speech, and call the say() function to generate speech output.
Q: How to convert audio to text in Python?
A: Converting audio to text in Python is typically done using speech recognition libraries such as SpeechRecognition. These libraries provide functions to transcribe spoken audio into text. You can capture audio input from a microphone or load audio files and then use the speech recognition API to convert the audio to text.
Q: What is the function of TTS in Python?
A: The function of TTS (text-to-speech) in Python is to synthesize spoken speech from textual content. TTS libraries enable Python programs to generate speech output from text strings, allowing for applications such as voice assistants, speech-enabled applications, accessibility tools, and more. TTS functionality is commonly used in applications where spoken feedback or interaction is required.
Subscribe and Get Free Video Courses & Articles in your Email
TypeError: item 1 in _argtypes_ passes a union by value, which is unsupported.
i am getting above error
Thanks, great job How can I slow it down