In todays post we will be making use of the gTTS Python library which is a tool to interface with Google translates text-to-speech API. The script will take a Word document file as input and then convert its text to speech. The audio will be saved as an MP3 file.
We begin by enabling the user to browse to a file for user input, to do this we shall use tkinter.
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
We then make use of the docx library to retrieve all text from our specified Word document.
import docx
def getText(filename):
doc = docx.Document(filename)
fullText = []
for para in doc.paragraphs:
fullText.append(para.text)
return '\n'.join(fullText)
Finally we pass the text over to the gTTS module for conversion. We have also the ‘slow= False’ which should stop the playback from being slowed down.
from gtts import gTTS
tts = gTTS(Text, slow=False)
tts.save('Output.mp3')
The full source code for this project can be found below.
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from gtts import gTTS
import docx
def getText(filename):
doc = docx.Document(filename)
fullText = []
for para in doc.paragraphs:
fullText.append(para.text)
return '\n'.join(fullText)
Tk().withdraw()
filename = askopenfilename()
Text = getText(filename)
tts = gTTS(Text, slow=False)
tts.save('Output.mp3')