系列文章:
由於網友提出問題。我將其截圖如下:
這讓我很好奇,到底是哪出了問題。於是,我將程式碼由 zh 改成 en。然後將測試文字檔改為英文。然後將修改後的程式碼編譯成EXE,方便使用。
測試結果:
發現沒有問題。我的測試環境為作業系統:Windows 10 64位元 Python 版本:3.9.0。 那接下來可能是版本問題。 最後完成程式:
下載檔案。解壓密碼:demo1234
教學影片:
以下是程式原始碼的內容:
程式目的:讀取Txt.txt 將之轉成merge.mp3
程式名稱:Txt2Mp3Python.py
程式內容:
from gtts import gTTS
import os
mp3_path=str(os.path.abspath(os.getcwd()))+'\\mp3\\'
i = 0
merge_file = list()
# 一行一行地讀取Txt.txt,轉成 001.mp3 002.mp3 003.mp3 ...
TxtFileName = 'Txt.txt'
with open(TxtFileName) as f:
for line in f.readlines():
i=i+1
myobj = gTTS(text=line, lang='en', slow=True)
myobj.save(mp3_path+str(i).zfill(3)+'.mp3')
merge_file.append(mp3_path+str(i).zfill(3)+'.mp3')
merge_file_mp3 = list()
# mp3資料夾內mp3 合併成 merge.mp3
for j in merge_file:
with open(j,'rb') as f:
merge_file_mp3.append(f.read())
with open('merge.mp3','wb') as f:
for j in merge_file_mp3:
f.write(j)
文字稿名稱:Txt.txt
文字稿內容:
Good afternoon,ladies and gentlemen!
I’m very honored to stand here and give you a short speech! To begin with ,I want to ask a question .Does everybody dream a good dream last night Actually ,today I want to talk about dream with you. Of course, What I want to talk is not a dream you have last night,but a dream—— about life.
Everyone has dreams about life, different dreams at different life stage,and we need dreams to support us. Dreams are like the stars we never reach in the sky,but like most mariners,we can chart our course by them. With the dream,we have a direction,with a direction, we were no longer confused.With the dream, there is hope,With hope, we have the strength to fight.
But I know,life is tough,and there are always ups and downs, maybe we fail in the way to our aims,and we may feel depressed ,whenever at this time, the dream in our heart can always comfort us, encourage us ,and support us to move ahead.
文字檔資料來源:
沒有留言:
張貼留言