前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用Python将. pdf电子书籍转换成音频有声读物

利用Python将. pdf电子书籍转换成音频有声读物

作者头像
HuangWeiAI
发布2020-04-14 15:23:26
1.3K0
发布2020-04-14 15:23:26
举报
文章被收录于专栏:浊酒清味
前言

有没有发现一个生活中的现象,我们很少有时间去真正读一些存放在电脑或者ipad上的pdf书籍。我们打算读这些书,但从来没有读过。所以我们为什么不用Python把它们做成有声书,一边听一边做别的事情呢?

我们计划Python脚本步骤是这样的:

  1. 允许用户选择读取一个.pdf文件
  2. 将文件内容转换为一个字符串
  3. 输出的mp3音频文件

允许用户选择读取一个.pdf文件

Python可以轻松地读取文件。

我只需要使用open(“filelocation”,“rb”)在读取模式下打开文件。但我不想每次使用代码时都要将文件复制并粘贴到代码目录中。因此,为了使它更容易,我们将使用tkinter库来打开一个让我们选择文件的接口:

代码语言:javascript
复制
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变量中。

将文件转换为一个字符串

如前所述,要在Python中打开文件,我们只需要使用open()方法。但是我们还希望将pdf文件转换为常规文本。

为此,我们将使用一个名为pdftotext的库。

先安装:

代码语言:javascript
复制
sudo pip install pdftotext

然后:

代码语言:javascript
复制
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

如果您打印这个变量,您将得到一个字符串数组。每个字符串都是文件中的一行。要将它们全部存储到一个.mp3文件中,我们必须确保它们都存储为一个字符串。让我们循环这个数组并将它们全部添加到一个字符串中:

代码语言:javascript
复制
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(谷歌文本到语音)库。我们所需要做的就是传递我们创建的字符串,将输出存储在一个变量中,然后使用save()方法将文件输出到计算机。

先安装:

代码语言:javascript
复制
sudo pip install gtts

然后:

代码语言:javascript
复制
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

就这么简单!快去拿你的pdf去尝试吧。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python与机器学习之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档