You are on page 1of 15

Basic operations

Using the web browser in Python


The webbrowser module provides a high-level interface to allow
displaying Web-based documents to users. Under most circumstances,
simply calling the open() function from this module will open url using
the default browser . You have to import the module and use open()
function.

Example
import webbrowser
webbrowser.open('http://net-informations.com', new=2)

If new is 0, the url is opened in the same browser window if possible. If


new is 1, a new browser window is opened if possible. If new is 2, a
new browser page ("tab") is opened if possible.
How to Launch Computer Programs Using Python

• A Python script can start other programs on your computer.


• For example, it can open up the calculator (to do calculations)
or it can open up notepad (so that you can write a
document). Or it can open up a sound file that can be played.
• Python has the low-level functionality to be able to open up
any program on your operating, just as if you had double-
clicked on it (to get it started).
• You can do this in Python using the subprocess.Popen()
function.
• Using this function, all you have to do is pass in the program
that you want to open up in its parameters.
Opening Up a Calculator
The code below shows how to open up a calculator
on a Windows computer.
import subprocess
subprocess.Popen('C:\\Windows\\System32\\calc.exe')

Opening Up Notepad
The code below shows up to open up notepad on a
Windows PC.
import subprocess
subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
Opening Up Wordpad
The code below shows how to open up
Wordpad on a Windows computer.
import subprocess
subprocess.Popen('C:\\Windows\\System32\\write.exe')
playsound

from playsound import playsound


playsound('myfile.wav')

To install playsound module


pip install playsound==1.2.2
Date and Time
from datetime import date
today = date.today()
print("Today's date:", today)
from datetime import datetime
now = datetime.now()
print("now =", now)
dt_string = now.strftime("%d/%m/%Y %H:%M:
%S")
print("date and time =", dt_string)
Convert text to speech
 Python provides many APIs to convert text to
speech. The Google Text to Speech API is
popular and commonly known as the gTTS API.
The gTTS API provides the facility to convert text
files into different languages such as English,
Hindi, German, Tamil, French, and many more.
We can also play the audio speech in fast or
slow mode.
• Installation of gTTS API

Type the following command in the terminal to install the gTTS API.
pip install gTTS  

Then, install the additional module to work with the gTTS.


pip install playsound  
import gtts  
from playsound import playsound  
# make a request to google to get synthesis  
t1 = gtts.gTTS("Welcome to the world of python")  
‘’’
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.
‘’’
# save the audio file  
t1.save("welcome.mp3")   
It will save it into a directory, we can listen this file as follow:
# play the audio file  
playsound("welcome.mp3")  
# Import the gTTS module for text  
# to speech conversion  
from gtts import gTTS  
  
# This module is imported so that we can  
# play the converted audio  
  
from playsound import playsound  
  
# It is a text value that we want to convert to audio  
text_val = 'All the best for your exam.'  
  
# Here are converting in English Language  
language = 'en'  
  
# Passing the text and language to the engine,  
# here we have assign slow=False. Which denotes  
# the module that the transformed audio should  
# have a high speed  
obj = gTTS(text=text_val, lang=language, slow=False)  
  
#Here we are saving the transformed audio in a mp3 file named  
# exam.mp3  
obj.save("exam.mp3")  
  
# Play the exam.mp3 file  
playsound("exam.mp3")  
In the above code, we have imported the API
and use the gTTS function. The gTTS() function
which takes three arguments -
  The first argument is a text value that we want
to convert into a speech.
The second argument is a specified language. It
supports many languages. We can convert the
text into the audio file.
The third argument represents the speed of the
speech. We have passed slow value as false; it
means the speech will be at normal speed.
List of languages
{'af': 'Afrikaans', 'sq': 'Albanian', 'ar': 'Arabic', 'hy':
'Armenian', 'bn': 'Bengali', 'bs': 'Bosnian', 'ca': 'Catalan',
'hr': 'Croatian', 'cs': 'Czech', 'da': 'Danish', 'nl': 'Dutch',
'en': 'English', 'et': 'Estonian', 'tl': 'Filipino', 'fi': 'Finnish',
'fr': 'French', 'de': 'German', 'el': 'Greek', 'en-us': 'English
(US)','gu': 'Gujarati', 'hi': 'Hindi', 'hu': 'Hungarian', 'is':
'Icelandic', 'id': 'Indonesian', 'it': 'Italian', 'ja': 'Japanese',
'en-ca': 'English (Canada)', 'jw': 'Javanese', 'kn':
'Kannada', 'km': 'Khmer', 'ko': 'Korean', 'la': 'Latin', 'lv':
'Latvian', 'mk': 'Macedonian', 'ml': 'Malayalam', 'mr', 'en-
in': 'English (India)'}
Reading from a File
pip3 install SpeechRecognition pydubCopy
open up a new Python file and import it:
import speech_recognition as sr

Make sure you have an audio file in the current


directory that contains English:
import speech_recognition as sr
filename = “ml.wav“
# initialize the recognizer
r = sr.Recognizer()
# open the file
with sr.AudioFile(filename) as source:
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data) print(text)

You might also like