How to Convert Text to Speech in Python with 5 Powerful TTS Libraries
This guide walks you through installing, configuring, and using five Python text‑to‑speech libraries—gTTS, Baidu AIP, pyttsx3, pywin32, and speech—to generate personalized audio files, adjust voice properties, and automate playback.
This article introduces five Python libraries for text‑to‑speech conversion and demonstrates how to install, configure, and generate audio files with each.
1. gTTS
gTTS converts any text into an MP3 audio file.
pip install gtts from gtts import gTTSConfigure the client: tts = gTTS(text=text, lang='zh-tw') Save the audio:
tts.save("output.mp3")2. Baidu AIP
Use Baidu's speech synthesis service by creating an account on the Baidu Open Platform.
# Install Baidu AIP
pip install baidu-aip from aip import AipSpeech APP_ID = 'your_app_id'
API_KEY = 'your_api_key'
SECRET_KEY = 'your_secret_key'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) res = client.synthesis(text, 'zh', 1, {
'spd': 5,
'pit': 5,
'vol': 5,
'per': 0
})
with open('output.mp3', 'wb') as f:
f.write(res)3. pyttsx3
pyttsx3 is an offline TTS engine that allows voice property adjustments.
pip install pyttsx3 import pyttsx3 engine = pyttsx3.init() engine.say('Hello World')
engine.runAndWait()Adjust volume:
vol = engine.getProperty('volume')
engine.setProperty('volume', vol + 0.5)Start a loop for continuous playback:
engine.startLoop()4. pywin32
pywin32 provides access to Windows COM interfaces, enabling speech synthesis via SAPI.
pip install pywin32 import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak("hello")Note: Chinese language support is limited.
5. speech
The speech library, built on top of pywin32, is convenient for voice‑controlled scripts.
pip install speech import speech
speech.say('hello')These five libraries cover a range of use cases from simple online TTS to offline, customizable speech synthesis. Choose the one that best fits your project requirements.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
