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.
- 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
- Language: Python
- Libraries:
gTTS==2.3.1(Google Text-to-Speech)PyPDF2==3.0.1(PDF reading)requests==2.28.2
- Complexity: Intermediate
# Install dependencies
pip install -r requirements.txt
# Run the program
python main.pypython main.pyThen follow the prompts:
- Enter PDF file path
- Select language
- Enter output file name
- Wait for conversion
- Choose to play audio or not
python main.py path/to/your/file.pdf==================================================
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
- Open PDF in binary read mode:
open('name.pdf', 'rb') - Create
PyPDF2.PdfReaderobject - Get total page count:
len(pdf_reader.pages) - Loop through all pages, extract text from each page
- Handle exceptions for pages that can't be read
- Join all text into single string
- Create gTTS object with text, language, and speed settings
- Generate audio data
- Save as MP3 file
- Display file information
- Windows: Uses
startcommand - macOS: Uses
afplaycommand - Linux: Uses
xdg-opencommand
extract_text_from_pdf(pdf_path): Extracts text from all PDF pagestext_to_speech(text, output_file, language, slow): Converts text to MP3play_audio(audio_file): Platform-specific audio playbackmain(): Orchestrates the entire workflow
| 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.
- File not found errors
- PDF reading errors
- Page extraction failures
- Empty text handling
- Network errors (gTTS requires internet)
- Keyboard interrupts
03_Audio_Book_Generator/
├── main.py # Main application
├── requirements.txt # Dependencies
├── README.md # Documentation
└── name.pdf # Sample PDF (user-provided)
- Python 3.6+
- Internet connection (for gTTS API)
- PDF file with extractable text (not scanned images)
- 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
text_to_speech(text, output_file, language='en', slow=True) # Slower speechfrom 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# 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())- 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)
- 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
- PDF may contain scanned images (use OCR tool first)
- PDF may be encrypted or protected
- Try a different PDF file
- gTTS requires internet to access Google's TTS API
- Check your network connection
- Try again after a few moments
- Ensure you have a media player installed
- Manually open the MP3 file
- Check file permissions
This project is open source and available for educational purposes.