Skip to content
Afa' Afa'

Text-to-Speech in Python

Convert text to speech in Python with picotts and pyttsx3.

1 min read Updated

Two ways to synthesise speech in Python: the lightweight picotts wrapper and the cross-platform pyttsx3 library. Both scripts are below.

tts-with-picotts.py

python
import os
import subprocess

# Set the text to be read
text = "Bonjour, comment allez-vous?"

# Set the language to French
language = "fr-FR"

# Set the path of the audio file
audio_file_path = "audio.wav"

# Set the command for generating the audio file using pico2wave
command = f"pico2wave -l {language} -w {audio_file_path} '{text}'"

# Execute the command using subprocess
subprocess.call(command, shell=True)

# Play the audio file using the default audio player
os.startfile(audio_file_path)

tts-with-pyttsx3.py

python
# Import the necessary libraries
import pyttsx3

# Initialize the pyttsx3 engine
engine = pyttsx3.init()

# Set the voice to a French voice
engine.setProperty('voice', 'fr')

# Set the speed of the engine
engine.setProperty('rate', 180)

# Set the volume of the engine
engine.setProperty('volume', 1.0)

# Define the input text in French
input_text = "Bonjour, comment ça va ?"

# Convert the input text to speech
engine.say(input_text)

# Play the speech
engine.runAndWait()
Something wrong or want to discuss this article? Get in touch

Search articles and projects

Type to filter articles and projects. Use the arrow keys to move through results and Enter to open one. Press Escape to close.