Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Audio Book Generator

Description

A Python application that converts PDF documents into audio books using Google Text-to-Speech (gTTS). Simply provide a PDF file, and the program will extract the text and convert it into an MP3 audio file that you can listen to anywhere.

Features

  • Extract text from PDF files (all pages)
  • Convert text to natural-sounding speech
  • Support for multiple languages
  • Customizable output file name
  • Progress tracking during conversion
  • Text preview before conversion
  • Automatic audio playback option
  • Cross-platform support
  • Error handling for corrupted pages

Stack

  • Language: Python
  • Libraries:
    • gTTS==2.3.1 (Google Text-to-Speech)
    • PyPDF2==3.0.1 (PDF reading)
    • requests==2.28.2
  • Complexity: Intermediate

Installation

# Install dependencies
pip install -r requirements.txt

# Run the program
python main.py

Usage

Method 1: Interactive Mode

python main.py

Then follow the prompts:

  1. Enter PDF file path
  2. Select language
  3. Enter output file name
  4. Wait for conversion
  5. Choose to play audio or not

Method 2: Command Line Argument

python main.py path/to/your/file.pdf

Example Session

==================================================
           AUDIO BOOK GENERATOR
==================================================

This tool converts PDF files to audio books using TTS.

Enter PDF file path (or press Enter for 'name.pdf'): document.pdf

Supported languages:
  en - English (default)
  hi - Hindi
  es - Spanish
  fr - French
  de - German
  ja - Japanese
  zh-CN - Chinese (Simplified)

Enter language code (default: en): en

Enter output file name (default: Audio.mp3): my_audiobook.mp3

==================================================
Extracting text from PDF...
==================================================

Total pages in PDF: 10
Extracted text from page 1
Extracted text from page 2
...
✓ Successfully extracted 15234 characters

Converting text to speech...
Language: en
Text length: 15234 characters

✓ Audio book successfully created: my_audiobook.mp3
File size: 1234.56 KB

Do you want to play the audio now? (y/n): y

How It Works

PDF Text Extraction

  1. Open PDF in binary read mode: open('name.pdf', 'rb')
  2. Create PyPDF2.PdfReader object
  3. Get total page count: len(pdf_reader.pages)
  4. Loop through all pages, extract text from each page
  5. Handle exceptions for pages that can't be read
  6. Join all text into single string

Text-to-Speech Conversion

  1. Create gTTS object with text, language, and speed settings
  2. Generate audio data
  3. Save as MP3 file
  4. Display file information

Audio Playback

  • Windows: Uses start command
  • macOS: Uses afplay command
  • Linux: Uses xdg-open command

Key Functions

  • extract_text_from_pdf(pdf_path): Extracts text from all PDF pages
  • text_to_speech(text, output_file, language, slow): Converts text to MP3
  • play_audio(audio_file): Platform-specific audio playback
  • main(): Orchestrates the entire workflow

Supported Languages

Code Language
en English
hi Hindi
es Spanish
fr French
de German
it Italian
ja Japanese
ko Korean
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
ar Arabic
ru Russian
pt Portuguese

For more languages, see gTTS documentation.

Error Handling

  • File not found errors
  • PDF reading errors
  • Page extraction failures
  • Empty text handling
  • Network errors (gTTS requires internet)
  • Keyboard interrupts

File Structure

03_Audio_Book_Generator/
├── main.py              # Main application
├── requirements.txt     # Dependencies
├── README.md           # Documentation
└── name.pdf            # Sample PDF (user-provided)

Requirements

  • Python 3.6+
  • Internet connection (for gTTS API)
  • PDF file with extractable text (not scanned images)

Limitations

  • Requires internet connection for TTS conversion
  • Cannot extract text from scanned PDFs (OCR not included)
  • Large PDFs may take time to process
  • gTTS has rate limiting (avoid excessive requests)
  • Some PDF formatting may be lost in text extraction

Customization

Change Speech Speed

text_to_speech(text, output_file, language='en', slow=True)  # Slower speech

Change Voice Accent

from gtts import gTTS
audio = gTTS(text=text, lang='en', tld='co.uk')  # British accent
audio = gTTS(text=text, lang='en', tld='com.au')  # Australian accent

Process Specific Pages

# Extract only pages 5-10
for page_num in range(4, 10):  # 0-indexed
    page = pdf_reader.pages[page_num]
    text_list.append(page.extract_text())

Learning Outcomes

  • Working with PDF files using PyPDF2
  • Text-to-Speech conversion with gTTS
  • File I/O operations
  • Error handling and validation
  • Command-line argument processing
  • Cross-platform compatibility
  • API usage (Google TTS)

Future Enhancements

  • OCR support for scanned PDFs
  • Multiple voice options
  • Audio format selection (WAV, OGG, etc.)
  • Batch processing multiple PDFs
  • GUI interface
  • Progress bar for large files
  • Chapter detection and splitting
  • Bookmark/timestamp generation
  • Speed and pitch control
  • Offline TTS option

Troubleshooting

"No text found" Error

  • PDF may contain scanned images (use OCR tool first)
  • PDF may be encrypted or protected
  • Try a different PDF file

Internet Connection Error

  • gTTS requires internet to access Google's TTS API
  • Check your network connection
  • Try again after a few moments

Audio Not Playing

  • Ensure you have a media player installed
  • Manually open the MP3 file
  • Check file permissions

License

This project is open source and available for educational purposes.