Transform Text Data into Audio File

With Python Programming Language

Ramya N
6 min readFeb 25, 2021
Photo by Brett Jordan on Unsplash

This is an article for how to transform text data into audio (.mp3) file with Python programming language... “From English verbal to English vocal”

I was inquisitive and when did my research on it, came up with one of the solution that I have articulated here with the related code snippets and to further enhance it, it can be automated to translate text to audio aka text-to-speech translation.
One of the use-case for this translation is, for ‘answering machines’ to transform/convert standard text to be played on and which is repetitive task thus information can be transformed to audio file and played during its context. It’s useful for visually challenged users and of course this feature can be used for dynamic translation (real-time audio chatting for the instant audio messages sent by the customers as in customer-care scenarios!).
And if you are beginner to this programming language or coding, Python is a versatile language (one of my most favourite factor about Python) and used, to automate tasks especially in the field of Data Science, Data Analysis and Artificial Intelligence, it’s the GO-TO language, however there are other programming languages such as R and Julia available. Every language has got its advantages. As a saying, everything/all objects existing on the Earth has got both plus and not-plus factors! so based on the requirements, infrastructure investment and end users of the software applications, tech-stack will be decided and apps are developed and deployed accordingly to solve the real-life tasks.

As a tip for another use-case, if you need .mp3 files for your digital books or electronic books, this feature comes in handy too!

For one of such versatile feature of Python i.e., transforming text data into audio (.mp3) file, below is the code I have curated along with few tips for both Mac and PC users especially for auto-playing it after source code’s execution:

Requirements: Google’s API ‘gTTS’ (which is a Python module thus it can be installed with the below code) and Text editor or IDE, I have used Atom for this coding in Mac and VS Code is also a very good choice.

gTTS stands for Google Text To Speech, a Python library.

$ pip3 install gtts

Step-1: After the above installation, import this ‘gtts’ and ‘os’ modules (it’s built-in so explicit installation is not required) to the project source-code file:

from gtts import gTTS
import os

Step-2: Two possibilities, as you can place your text data in the same file or import it from the external file. In the below code snippet, I have imported from the external file:

# fh = file handler
# 'r' = open the file in read mode
fh = open("data.txt", "r")
brief = fh.read().replace("\n", " ")

OR: An alternate for above if you want your text data to be in the same file of source code, create the variable and store it, here I have named variable as ‘brief’.

Variables offers to store and maintain data, however create them mindfully and contextually!

Note: I have written introductory information about Python programming languague below and enclosed it in triple quotation which allows us to write data in multiple lines, as it helps for better readability!

brief = """Python is a programming & interpreted language used to build full stack applications meaning application developed with both frontend and backend, such as database or API. Python is quite well known as it is a versatile programming language used for building types of applications such as web applications, game developments, API developments, software testing, web scraping, data analysis, machine learning models and Natural Language Processing known as NLP for predictions along with the fields of Computer Vision known for object detection. The well used frameworks with Python are Django and Flask, they provides the basic structure for the further development of applications. The Integrated Development Environment in short I D E can be used to build Pythonic applications, example PyCharm is available in community edition developed by JetBrains and VS Code editor is an open source software developed by Microsoft. Python is a interpreted language meaning it gets compiled and executed during its runtime thus it is also known as dynamically typed language"""

Step-3: Create a variable and assign the code of desired language of your choice to which you want to convert text into. Here I have chosen English. For French, you would use ‘fr’ for example:

language = 'en'

Step-4: Pass in all the above created variables to the gTTS function as below and its output I have stored in the variable called ‘output’:

Parameters: ‘text’ (to pass in the data), ‘lang’ (to pass in the language in which you need the output) and ‘slow’ (To keep the play speed ideal, if you assign it a ‘True’ value, the recording would be faster than usual speed!).

output = gTTS(text=brief, lang=language, slow=False)

Step-5: To save the output to the audio (.mp3) file format and pass in the file name of your choice, however it’s a good tip to keep the name contextual. The audio file ‘about_python.mp3’ gets saved into the same directory (folder) as your project’s root folder:

output.save("about_python.mp3")

Step-6: This step is required based on the ‘Step-2’ - i.e, if you have imported your text data from an external file, below is the code:

# It's always advised to close the file once it returns the result, as it helps with the resources!fh.close()

Step-7: If you want audio file to auto-play once it’s been generated from ‘Step-5’:

# This is for Mac OS:
os.system("about_python.mp3")

If you are working with PC (Windows OS), below is the statement:

# For PC:
os.system("start about_python.mp3")

Here is the over-all code by keeping the text data within the souce-code file and you can save it as ‘python_text.py’, for example:

from gtts import gTTSbrief = """Python is a programming & interpreted language used to build full stack applications meaning application developed with both frontend and backend, such as database or API. Python is quite well known as it is a versatile programming language used for building types of applications such as web applications, game developments, API developments, software testing, web scraping, data analysis, machine learning models and Natural Language Processing known as NLP for predictions along with the fields of Computer Vision known for object detection. The well used frameworks with Python are Django and Flask, they provides the basic structure for the further development of applications. The Integrated Development Environment in short I D E can be used to build Pythonic applications, example PyCharm is available in community edition developed by JetBrains and VS Code editor is an open source software developed by Microsoft. Python is a interpreted language meaning it gets compiled and executed during its runtime thus it is also known as dynamically typed language"""language = 'en'audio_output = gTTS(text=brief, lang=language, slow=False)audio_output.save("python_brief.mp3")

This is the over-all code if you have imported text data from an external file:

from gtts import gTTS
import os
# fh = file handler
fh = open("info.txt", "r")
content = fh.read().replace("\n", " ")
language = 'en'output = gTTS(text=content, lang=language, slow=False)# To save output to mp3 file:
output.save("about_python.mp3")
fh.close()# To play the mp3 file after its been created:
os.system("intro_python.mp3")
# For Windows OS:
# os.system("start about_python.mp3")

To run/execute the .py file: From the terminal/cmd, navigate to current directory of the project’s root folder, write the below code and hit enter then in couple of minutes you would see generated audio file in your project’s root folder.

$ python3 python_text.py

Below are the links to install Text editors mentioned above. If you are a beginner or yet to install them, both the Text editors are compatible with both Mac and PC. When you visit these links, both the providers recognizes your OS platform:

Atom = https://atom.io/ (by Atom)

VS Code = https://code.visualstudio.com/ (by Microsoft)

These Text editors are open-source.

Thank you for reading this article…

--

--

Ramya N

Data Analysis, Web & Full Stack Dev, Tech Writer & ML/DL/NLP Enthusiast | Code Instructor & Mentor | Health & Fitness Influencer