Skip to content

Instantly share code, notes, and snippets.

@ahmedbr
Last active July 30, 2022 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmedbr/04b962f3f88c2366b9c93868954c5c82 to your computer and use it in GitHub Desktop.
Save ahmedbr/04b962f3f88c2366b9c93868954c5c82 to your computer and use it in GitHub Desktop.
Convert a textual PDF file to an audio book using Pyhon
import PyPDF3
import pyttsx3
import pdfplumber
import os
pdf_file_path = r"path_to_pdf_file.pdf"
root, ext = os.path.splitext(pdf_file_path)
output_audio_file = root + ".mp3"
# get num of pages
book = open(pdf_file_path, "rb")
pdf_reader = PyPDF3.PdfFileReader(book)
pages_num = pdf_reader.numPages
# extract text from the textual file
extracted_text = ""
with pdfplumber.open(pdf_file_path) as pdf:
for i in range(0, pages_num):
page = pdf.pages[i]
extracted_text += page.extract_text()
# tts part
engine = pyttsx3.init()
engine.save_to_file(extracted_text, output_audio_file)
engine.runAndWait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment