Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make use of Python. Conversion of pdf e-books into audio audiobooks

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Python to share with you. Pdf e-books into audio audio books, the editor feels very practical, so share with you to learn, I hope you can learn something after reading this article, do not say much, follow the editor to have a look.

Have you found a phenomenon in life that we seldom have time to actually read some pdf books stored on computers or ipad? We are going to read these books, but we have never read them. So why don't we use Python to make audio books and listen to them and do other things at the same time?

Our planned Python script steps are as follows:

Allows the user to choose to read a .pdf file

Convert the contents of the file to a string

Exported mp3 audio file

Allows the user to choose to read a .pdf file

Python can easily read files.

I just need to use open ("filelocation", "rb") to open the file in read mode. But I don't want to copy and paste files into the code directory every time I use the code. So, to make it easier, we will use the tkinter library to open an interface that allows us to select files:

From tkinter import Tkfrom tkinter.filedialog import askopenfilename

Tk (). Withdraw () # we don't want a full GUI, so keep the root window from appearingfilelocation = askopenfilename () # open the dialog GUI

Great. Now, we store the file location in the filelocation variable.

Convert a file to a string

As mentioned earlier, to open the file in Python, we only need to use the open () method. But we also want to convert the pdf file to regular text.

To do this, we will use a library called pdftotext.

Install first:

Sudo pip install pdftotext

Then:

From tkinter import Tkfrom tkinter.filedialog import askopenfilenameimport pdftotext

Tk (). Withdraw () # we don't want a full GUI, so keep the root window from appearingfilelocation = askopenfilename () # open the dialog GUI

With open (filelocation, "rb") as f: # open the file in reading (rb) mode and call it f pdf = pdftotext.PDF (f) # store a text version of the pdf file f in pdf variable

If you print this variable, you will get an array of strings. Each string is a line in the file. To store them all in a .mp3 file, we must make sure that they are all stored as a string. Let's loop through the array and add them all to a string:

From tkinter import Tkfrom tkinter.filedialog import askopenfilenameimport pdftotext

Tk (). Withdraw () # we don't want a full GUI, so keep the root window from appearingfilelocation = askopenfilename () # open the dialog GUI

With open (filelocation, "rb") as f: # open the file in reading (rb) mode and call it f pdf = pdftotext.PDF (f) # store a text version of the pdf file f in pdf variable

String_of_text =''for text in pdf: string_of_text + = text

Export .mp3 file

Now we are ready to use the gTTS (Google text-to-Voice) library. All we need to do is pass the string we created, store the output in a variable, and then use the save () method to output the file to the computer.

Install first:

Sudo pip install gtts

Then:

From tkinter import Tkfrom tkinter.filedialog import askopenfilenameimport pdftotextfrom gtts import gTTS

Tk (). Withdraw () # we don't want a full GUI, so keep the root window from appearingfilelocation = askopenfilename () # open the dialog GUI

With open (filelocation, "rb") as f: # open the file in reading (rb) mode and call it f pdf = pdftotext.PDF (f) # store a text version of the pdf file f in pdf variable

String_of_text =''for text in pdf: string_of_text + = text

Final_file = gTTS (text=string_of_text, lang='en') # store file in variablefinal_file.save ("Generated Speech.mp3") # save file to computer

It's that simple! Go get your pdf and try it.

The above is how to use Python. Pdf e-books are converted into audio audiobooks, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report