SoFunction
Updated on 2024-11-12

Python call pyttsx3 for offline text-to-speech

pyttsx3 is an offline text-to-speech conversion library for Python.

Install the dependencies:

pip install pyttsx3

Linux installation requirements:

apt update && apt install espeak ffmpeg libespeak1

Usage:

import pyttsx3
engine = ()
("I will speak this text")
()

Single-line usage of the read-aloud function with default options

import pyttsx3
("I will speak this text")

Change voice, speed of speech and volume:

import pyttsx3
engine = () # object creation
# Speed of speech
rate = ('rate')   # getting details of current speaking rate
print (rate)                        #printing current voice rate
('rate', 125)     # setting up new voice rate
# Volume
volume = ('volume')   #getting to know current volume level (min=0 and max=1)
print (volume)                          #printing current volume level
('volume',1.0)    # setting up volume level  between 0 and 1
# Voices
voices = ('voices')       #getting details of current voice
#('voice', voices[0].id)  #changing index, changes voices. o for male
('voice', voices[1].id)   #changing index, changes voices. 1 for female
("Hello World!")
('My current speaking rate is ' + str(rate))
()
()
# Save to file
# On linux make sure that 'espeak' and 'ffmpeg' are installed
engine.save_to_file('Hello World', 'test.mp3')
()

This article on Python call pyttsx3 to achieve offline text-to-speech article is introduced to this, more related Python offline text-to-speech content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!