
First look at the line that says text_box.insert(END, content) basically END is defined inside tkinter and it returns the last index that means where is the end of file, similarly 1.0 means the beginning of the text.
Convert pdf to audiobook install#
You need to install gTTS and PyMuPDF, so inside your terminal run pip install PyMuPDF and pip install gTTS for installing them.Īs you can see above code is self explanatory but still I want to highlight some points. # using gTTS to convert that text into audio and storing it inside file named as audio.mp3
Convert pdf to audiobook pdf#
# after whole pdf content is stored then retrieving it from textbox and storing it inside variable # looping through all the pages, collecting text from each page and showing it on text box for n in range(total_pages): #reading pdf file and creating instance of Document class from fitz import fitz # fitz is actually PyMuPDF from gtts import gTTS In this step we will create function open_pdf this function will create a dialogue box for selecting pdf file and then reading all of it text and showing inside the text box created earlier, then it'll use gTTS for creating audio file of all the text from text box. Now if you run it, you'll see something like this, # for keeping window open till we don't close it manually Menu.add_cascade(label= "File", menu=file_menu)

# adding `File` tab into menu defined above Text_box = Text(window, height= 30, width= 60) Window.geometry( "500x500") # setting default size of the window # creating text box for displaying pdf content Window.title( "convert pdf to audiobook") # creating main window instance from Tk class defined in tkinter Tkinter comes preinstalled with Python so no need to install it from pip. In this step we'll be creating our GUI so open up your favourite code editor and create a file as main.py and import tkinter.
Convert pdf to audiobook mac os x#
We are using playsound just for playing the audio file that will be created using gTTS, you can use any Python library for that like pydub or use os module to play on native audio player installed on terminal, but I guess this only works on Mac OS X and linux Let's dive into the code now ✨ Playsound is also a Python library for playing audio files like.

GTTS stands for Google Text To Speech it's a Python library as well as a CLI tool for converting text into speech. Modules used:īefore moving ahead I want to tell you something that in most of the online tutorials you'll find people using PyPDF2 for working with pdf files but the reason we are not using it is because it does not always work, like till the date I'm writing this post if you use PyPDF2 for reading pdf generated by google like from google docs, it's not able to read text from it. We also create a text box for displaying the pdf text content and a button play for start playing it as audio. So now we know what we are going to do so let's break it down into smaller chunks and focus on each of them individually.įirst of all we are going to create a window and a dialog box to open the desired pdf file. You can find the sample pdf file and output mp3 file in the source code.Yeah that's it, I'll be explaining the rest 😉. You can play the mp3 file to listen to the speech and compare it with your PDF text. Running the above script you will get a file called Audio.mp3 saved under the current directory. MyAudio.save("Audio.mp3") Testing the Program

MyAudio = gTTS(text=textString, lang=language, slow=False) #Converting multiline text to single line text #Extracting text data from each page of the pdf file Pdf_Reader = PyPDF2.PdfFileReader(pdf_File)Ĭount = pdf_Reader.numPages # counts number of pages in pdf

Finally we convert string of text into English audio speech at fast speed.
