Skip to content

Instantly share code, notes, and snippets.

@harendra21
Created November 23, 2021 11:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harendra21/c278be5e139dda6ffc0552231dff6e5a to your computer and use it in GitHub Desktop.
Save harendra21/c278be5e139dda6ffc0552231dff6e5a to your computer and use it in GitHub Desktop.

How to convert PDF to Audio using Python

import pyttsx3
import PyPDF2
pdfreader = PyPDF2.PdfFileReader(open('story.pdf','rb'))
speaker = pyttsx3.init()
for page_num in range(pdfreader.numPages):   
    text = pdfreader.getPage(page_num).extractText()  ## extracting text from the PDF
    cleaned_text = text.strip().replace('\n',' ')  ## Removes unnecessary spaces and break lines
    print(cleaned_text)                ## Print the text from PDF
    #speaker.say(cleaned_text)        ## Let The Speaker Speak The Text
    speaker.save_to_file(cleaned_text,'story.mp3')  ## Saving Text In a audio file 'story.mp3'
    speaker.runAndWait()
speaker.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment