Python Text to Speech¶
In [ ]:
pip install pyttsx3
In [1]:
import pyttsx3
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
In [2]:
import pyttsx3
engine = pyttsx3.init()
text = "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.say(text)
engine.runAndWait()
Chnage Rate of Speech¶
In [3]:
engine.getProperty('rate')
Out[3]:
200
In [8]:
engine.setProperty('rate',150)
In [9]:
engine = pyttsx3.init()
text = "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.say(text)
engine.runAndWait()
In [10]:
engine = pyttsx3.init()
text = "Hello, aap kaise hai? Aaj ka din kaisa raha?"
engine.say(text)
engine.runAndWait()
Volume¶
In [11]:
engine.getProperty('volume')
Out[11]:
1.0
In [16]:
engine.setProperty('volume',0.9)
In [17]:
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
Voice¶
In [19]:
voices = engine.getProperty('voices')
voices
Out[19]:
[<pyttsx3.voice.Voice at 0x13cef0c4e10>, <pyttsx3.voice.Voice at 0x13cef0ab190>]
In [23]:
engine.setProperty('voice', voices[0].id)
In [24]:
engine = pyttsx3.init()
engine.say("I will speak this text")
engine.runAndWait()
Google Text to Speech¶
In [ ]:
pip install gTTS
In [25]:
from gtts import gTTS
tts = gTTS('I will speak this text')
tts.save('hello.mp3')
In [26]:
from gtts import gTTS
tts = gTTS('Hello, aap kaise hai? Aaj ka din kaisa raha?')
tts.save('hello.mp3')
In [28]:
from gtts import gTTS
tts = gTTS('अब मैं जेल जाऊंगा 15 दिन की सजा सुनकर कोर्ट में बोले संजय राउत, फिर आई राहत की खबर')
tts.save('hello.mp3')
In [29]:
from gtts import gTTS
tts = gTTS('CM ਮਾਨ ਦੀ ਮੁੜ ਵਿਗੜੀ ਸਿਹਤ, Fortis ਹਸਪਤਾਲ ਕਰਵਾਇਆ ਭਰਤੀ, ਮਜੀਠੀਆ ਨੇ ਕਿਹਾ- ਸਰਕਾਰ CM ਦੀ ਸਿਹਤ ਦੀ ਜਾਣਕਾਰੀ ਰੱਖੇ ਪਾਰਦਰਸ਼ੀ')
tts.save('hello.mp3')
In [32]:
import gtts
gtts.lang.tts_langs()
Out[32]:
{'af': 'Afrikaans', 'ar': 'Arabic', 'bg': 'Bulgarian', 'bn': 'Bengali', 'bs': 'Bosnian', 'ca': 'Catalan', 'cs': 'Czech', 'da': 'Danish', 'de': 'German', 'el': 'Greek', 'en': 'English', 'es': 'Spanish', 'et': 'Estonian', 'fi': 'Finnish', 'fr': 'French', 'gu': 'Gujarati', 'hi': 'Hindi', 'hr': 'Croatian', 'hu': 'Hungarian', 'id': 'Indonesian', 'is': 'Icelandic', 'it': 'Italian', 'iw': 'Hebrew', 'ja': 'Japanese', 'jw': 'Javanese', 'km': 'Khmer', 'kn': 'Kannada', 'ko': 'Korean', 'la': 'Latin', 'lv': 'Latvian', 'ml': 'Malayalam', 'mr': 'Marathi', 'ms': 'Malay', 'my': 'Myanmar (Burmese)', 'ne': 'Nepali', 'nl': 'Dutch', 'no': 'Norwegian', 'pl': 'Polish', 'pt': 'Portuguese', 'ro': 'Romanian', 'ru': 'Russian', 'si': 'Sinhala', 'sk': 'Slovak', 'sq': 'Albanian', 'sr': 'Serbian', 'su': 'Sundanese', 'sv': 'Swedish', 'sw': 'Swahili', 'ta': 'Tamil', 'te': 'Telugu', 'th': 'Thai', 'tl': 'Filipino', 'tr': 'Turkish', 'uk': 'Ukrainian', 'ur': 'Urdu', 'vi': 'Vietnamese', 'zh-CN': 'Chinese (Simplified)', 'zh-TW': 'Chinese (Mandarin/Taiwan)', 'zh': 'Chinese (Mandarin)'}
In [40]:
from gtts import gTTS
tts = gTTS('CM Maan di mudh vigdi sehat, Fortis hospital karwaya bharti, Majithia ne keha - sarkar CM di sehat di jaanakari rakhe paardarshi.',lang='en')
tts.save('hello.mp3')
In [41]:
import playsound
playsound.playsound('hello.mp3', True)
In [42]:
import IPython.display as ipd
ipd.Audio(filename='hello.mp3')
Out[42]:
Read News¶
In [43]:
import requests
r = requests.get('https://newsapi.org/v2/top-headlines?country=in&apiKey=9fe4be099c6c417a8c00b56ee67f9db5')
r
Out[43]:
<Response [200]>
In [44]:
r.json()
Out[44]:
{'status': 'ok', 'totalResults': 0, 'articles': []}
In [ ]: