Python Text to Speech (Pyttsx3)¶
In [ ]:
pip install pyttsx3
In [2]:
import pyttsx3
In [3]:
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
In [4]:
engine = pyttsx3.init()
engine.say("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.")
engine.runAndWait()
Change Rate of Speech¶
In [5]:
engine.getProperty('rate')
Out[5]:
200
In [7]:
engine.setProperty('rate',50)
engine.say("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.")
engine.runAndWait()
In [22]:
engine.setProperty('rate',500)
engine.say("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.")
engine.runAndWait()
In [20]:
engine.setProperty('rate',150)
engine.say("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.")
engine.runAndWait()
Chnage Volume¶
In [11]:
engine.getProperty('volume')
Out[11]:
1.0
In [16]:
engine.setProperty('volume',0.5)
In [28]:
engine.setProperty('rate',200)
engine.say("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.")
engine.runAndWait()
In [18]:
engine.setProperty('volume',1)
engine.setProperty('rate',200)
engine.say("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.")
engine.runAndWait()
Check Voices¶
In [25]:
voices = engine.getProperty('voices')
for voice in voices:
print(voice)
<Voice id=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0 name=Microsoft David Desktop - English (United States) languages=[] gender=None age=None> <Voice id=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0 name=Microsoft Zira Desktop - English (United States) languages=[] gender=None age=None>
Male Voice¶
In [26]:
engine.setProperty('voice',voices[0].id)
In [27]:
engine.say("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.")
engine.runAndWait()
Female Voice¶
In [29]:
engine.setProperty('voice',voices[1].id)
engine.say("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.")
engine.runAndWait()
In [31]:
engine.setProperty('voice',voices[1].id)
engine.say("Namaskar, aap ka din kaisa raha?")
engine.runAndWait()
2. Google Text to Speech¶
- pip install gTTS
In [ ]:
pip install gTTS
In [32]:
from gtts import gTTS
tts = gTTS('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.')
tts.save('hello.mp3')
In [33]:
from gtts import gTTS
tts = gTTS('Namaskar, aap ka din kaisa raha?')
tts.save('hello.mp3')
In [34]:
from gtts import gTTS
tts = gTTS('राष्ट्रपति द्रौपदी मुर्मू आज लोकसभा और राज्यसभा की संयुक्त बैठक को संबोधित करते हुए अगले 5 सालों के लिए नई सरकार के रोडमैप की रूपरेखा पेश करेंगी. बता दें कि 18वीं लोकसभा का पहला सत्र जारी है')
tts.save('hello.mp3')
In [39]:
from gtts import gTTS
tts = gTTS('hello', lang='en', tld='co.in')
tts.save('hello.mp3')
In [40]:
from gtts import gTTS
tts = gTTS('hello', lang='en', tld='com.au')
tts.save('hello.mp3')
In [41]:
from gtts import gTTS
tts = gTTS('hello', lang='fr', tld='fr')
tts.save('hello.mp3')
In [42]:
from gtts import gTTS
tts = gTTS('राष्ट्रपति द्रौपदी मुर्मू आज लोकसभा और राज्यसभा की संयुक्त बैठक को संबोधित करते हुए अगले 5 सालों के लिए नई सरकार के रोडमैप की रूपरेखा पेश करेंगी. बता दें कि 18वीं लोकसभा का पहला सत्र जारी है',lang='fr', tld='fr')
tts.save('hello.mp3')
In [ ]:
pip install pygame
In [1]:
from gtts import gTTS
import pygame
tts = gTTS('राष्ट्रपति द्रौपदी मुर्मू आज लोकसभा और राज्यसभा की संयुक्त बैठक को संबोधित करते हुए अगले 5 सालों के लिए नई सरकार के रोडमैप की रूपरेखा पेश करेंगी. बता दें कि 18वीं लोकसभा का पहला सत्र जारी है')
tts.save('hello.mp3')
pygame.mixer.init()
pygame.mixer.music.load("hello.mp3")
pygame.mixer.music.play()
pygame 2.6.0 (SDL 2.28.4, Python 3.11.4) Hello from the pygame community. https://www.pygame.org/contribute.html
Play Audio using Playsound¶
In [ ]:
pip install playsound==1.2.2
In [2]:
import playsound
playsound.playsound('hello.mp3')
Read News¶
In [ ]:
import requests
import playsound
from gtts import gTTS
import os
r = requests.get('https://newsapi.org/v2/top-headlines?country=in&apiKey=9fe4be099c6c417a8c00b56ee67f9db5')
data = r.json()
for i in range(20):
source = data['articles'][i]['source']['name']
title = data['articles'][i]['title']
news = "Reading News " + str(i+1) + " from " + source + " " + title
print(news)
tts = gTTS(news)
tts.save('hello.mp3')
playsound.playsound('hello.mp3')
os.remove('hello.mp3')
In [2]:
import os
os.remove('hello.mp3')
In [7]:
import requests
import playsound
from gtts import gTTS
import os
voice = input("Say Something:")
print(voice)
L = voice.split()
print(L)
if 'name' in L:
text = "My Name is Karan Arora"
print(text)
tts = gTTS(text)
tts.save('hello.mp3')
playsound.playsound('hello.mp3')
os.remove('hello.mp3')
if 'age' in L:
text = "I am 30 Years Old"
print(text)
tts = gTTS(text)
tts.save('hello.mp3')
playsound.playsound('hello.mp3')
os.remove('hello.mp3')
if 'news' in L:
r = requests.get('https://newsapi.org/v2/top-headlines?country=in&apiKey=9fe4be099c6c417a8c00b56ee67f9db5')
data = r.json()
for i in range(3):
source = data['articles'][i]['source']['name']
title = data['articles'][i]['title']
news = "Reading News " + str(i+1) + " from " + source + " " + title
print(news)
tts = gTTS(news)
tts.save('hello.mp3')
playsound.playsound('hello.mp3')
os.remove('hello.mp3')
aaj ki news suna do ['aaj', 'ki', 'news', 'suna', 'do'] Reading News 1 from NDTV News Gulbadin Naib Faces Ban For 'Faking Injury' In Afghanistan's T20 World Cup Thriller? ICC Rule Says T.. - NDTV Sports Reading News 2 from India Today Julian Assange pleads guilty in WikiLeaks espionage case, avoids prison time - India Today Reading News 3 from Google News Why the success of China's sample return lunar mission matters - The Indian Express
In [ ]: