使用 Python 将任何 .pdf 文件📚 转换为有声读物🔈
pdfReader = pd.PdfFileReader(打开('Excel-eBook.pdf', 'rb'))
(编辑:我很高兴你们都喜欢这个项目!它成为了本周最热门的 Python 文章!)
前段时间,我在琢磨谷歌的 Python 文本转语音库。
这个库基本上可以读取任意一段文本并将其转换为.mp3
文件。后来,我开始琢磨如何利用它做一些有用的事情。
我已安装、保存和未读的 PDF 书籍 😕
我喜欢读书,真的喜欢。我觉得语言和思想的交流很奇妙。我有一个目录,里面存放着我计划读但一直没读完的 PDF 书。所以我想,为什么不把它们做成有声书,一边做其他事情一边听呢😄!
所以我开始规划剧本应该是什么样子。
- 允许用户选择一个
.pdf file
- 将文件转换为一个字符串
- 输出
.mp3
文件。
不用多说,让我们开始吧。
允许用户选择一个 .pdf 文件
Python 可以轻松读取文件。我只需要使用该方法open("filelocation", "rb")
以读取模式打开文件即可。但我不想每次使用代码时都要将文件复制粘贴到代码目录中。因此,为了方便起见,我们将使用tkinter library
打开一个界面来选择文件。
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filelocation = askopenfilename() # open the dialog GUI
太好了。现在我们把文件位置存储在filelocation
变量中了。
允许用户选择 .pdf 文件✔️
将文件转换为一个字符串
正如我之前所说,要在 Python 中打开文件,我们只需要使用open()
方法。但我们还需要将 PDF 文件转换为常规文本。所以我们不妨现在就这么做。
为此,我们将使用一个名为的库pdftotext
。
让我们安装它:
sudo pip install pdftotext
然后:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import pdftotext
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filelocation = 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
太好了。现在我们将文件存储在变量中了pdf
。
如果打印这个变量,您将得到一个字符串数组。每个字符串在文件中占一行。为了将它们全部放入一个.mp3
文件中,我们必须确保它们都存储为一个字符串。因此,让我们循环遍历这个数组,并将它们全部添加到一个字符串中。
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import pdftotext
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filelocation = 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
太棒了😄。现在我们把所有东西都串成一串了。
将文件转换为一个字符串✔️
输出 .mp3 文件🔈
现在我们可以使用gTTS
(Google 文本转语音)库了。我们需要做的就是传递我们生成的字符串,将输出存储在变量中,然后使用该save()
方法将文件输出到计算机。
让我们安装它:
sudo pip install gtts
然后:
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import pdftotext
from gtts import gTTS
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filelocation = 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 variable
final_file.save("Generated Speech.mp3") # save file to computer
就这么简单!我们完成了🎇
(编辑:很高兴大家喜欢这篇文章!我所有文章的意图都是尽可能简单易懂,以便各个级别的读者都能理解。如果您想了解更多关于自定义此 API 的信息,请查看此页面:https://gtts.readthedocs.io/en/latest/)
文章来源:https://dev.to/mustafaanas/convert-any-pdf-file-into-an-audio-book-with-python-1gk4我毕生致力于尽可能地支持和贡献网络社区的常识。我的一些文章可能听起来有点傻,或者太难,但任何知识都是有用的。如果你喜欢我的文章,请随时给我买杯咖啡,帮助我继续写作 :)