How to Create an Audiobook App Using Python and Streamlit – I often listen to podcasts and audiobooks while working, in addition to music.
However, I also have a large number of articles, PDF files, e-books, and reports that I want or need to read, but I struggle to focus on both reading and coding at the same time.
As I’ve become more interested in Python programming, I thought of a way to convert articles, reports, e-books, and PDF files into audiobooks, allowing me to listen to them while I work.
As someone learning Python, I’m always looking for projects that challenge my skills and provide useful functionality. One idea I had was to create an app that converts PDF files into audiobooks.
Using Python and Streamlit, I built a simple web-based application that can convert PDF files into MP3 audiobooks with minimal effort.
This post will explain how I created my PDF-to-audiobook converter, covering the steps from extracting text from a PDF file to converting it into speech and making the audiobook available for download.
Overview of the Audiobook App We’re Going to Build
This app allows users to:
- Upload PDF files via a drag-and-drop interface.
- Generate an MP3 audiobook from the uploaded PDF.
- Download the generated MP3 file once the conversion is complete.
The Tools and Modules
To build this app, I used:
- Streamlit for the web interface.
- pdfplumber to extract text from PDF files.
- pyttsx3 for text-to-speech conversion, which runs offline.
- Pathlib to handle file paths and ensure everything is saved correctly.
Here’s how I put it all together and how each part of the app works!
How I Created an Audiobook App Using Python and Streamlit: Step-by-Step Guide
Before diving into the code, I made sure I had all the necessary Python libraries installed. I used the following command to install the required modules:
1. Setting Up the Environment
Before we begin, make sure you have the necessary Python libraries installed. You can install them via pip
by running the following commands:
pip install streamlit pyttsx3 pdfplumber
2. Extracting Text from PDF
The first step in converting a PDF into an audiobook is to extract the text from the uploaded PDF file. For this, we’ll use the pdfplumber
library, which allows us to extract text from each page of a PDF.
Here’s the function that handles text extraction:
import pdfplumber
def extract_text_from_pdf(pdf_file):
with pdfplumber.open(pdf_file) as pdf:
text = ''
for page in pdf.pages:
text += page.extract_text()
return text
This function opens the PDF file, iterates over its pages, and extracts all the text into a single string. This text is later converted into speech.
3. Converting Text to Speech
Once the text is extracted, we use the pyttsx3
library to convert the text into an MP3 audiobook.
Pyttsx3 is a text-to-speech conversion library that works offline and supports multiple voice settings, such as adjusting the speech rate and voice selection.
Here’s the function that handles text-to-speech conversion:
import pyttsx3
def convert_text_to_speech(text, output_file='audiobook.mp3'):
engine = pyttsx3.init()
engine.setProperty('rate', 150) # Adjust speaking rate
engine.save_to_file(text, output_file)
engine.runAndWait()
The convert_text_to_speech()
function initializes the text-to-speech engine, sets the speech rate to 150 words per minute, and saves the spoken version of the text as an MP3 file.
4. Creating the Streamlit App
Streamlit allows us to create a simple, interactive web interface. Users can upload their PDF files through the file uploader, and once uploaded, they can generate and download the audiobook.
Here’s how the Streamlit app is structured:
import streamlit as st
import os
from pathlib import Path
st.title("PDF to Audiobook Converter")
# File uploader for PDF
uploaded_pdf = st.file_uploader("Upload a PDF file", type="pdf")
if uploaded_pdf is not None:
# Slugify the PDF file name for the MP3 output
pdf_filename = uploaded_pdf.name
slugified_name = slugify(Path(pdf_filename).stem)
output_mp3_filename = f"{slugified_name}.mp3"
# Button to generate audiobook
if st.button("Generate Audiobook"):
with st.spinner('Converting PDF to audiobook...'):
text = extract_text_from_pdf(uploaded_pdf)
convert_text_to_speech(text, output_mp3_filename)
# Display download button after conversion
if Path(output_mp3_filename).exists():
st.success(f"Audiobook '{output_mp3_filename}' generated successfully!")
st.audio(output_mp3_filename, format='audio/mp3')
with open(output_mp3_filename, "rb") as file:
st.download_button(
label="Download Audiobook",
data=file,
file_name=output_mp3_filename,
mime="audio/mpeg"
)
else:
st.error("Failed to generate audiobook.")
Breakdown of the Streamlit App:
- Title and File Uploader: The app starts by displaying a title using
st.title()
. Thest.file_uploader()
allows users to upload PDF files. - Slugifying the PDF Filename: The file name of the uploaded PDF is “slugified” to create a clean, hyphenated MP3 file name. This ensures that spaces and special characters in the PDF name are handled correctly.
- Generate Audiobook Button: Once a PDF file is uploaded, the user can click the “Generate Audiobook” button. This triggers the conversion of the PDF into an audiobook using the functions we created earlier.
- Progress Indicator: A progress spinner (
st.spinner
) informs the user that the conversion is in progress. - Download Button: After the conversion is done, a download button appears, allowing users to download the generated MP3 file.
5. Slugifying the PDF File Name
To ensure that the MP3 file names are clean and consistent, we “slugify” the PDF file names by converting spaces and special characters into hyphens:
import re
def slugify(filename):
filename = re.sub(r'[^\w\s-]', '', filename).strip().lower()
return re.sub(r'[-\s]+', '-', filename)
This function:
- Removes special characters.
- Converts spaces to hyphens.
- Converts the entire string to lowercase, creating a URL-friendly and readable file name.
6. Running the App
Once the code is ready, you can launch the Streamlit app by running the following command in your terminal:
streamlit run app.py
Streamlit will open a web browser window, allowing you to upload a PDF file and convert it into an audiobook.
How to Use the Streamlit Audiobook App
Now, you just need to upload a PDF file. After that, click the Generate Audiobook button. The PDF to Audiobook Converter tool will generate an audiobook, converting your PDF file.
Then, you can listen to it directly and/or download the audiobook and save it locally by clicking the Download Audiobook button.
Putting It All Together
With just a few Python libraries, we’ve created a fully functional web-based app that converts PDF documents into audiobooks.
This is all the app.py file putting together:
import streamlit as st
import pyttsx3
import pdfplumber
import os
from pathlib import Path
import re
# Function to slugify the filename
def slugify(filename):
# Remove non-alphanumeric characters, replace spaces with hyphens, and make lowercase
filename = re.sub(r'[^\w\s-]', '', filename).strip().lower()
return re.sub(r'[-\s]+', '-', filename)
# Function to extract text from PDF
def extract_text_from_pdf(pdf_file):
with pdfplumber.open(pdf_file) as pdf:
text = ''
for page in pdf.pages:
text += page.extract_text()
return text
# Function to convert text to speech and save it as an audiobook
def convert_text_to_speech(text, output_file):
engine = pyttsx3.init()
engine.setProperty('rate', 150) # Adjust speaking rate here
engine.save_to_file(text, output_file)
engine.runAndWait()
# Streamlit app
st.title("PDF to Audiobook Converter")
# st.subheader("Generate an Audibook file from PDF.") # Use this or the following
st.markdown("##### Generate an Audibook file from PDF.")
# Step 1: File uploader for PDF
uploaded_pdf = st.file_uploader("Upload a PDF file", type="pdf")
# Step 2: Button to generate audiobook
if uploaded_pdf is not None:
# Extract filename and slugify it to create the output filename
pdf_filename = uploaded_pdf.name
slugified_name = slugify(Path(pdf_filename).stem) # Get the slugified name without extension
output_mp3_filename = f"{slugified_name}.mp3"
if st.button("Generate Audiobook"):
with st.spinner('Converting PDF to audiobook...'):
# Step 3: Extract text from the uploaded PDF
text = extract_text_from_pdf(uploaded_pdf)
# Step 4: Convert text to audiobook with slugified output file name
convert_text_to_speech(text, output_mp3_filename)
# Step 5: Display download button after audiobook is created
if Path(output_mp3_filename).exists():
st.success(f"Audiobook '{output_mp3_filename}' generated successfully!")
st.audio(output_mp3_filename, format='audio/mp3')
with open(output_mp3_filename, "rb") as file:
btn = st.download_button(
label="Download Audiobook",
data=file,
file_name=output_mp3_filename,
mime="audio/mpeg"
)
else:
st.error("Failed to generate audiobook.")
Using Streamlit for the interface, pdfplumber for text extraction, and pyttsx3 for text-to-speech conversion, this app is not only simple to use but also demonstrates the power of Python for rapid web app development.
You can extend this project by adding more features, such as:
- Selecting different voices or adjusting the speaking rate.
- Supporting different output audio formats.
- Adding additional functionality like converting ePub or DOCX files.
You can find the full code and updates on Github.
This project offers a great foundation for building practical applications with Python and Streamlit. Happy coding!