#gtts는 구글에서 제공하는 Text to Speech 서비스
pip install gtts
from gtts import gTTS
text = "안녕하세요. 파이썬과 40개의 작품들 입니다."
tts = gTTS(text=text, lang = 'ko')
tts.save(r"3. 텍스트를 음성으로 변환\hi.mp3")
이 결과로 폴더에 hi.mp3 파일이 만들어진다.
from gtts import gTTS #google Text to Speach
from playsound import playsound #playsound 모듈로부터 playsound 불러오기
import os # os 라이브러리 불러오기
os.chdir(os.path.dirname(os.path.abspath(__file__)))
text = "안녕하세요. 파이썬과 40개의 작품들 입니다"
tts = gTTS(text=text, lang = 'ko')
tts.save("hi.mp3")
playsound("hi.mp3")
Error 263 for command:
open hi.mp3
지정한 장치가 열려 있지 않거나 MCI에서 인식되지 않습니다.
pip install playsound==1.2.2
from gtts import gTTS
from playsound import playsound
import os
#경로를 .py 파일의 실행경로로 이동, 현재 경로로 이동
os.chdir(os.path.dirname(os.path.abspath(__file__)))
file_path = '나의텍스트.txt' #나의텍스트.txt 경로 바인딩
with open(file_path, 'rt', encoding= 'UTF8') as f :
read_file = f.read()
tts = gTTS(text=read_file, lang='ko')
tts.save("myText.mp3")
playsound("myText.mp3")