PDF to Audio Converter

 pip install PyMuPDF gtts tk


import fitz  # PyMuPDF
from gtts import gTTS
import tkinter as tk
from tkinter import filedialog, messagebox
import os

# Function to extract text from PDF
def extract_text_from_pdf(pdf_path):
    doc = fitz.open(pdf_path)
    text = ""
    for page in doc:
        text += page.get_text("text") + "\n"
    return text

# Function to convert text to speech
def convert_text_to_audio(text, output_file):
    if text.strip():
        tts = gTTS(text=text, lang='en')
        tts.save(output_file)
        messagebox.showinfo("Success", f"Audio file saved as {output_file}")
        os.system(f"start {output_file}")  # Opens the audio file
    else:
        messagebox.showwarning("Warning", "No text found in PDF.")

# Function to open file dialog
def select_pdf():
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
    if file_path:
        text = extract_text_from_pdf(file_path)
        if text:
            convert_text_to_audio(text, "output_audio.mp3")

# Tkinter GUI Setup
root = tk.Tk()
root.title("PDF to Audio Converter 🔊")
root.geometry("400x200")

lbl_title = tk.Label(root, text="📄 PDF to Audio Converter 🔊", font=("Arial", 14, "bold"))
lbl_title.pack(pady=10)

btn_select_pdf = tk.Button(root, text="Select PDF & Convert", command=select_pdf, font=("Arial", 12))
btn_select_pdf.pack(pady=20)

root.mainloop()

No comments: