1. 程式人生 > 其它 >利用python將PDF文件轉語音

利用python將PDF文件轉語音

技術標籤:pdfpython

win10 python3.9 測試通過:

import pdfplumber
# 讀取PDF文件
pdf = pdfplumber.open("d:/電子書/終身成長.pdf")

# 獲取頁數
print("總頁數:",len(pdf.pages))
print("-----------------------------------------")

# 讀取第4頁
first_page = pdf.pages[100]
print("本頁:",first_page.page_number+1)
print("-----------------------------------------")

# 匯出第4頁文字
text = first_page.extract_text()
print(text)

##!!!! sudo apt-get update && sudo apt-get install espeak
##!!!!否則沒聲,只讀英文
#Reading  text to speech
import pyttsx3

# 初始化來獲取語音引擎
engine = pyttsx3.init()

# # 去掉文字中的換行符
# text = text.replace('\n','')
# #text = "I am a teacher,and you!"
# # 調整人聲型別
# voices = engine.getProperty('voices')  
# engine.setProperty('voice', voices[0].id)

# # 調整語速,範圍一般在0~500之間
# rate = engine.getProperty('rate')                         
# engine.setProperty('rate', 200)     
rate = engine.getProperty('rate')                         
engine.setProperty('rate', 150)  
# # 調整聲量,範圍在0~1之間
# volume = engine.getProperty('volume')                         
# engine.setProperty('volume',0.8) 

# 朗讀文字
engine.say(text)
#engine.save_to_file(text, 'test.mp3')
engine.runAndWait()