

{"id":104483,"date":"2021-11-30T09:00:57","date_gmt":"2021-11-30T03:30:57","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=104483"},"modified":"2026-06-01T13:59:59","modified_gmt":"2026-06-01T08:29:59","slug":"python-speech-to-text-and-text-to-speech","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/","title":{"rendered":"Convert Speech to Text and Text to Speech in Python"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:2581,&quot;href&quot;:&quot;https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1VW1IknOhKvJPnRzuKRBFfp1zqDMyL80z\\\/view?usp=drive_link&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20260601082535\\\/https:\\\/\\\/drive.google.com\\\/file\\\/d\\\/1VW1IknOhKvJPnRzuKRBFfp1zqDMyL80z\\\/view?usp=drive_link&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2026-06-02 07:14:49&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-06 06:15:18&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-09 08:56:01&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-09 08:56:01&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>Speech recognition is useful in various appliances around us like Google Assistant, Alexa, Cortona etc. Speech recognition has made things easy like we can use our voice to talk to google assistants and find the answers to our questions.<\/p>\n<h3>About Converting Speech to text and text to Speech<\/h3>\n<p>This project will recognise the speech of the user and print it on the tkinter window and convert the text typed by the user to speech. Let\u2019s start developing this fun and easy project.<\/p>\n<h3>Python Conversion of Speech to text and text to Speech project<\/h3>\n<p>The main objective of this project is to develop our own converter of speech to text and text to speech. Installation of speech recognition, tkinter and gtts is mandatory.<\/p>\n<h3>Project Prerequisites<\/h3>\n<p>Basic knowledge of python, speechrecognition, tkinter and gtts is sufficient to complete this project.<\/p>\n<h3>Download project on Conversion of Speech to text and text to Speech<\/h3>\n<p>Source code of project on conversion of speech to text and text to speech: <a href=\"https:\/\/drive.google.com\/file\/d\/1VW1IknOhKvJPnRzuKRBFfp1zqDMyL80z\/view?usp=drive_link\"><strong>Speech to Text and Text to Speech Python Project<\/strong><\/a><\/p>\n<h3>Project File Structure<\/h3>\n<ol>\n<li>Importing various modules<\/li>\n<li>Initializing main window<\/li>\n<li>Backend of speak and recordvoice function<\/li>\n<li>Rest Code<\/li>\n<\/ol>\n<h4>1. Importing various modules:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from tkinter import *\r\nfrom tkinter.messagebox import showinfo\r\nfrom gtts import gTTS\r\nimport speech_recognition as sr\r\nimport os\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>a. tkinter: Graphical user Interface can be easily be created with the help of tkinter.<br \/>\nb. Messagebox: Message boxes are displayed on the tkinter window.<br \/>\nc. gtts: This module helps in playing the sound after converting text to speech.<br \/>\nd. speech_recognition: The main purpose of speechrecognition is to identify the words spoken.<br \/>\ne. os: This module is useful to interact with the operating system<\/p>\n<h4>2. Initializing main window:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">mainwindow= Tk()\r\nmainwindow.title('DataFlair Text-To-Speech and Speech-To-Text Converter')\r\nmainwindow.geometry('500x500')\r\nmainwindow.resizable(0, 0)\r\nmainwindow.configure(bg='yellow')<\/pre>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>a. Tk(): Every component of tkinter applications can easily be accessed with The help of tk.<br \/>\nb. title(): It sets the title of the window.<br \/>\nc. geometry(): It helps to set the geometry of the window.<br \/>\nd. resizable(): It helps to change the size of the tkinter window as per the requirement of a user.<\/p>\n<h4>3. Backend of speak and recordvoice function:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def say(text1):\r\n     language = 'en'\r\n     speech = gTTS(text = text1, lang = language, slow = False)\r\n     speech.save(\"text.mp3\")\r\n     os.system(\"start text.mp3\")\r\n \r\ndef recordvoice():\r\n    while True:\r\n        r = sr.Recognizer()\r\n        with sr.Microphone() as source:\r\n            audio=r.listen(source)\r\n            try:    \r\n                text1 = r.recognize_google(audio,language=\"en-IN\")\r\n            except:\r\n                pass\r\n            return text1\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>a. Recognizer(): The main purpose of this instance is to recognize the voice.<br \/>\nb. Microphone(): It uses the default microphone as the audio source.<br \/>\nc. listen(): It listens to the phrase that is spoken and extracts into audio data.<\/p>\n<h4>4. Rest Code:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def TextToSpeech():\r\n    texttospeechwindow = Toplevel(mainwindow)\r\n    texttospeechwindow.title('Text-to-Speech Converter by DataFlair')\r\n    texttospeechwindow.geometry(\"500x500\")\r\n    texttospeechwindow.configure(bg='Blue')\r\n \r\n    Label(texttospeechwindow, text='Text-to-Speech Converter by DataFlair', font=(\"Times New Roman\", 15), bg='Blue').place(x=50)\r\n \r\n    text = Text(texttospeechwindow, height=5, width=30, font=12)\r\n    text.place(x=7, y=60)\r\n   \r\n    speakbutton = Button(texttospeechwindow, text='listen', bg='coral', command=lambda: say(str(text.get(1.0, END))))\r\n    speakbutton.place(x=140, y=200)\r\n \r\ndef SpeechToText():\r\n    speechtotextwindow = Toplevel(mainwindow)\r\n    speechtotextwindow.title('Speech-to-Text Converter by DataFlair')\r\n    speechtotextwindow.geometry(\"500x500\")\r\n    speechtotextwindow.configure(bg='pink')\r\n \r\n    Label(speechtotextwindow, text='Speech-to-Text Converter by DataFlair', font=(\"Comic Sans MS\", 15), bg='IndianRed').place(x=50)\r\n \r\n    text = Text(speechtotextwindow, font=12, height=3, width=30)\r\n    text.place(x=7, y=100)\r\n   \r\n    recordbutton = Button(speechtotextwindow, text='Record', bg='Sienna', command=lambda: text.insert(END, recordvoice()))\r\n    recordbutton.place(x=140, y=50)\r\n \r\nLabel(mainwindow, text='DataFlair Text-To-Speech and Speech-To-Text Converter',\r\n     font=('Times New Roman', 16), bg='red', wrap=True, wraplength=450).place(x=25, y=0)\r\n \r\ntexttospeechbutton = Button(mainwindow, text='Text-To-Speech Conversion', font=('Times New Roman', 16), bg='Purple', command=TextToSpeech)\r\ntexttospeechbutton.place(x=100, y=150)\r\n \r\nspeechtotextbutton = Button(mainwindow, text='Speech-To-Text Conversion', font=('Times New Roman', 16), bg='Purple', command=SpeechToText)\r\nspeechtotextbutton.place(x=100, y=250)\r\n \r\nmainwindow.update()\r\nmainwindow.mainloop()\r\n<\/pre>\n<p><strong>Code Explanation:<\/strong><\/p>\n<p>a. TextToSpeech(): This is the main function for converting text to speech.<br \/>\nb. Label(): Display boxes are displayed on the screen where you can place the text.<br \/>\nc. Button(): It adds a button on the tkinter window.<br \/>\nd. SpeechToText(): This is the main function for converting speech to text.<br \/>\ne. mainwindow.mainloop(): It helps in running our program.<\/p>\n<h3>Python Speech to Text Output<\/h3>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-speech-to-text-output.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-104485\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-speech-to-text-output.webp\" alt=\"python speech to text output\" width=\"1920\" height=\"1024\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>We have successfully developed a project on conversion of Speech to text and text to Speech with the help of three modules speechrecognition, gtts and tkinter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Speech recognition is useful in various appliances around us like Google Assistant, Alexa, Cortona etc. Speech recognition has made things easy like we can use our voice to talk to google assistants and find&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":104486,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[21063,6006,21082,25306,25979,25980],"class_list":["post-104483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-beginner-python-project","tag-how-to-convert-speech-to-text-in-python","tag-python-project","tag-python-project-for-text-to-speech-conversion","tag-python-speech-to-text","tag-speech-to-text"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Convert Speech to Text and Text to Speech in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"Develop Python project on conversion of Speech to text and text to speech with the help of three modules speechrecognition, gtts and tkinter.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert Speech to Text and Text to Speech in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Develop Python project on conversion of Speech to text and text to speech with the help of three modules speechrecognition, gtts and tkinter.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-30T03:30:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-01T08:29:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Convert Speech to Text and Text to Speech in Python - DataFlair","description":"Develop Python project on conversion of Speech to text and text to speech with the help of three modules speechrecognition, gtts and tkinter.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/","og_locale":"en_US","og_type":"article","og_title":"Convert Speech to Text and Text to Speech in Python - DataFlair","og_description":"Develop Python project on conversion of Speech to text and text to speech with the help of three modules speechrecognition, gtts and tkinter.","og_url":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-11-30T03:30:57+00:00","article_modified_time":"2026-06-01T08:29:59+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Convert Speech to Text and Text to Speech in Python","datePublished":"2021-11-30T03:30:57+00:00","dateModified":"2026-06-01T08:29:59+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/"},"wordCount":472,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp","keywords":["Beginner Python Project","how to convert speech to text in Python","Python project","Python project for text to speech conversion","python speech to text","speech to text"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/","url":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/","name":"Convert Speech to Text and Text to Speech in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp","datePublished":"2021-11-30T03:30:57+00:00","dateModified":"2026-06-01T08:29:59+00:00","description":"Develop Python project on conversion of Speech to text and text to speech with the help of three modules speechrecognition, gtts and tkinter.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/11\/python-convert-speech-to-text-and-text-to-speech.webp","width":1200,"height":628},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-speech-to-text-and-text-to-speech\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Convert Speech to Text and Text to Speech in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=104483"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104483\/revisions"}],"predecessor-version":[{"id":148664,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/104483\/revisions\/148664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/104486"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=104483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=104483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=104483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}