Text to speech
Download all scripts: text_to_speech.zip
Set up a virtual environment
https://docs.python.org/3/tutorial/venv.html
# macOS or Unix
# Create a new virtual environment
python3 -m venv venv-COURSE
# Activate the virtual environment
source venv-COURSE/bin/activate
# Install gTTS (Google Text-to-Speech)
pip install gTTS
# Windows
# Create a new virtual environment
py -m venv venv-COURSE
# Activate the virtual environment
venv-COURSE\Scripts\activate.bat
# Install gTTS (Google Text-to-Speech)
pip install gTTS
text_to_speech.py
#!/usr/bin/env python3
import os.path # Used to assemble a path to a file
from gtts import gTTS # Used to convert text to speech (an MP3 file with spoken text)
from subprocess import call # Used to play an MP3 file
# Choose a string to be converted to an MP3 file
text = 'Time flies like an arrow; fruit flies like a banana'
# Assemble a path to a file
filename = 'gTTS.mp3' # Choose a file name
directory_of_script = os.path.dirname(os.path.realpath(__file__)) # Get the directory name of this script
path_to_file = os.path.join(directory_of_script, filename) # Join the directory name and the filename
# Request the MP3 file
print('Requesting an MP3 file for \'{}\'.'.format(text))
tts = gTTS(text = text, lang = 'en')
# Save the MP3 file
print('Saving the MP3 file to \'{}\'.'.format(filename))
tts.save(path_to_file)
# Play the MP3 file
print('Playing the MP3 file.')
call(['afplay', path_to_file])
'''
datetime – A module for basic date and time types
https://docs.python.org/3/library/datetime.html
gTTS – A Python interface for Google's Text to Speech API
https://github.com/pndurette/gTTS
os.path — Common pathname manipulations
https://docs.python.org/3/library/os.path.html
subprocess
https://docs.python.org/3/library/subprocess.html
'''
the_time_is.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime # Used to get the current time
import os.path # Used to assemble a path to a file
from gtts import gTTS # Used to convert text to speech (an MP3 file with spoken text)
from subprocess import call # Used to play an MP3 file
# Create a string with the current time
now = datetime.now() # Get current time as datetime object
text = datetime.strftime(now, 'The time is %-H o\'clock %-M minutes and %-S seconds') # Format the datetime object
# Assemble a path to a file
filename = 'gTTS.mp3' # Choose a file name
directory_of_script = os.path.dirname(os.path.realpath(__file__)) # Get the directory name of this script
path_to_file = os.path.join(directory_of_script, filename) # Join the directory name and the filename
# Request the MP3 file
print('Requesting an MP3 file for \'{}\'.'.format(text))
tts = gTTS(text = text, lang = 'en')
# Save the MP3 file
print('Saving the MP3 file to \'{}\'.'.format(filename))
tts.save(path_to_file)
# Play the MP3 file
print('Playing the MP3 file.')
call(['afplay', path_to_file])
'''
datetime – A module for basic date and time types
https://docs.python.org/3/library/datetime.html
gTTS – A Python interface for Google's Text to Speech API
https://github.com/pndurette/gTTS
os.path — Common pathname manipulations
https://docs.python.org/3/library/os.path.html
subprocess
https://docs.python.org/3/library/subprocess.html
'''
the_time_is_multilingual.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse # Used to parse command line arguments
from datetime import datetime # Used to get the current time
import os.path # Used to assemble a path to a file
from gtts import gTTS # Used to convert text to speech (an MP3 file with spoken text)
from subprocess import call # Used to play an MP3 file
# Setup argument parser
parser = argparse.ArgumentParser(description = 'Speak the current time using Google Text-to-Speech.')
parser.add_argument('-v', '--verbose', help = 'Comment what\'s going on', default = False, action='store_true')
parser.add_argument('-l', '--language', help = 'Select the language', default = 'en', choices=['de', 'en'])
args = parser.parse_args()
# Create a string with the current time
now = datetime.now() # Get current time as datetime object
if args.language == 'en':
text = datetime.strftime(now, 'The time is %-H o\'clock %-M minutes and %-S seconds') # Format the datetime object
else:
text = datetime.strftime(now, 'Es ist %-H Uhr %-M Minuten und %-S Sekunden')
# Assemble a path to a file
filename = 'gTTS.mp3' # Choose a file name
directory_of_script = os.path.dirname(os.path.realpath(__file__)) # Get the directory name of this script
path_to_file = os.path.join(directory_of_script, filename) # Join the directory name and the filename
# Request the MP3 file
if args.verbose: print('Requesting an MP3 file for \'{}\'.'.format(text))
tts = gTTS(text = text, lang = args.language)
# Save the MP3 file
if args.verbose: print('Saving the MP3 file to \'{}\'.'.format(filename))
tts.save(path_to_file)
# Play the MP3 file
if args.verbose: print('Playing the MP3 file.')
call(['afplay', path_to_file])
'''
argparse
https://docs.python.org/3/library/argparse.html
datetime – A module for basic date and time types
https://docs.python.org/3/library/datetime.html
gTTS – A Python interface for Google's Text to Speech API
https://github.com/pndurette/gTTS
os.path — Common pathname manipulations
https://docs.python.org/3/library/os.path.html
subprocess
https://docs.python.org/3/library/subprocess.html
'''