Audiobook Creator Using gTTS in Python: Build Your Own Text-to-Speech Tool
Audiobooks have become increasingly popular as people look for convenient ways to consume content while multitasking. Whether it’s listening to novels, study material, or blogs, audio content offers flexibility and accessibility. With Python, you can create your own audiobook generator using the gTTS (Google Text-to-Speech) library.
In this blog, you’ll learn how to convert text into speech, create audio files, and build a simple audiobook creator step by step.
1. What is gTTS?
gTTS (Google Text-to-Speech) is a Python library that converts text into spoken audio using Google’s text-to-speech API. It supports multiple languages and produces natural-sounding speech.
Key Features:
- Simple and easy to use
- Supports multiple languages
- Generates MP3 audio files
- Works offline after generation
2. Why Build an Audiobook Creator?
Creating an audiobook generator can be useful for:
- Converting study notes into audio
- Listening to blogs or articles
- Helping visually impaired users
- Learning languages through listening
- Automating content creation
3. Installing Required Libraries
To get started, install the required library:
pip install gTTS
(Optional for playback)
pip install playsound
4. Convert Text to Speech (Basic Example)
from gtts import gTTS
text = "Welcome to your first audiobook created with Python."
tts = gTTS(text=text, lang='en')
tts.save("audiobook.mp3")
print("Audiobook created successfully!")
This code converts text into an MP3 audio file.
5. Play the Audio File
from playsound import playsound
playsound("audiobook.mp3")
6. Convert Text File into Audiobook
You can convert an entire text file into audio:
from gtts import gTTS
with open("book.txt", "r",
encoding="utf-8") as file:
text = file.read()
tts = gTTS(text=text, lang='en')
tts.save("book_audio.mp3")
7. Handling Large Text (Important)
gTTS may not work efficiently with very large text. So, split the content into smaller parts:
from gtts import gTTS
def text_to_audio_chunks(text, chunk_size=500):
for i in range(0, len(text), chunk_size):
yield text[i:i+chunk_size]
text = "Your long text goes here..."
for i, chunk in enumerate
(text_to_audio_chunks(text)):
tts = gTTS(text=chunk, lang='en')
tts.save(f"part_{i}.mp3")
8. Merge Audio Files (Optional)
You can combine multiple audio files using libraries like pydub:
pip install pydub
from pydub import AudioSegment
combined = AudioSegment.empty()
for i in range(5):
audio = AudioSegment.
from_mp3(f"part_{i}.mp3")
combined += audio
combined.export("final_audiobook.mp3",
format="mp3")
9. Add Language Support
gTTS supports multiple languages:
tts = gTTS(text="नमस्ते, यह एक ऑडियोबुक है।",
lang='hi')
tts.save("hindi_audio.mp3")
10. Build a Simple Audiobook App
You can create a simple command-line tool:
from gtts import gTTS
file_name = input("Enter text file name: ")
with open(file_name, "r",
encoding="utf-8") as f:
text = f.read()
tts = gTTS(text=text, lang='en')
tts.save("output.mp3")
print("Audiobook created!")
11. Real-World Use Cases
1. Education
Convert notes into audio for revision.
2. Content Creation
Turn blogs into podcasts or audio content.
3. Accessibility
Help visually impaired users access text content.
4. Language Learning
Improve listening and pronunciation skills.
12. Tips for Better Audio Quality
- Use clear and well-formatted text
- Avoid very long paragraphs
- Split content into sections
- Choose the correct language code
13. Limitations of gTTS
- Requires internet connection for conversion
- Limited voice customization
- Not ideal for very large files without splitting
14. Alternatives to gTTS
If you need more advanced features:
- pyttsx3 – Offline text-to-speech
- Amazon Polly – High-quality voices
- Google Cloud TTS – More control and customization
Conclusion
Creating an audiobook using Python and gTTS is a simple yet powerful project that combines automation and accessibility. With just a few lines of code, you can convert text into audio and build tools that enhance learning, productivity, and content consumption.
As you grow your skills, you can expand this project by adding features like a graphical interface, voice selection, or cloud integration. Whether for personal use or professional projects, an audiobook creator is a great way to explore the potential of Python.
Start building your own audiobook today and bring your text to life with sound!