新聞中心
本篇文章給大家分享的是有關(guān)如何利用Python將. pdf電子書籍轉(zhuǎn)換成音頻有聲讀物,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)服務(wù)項目包括湟中網(wǎng)站建設(shè)、湟中網(wǎng)站制作、湟中網(wǎng)頁制作以及湟中網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,湟中網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到湟中省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
有沒有發(fā)現(xiàn)一個生活中的現(xiàn)象,我們很少有時間去真正讀一些存放在電腦或者ipad上的pdf書籍。我們打算讀這些書,但從來沒有讀過。所以我們?yōu)槭裁床挥肞ython把它們做成有聲書,一邊聽一邊做別的事情呢?
我們計劃Python腳本步驟是這樣的:
允許用戶選擇讀取一個.pdf文件
將文件內(nèi)容轉(zhuǎn)換為一個字符串
輸出的mp3音頻文件
允許用戶選擇讀取一個.pdf文件
Python可以輕松地讀取文件。
我只需要使用open(“filelocation”,“rb”)在讀取模式下打開文件。但我不想每次使用代碼時都要將文件復(fù)制并粘貼到代碼目錄中。因此,為了使它更容易,我們將使用tkinter庫來打開一個讓我們選擇文件的接口:
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
太好了。現(xiàn)在,我們將文件位置存儲在filelocation變量中。
將文件轉(zhuǎn)換為一個字符串
如前所述,要在Python中打開文件,我們只需要使用open()方法。但是我們還希望將pdf文件轉(zhuǎn)換為常規(guī)文本。
為此,我們將使用一個名為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
如果您打印這個變量,您將得到一個字符串?dāng)?shù)組。每個字符串都是文件中的一行。要將它們?nèi)看鎯Φ揭粋€.mp3文件中,我們必須確保它們都存儲為一個字符串。讓我們循環(huán)這個數(shù)組并將它們?nèi)刻砑拥揭粋€字符串中:
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文件
現(xiàn)在,我們準(zhǔn)備使用gTTS(谷歌文本到語音)庫。我們所需要做的就是傳遞我們創(chuàng)建的字符串,將輸出存儲在一個變量中,然后使用save()方法將文件輸出到計算機(jī)。
先安裝:
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
就這么簡單!快去拿你的pdf去嘗試吧。
以上就是如何利用Python將. pdf電子書籍轉(zhuǎn)換成音頻有聲讀物,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:如何利用Python將.pdf電子書籍轉(zhuǎn)換成音頻有聲讀物
文章網(wǎng)址:http://www.ef60e0e.cn/article/ippjjo.html