Build your audiobook from any PDF with Python

Did you know that you can build your audiobook with Python from any PDF? Sounds cool, isn’t it?
In this quick tutorial, I will show you how to build an audiobook with Python.

In our example, we will be using PyPDF2 and pyttsx3.

PyPDF2 is a pure-Pyt…


This content originally appeared on DEV Community and was authored by Stokry

Did you know that you can build your audiobook with Python from any PDF? Sounds cool, isn't it?
In this quick tutorial, I will show you how to build an audiobook with Python.

In our example, we will be using PyPDF2 and pyttsx3.

PyPDF2 is a pure-Python library built as a PDF toolkit. It is capable of:

  • extracting document information (title, author, …)
  • splitting documents page by page
  • merging documents page by page
  • cropping pages
  • merging multiple pages into a single page
  • encrypting and decrypting PDF files
  • and more!

By being Pure-Python, it should run on any Python platform without any dependencies on external libraries. It can also work entirely on StringIO objects rather than file streams, allowing for PDF manipulation in memory. It is therefore a useful tool for websites that manage or manipulate PDFs.

Pyttsx3 is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline, and is compatible with both Python 2 and 3.

Let's jump to the code:

First we need to import our dependencies:

import PyPDF2
import pyttsx3

Then we need to open our pdf

pdf_file =  open('test.pdf', 'rb')

where mode='rb' is used for open the file in binary format for reading.

We define PDF file reader:

pdf_read = PyPDF2.PdfFileReader(pdf_file)

Then we need to give number of pages in PDF file:

num_pages = pdf_read.numPages

Then we define our init and we also can do a print :

engine = pyttsx3.init()
print('Read PDF')

After that, we define our loop that will read all pages one by one:

for n in range(0, num_pages):
    page = pdf_read.getPage(n)
    text = page.extractText()
    x = n + 1
    print(f"Reading page {x}/{num_pages}.")
    engine.say(text)
    engine.save_to_file(text, 'book.mp3')
    engine.runAndWait()

This part of the code retrieves a page by the number from this PDF file, extracts text from the page, and reads text from the page. Also we save to mp3 file.

Thank you all.


This content originally appeared on DEV Community and was authored by Stokry


Print Share Comment Cite Upload Translate Updates
APA

Stokry | Sciencx (2021-04-28T11:45:19+00:00) Build your audiobook from any PDF with Python. Retrieved from https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/

MLA
" » Build your audiobook from any PDF with Python." Stokry | Sciencx - Wednesday April 28, 2021, https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/
HARVARD
Stokry | Sciencx Wednesday April 28, 2021 » Build your audiobook from any PDF with Python., viewed ,<https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/>
VANCOUVER
Stokry | Sciencx - » Build your audiobook from any PDF with Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/
CHICAGO
" » Build your audiobook from any PDF with Python." Stokry | Sciencx - Accessed . https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/
IEEE
" » Build your audiobook from any PDF with Python." Stokry | Sciencx [Online]. Available: https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/. [Accessed: ]
rf:citation
» Build your audiobook from any PDF with Python | Stokry | Sciencx | https://www.scien.cx/2021/04/28/build-your-audiobook-from-any-pdf-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.