Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert pdf into audiobooks with python

Shulou Source: shulou.com Published: 2022-06-01 18:19:07 09月12日 Update

This article mainly introduces how to use python to convert pdf into audiobooks related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to use python to convert pdf into audiobooks will have a harvest, let's take a look at it.

The pyttsx3 module in python can realize voice broadcast, while the pdfplumber or PyPDF2 module converts pdf to text. I use the pdfplumber module. First of all, let's download the module. You can pip install the module name yourself.

Then we add the required pdf files to the same folder of the code (easy to call, you can also use the absolute path)

Code

# @ Time:2022/2/120:29# @ Author: favorite book # @ File: audiobooks .py # @ ps:tutu qqnum:2117472285import pyttsx3import pdfplumberpdf=pdfplumber.open (input ('Please enter the title:') pg_no=int (input ("Please enter the page you want to read:)) # Total pages numpages=len (pdf.pages) for num in range ((pg_no-1)) Numpages): # get the content of the specified number of pages of the article first_page= pdf.pages [num] # get the text content text=first_page.extract_text () # initialize speaker=pyttsx3.init () # remove the newline character text=text.replace ('\ nfeeds)) # adjust the type of human voice Voice [0] is a Chinese female voice, so you can switch to others by yourself, but note that the English female voice cannot read Chinese voices=speaker.getProperty ('voices') speaker.setProperty (' voice',voices [0] .id) # adjust the speed rate=speaker.getProperty ('rate') speaker.setProperty (' rate',150) # adjust the volume, in 0-1 volume=speaker.getProperty ('volume') speaker.setProperty (' volume',1) # Hey I did not tune # read speaker.say (text) # save audio # speaker.save_to_file (text, 'T1.mp3') speaker.runAndWait ()

Running result:

This is the end of the article on "how to convert pdf into audiobooks with python". Thank you for reading! I believe everyone has a certain understanding of "how to use python to convert pdf into audiobooks". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Reading module content article knowledge adjustment code female voice file article number of pages Chinese input preference book title voice value location unit newline character Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology macOS Shulou Information Microsoft MySQL