Skip to content

Commit 7cc58a0

Browse files
committed
added converting text to speech tutorial
1 parent 8124984 commit 7cc58a0

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4646
- [How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
4747
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
4848
- [How to Predict Stock Prices in Python using TensorFlow 2 and Keras](https://www.thepythoncode.com/article/stock-price-prediction-in-python-using-tensorflow-2-and-keras). ([code](machine-learning/stock-prediction))
49+
- [How to Convert Text to Speech in Python](https://www.thepythoncode.com/article/convert-text-to-speech-in-python). ([code](machine-learning/text-to-speech))
4950

5051

5152
- ### [General Python Topics](https://www.thepythoncode.com/topic/general-python-topics)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [How to Convert Text to Speech in Python](https://www.thepythoncode.com/article/convert-text-to-speech-in-python)
2+
- `pip3 install -r requirements.txt`
3+
- To convert text to speech online using Google API, use `tts_google.py`
4+
- To use offline engines in your platform, consider using `tts_pyttsx3.py`
5.34 KB
Binary file not shown.
4.41 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pyttsx3
2+
gTTS
3+
playsound
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import gtts
2+
from playsound import playsound
3+
4+
# make request to google to get synthesis
5+
tts = gtts.gTTS("Hello world")
6+
# save the audio file
7+
tts.save("hello.mp3")
8+
# play the audio file
9+
playsound("hello.mp3")
10+
11+
# in spanish
12+
tts = gtts.gTTS("Hola Mundo", lang="es")
13+
tts.save("hola.mp3")
14+
playsound("hola.mp3")
15+
16+
# all available languages along with their IETF tag
17+
print(gtts.lang.tts_langs())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pyttsx3
2+
3+
# initialize Text-to-speech engine
4+
engine = pyttsx3.init()
5+
6+
# convert this text to speech
7+
text = "Python is a great programming language"
8+
engine.say(text)
9+
# play the speech
10+
engine.runAndWait()
11+
12+
# get details of speaking rate
13+
rate = engine.getProperty("rate")
14+
print(rate)
15+
16+
# setting new voice rate (faster)
17+
engine.setProperty("rate", 300)
18+
engine.say(text)
19+
engine.runAndWait()
20+
21+
# slower
22+
engine.setProperty("rate", 100)
23+
engine.say(text)
24+
engine.runAndWait()
25+
26+
# get details of all voices available
27+
voices = engine.getProperty("voices")
28+
print(voices)
29+
# set another voice
30+
engine.setProperty("voice", voices[1].id)
31+
engine.say(text)
32+
engine.runAndWait()

0 commit comments

Comments
 (0)