{"id":1064584,"date":"2024-12-31T16:10:15","date_gmt":"2024-12-31T08:10:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064584.html"},"modified":"2024-12-31T16:10:18","modified_gmt":"2024-12-31T08:10:18","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e8%af%ad%e9%9f%b3%e8%ae%be%e7%bd%ae%e4%b8%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064584.html","title":{"rendered":"\u5982\u4f55\u5c06python\u8bed\u97f3\u8bbe\u7f6e\u4e3a\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a9baed3a-23b7-490e-972c-d20e613fdee0.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5c06python\u8bed\u97f3\u8bbe\u7f6e\u4e3a\u4e2d\u6587\" \/><\/p>\n<p><p> <strong>\u5c06Python\u8bed\u97f3\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u9002\u5f53\u7684TTS\u5e93\u3001\u914d\u7f6e\u4e2d\u6587\u8bed\u97f3\u5305\u3001\u8c03\u6574\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u9002\u5f53\u7684TTS\u5e93\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3aTTS\uff08\u6587\u672c\u8f6c\u8bed\u97f3\uff09\u5e93\u76f4\u63a5\u5f71\u54cd\u5230\u8bed\u97f3\u7684\u751f\u6210\u8d28\u91cf\u3002\u5177\u4f53\u7684\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9002\u5f53\u7684TTS\u5e93<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u6709\u591a\u4e2aTTS\u5e93\u53ef\u4ee5\u7528\u6765\u751f\u6210\u8bed\u97f3\uff0c\u5305\u62ecgTTS\u3001pyttsx3\u7b49\u3002\u8fd9\u4e9b\u5e93\u90fd\u652f\u6301\u4e2d\u6587\u8bed\u97f3\u751f\u6210\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528gTTS\u548cpyttsx3\u5e93\u5b9e\u73b0\u4e2d\u6587\u8bed\u97f3\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5gTTS\u5e93<\/h4>\n<\/p>\n<p><p>gTTS\uff08Google Text-to-Speech\uff09\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684TTS\u5e93\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\uff0c\u5305\u62ec\u4e2d\u6587\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5gTTS\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install gtts<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528gTTS\u751f\u6210\u4e2d\u6587\u8bed\u97f3<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u4e2d\u6587\u8bed\u97f3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gtts import gTTS<\/p>\n<p>import os<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u8f6c\u6362\u6210\u8bed\u97f3\u7684\u4e2d\u6587\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;\u4f60\u597d\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u8bed\u97f3\u793a\u4f8b\u3002&quot;<\/p>\n<h2><strong>\u4f7f\u7528gTTS\u751f\u6210\u8bed\u97f3\uff0c\u9009\u62e9\u4e2d\u6587<\/strong><\/h2>\n<p>tts = gTTS(text=text, lang=&#39;zh-cn&#39;)<\/p>\n<h2><strong>\u5c06\u8bed\u97f3\u4fdd\u5b58\u4e3amp3\u6587\u4ef6<\/strong><\/h2>\n<p>tts.save(&quot;chinese_voice.mp3&quot;)<\/p>\n<h2><strong>\u64ad\u653e\u751f\u6210\u7684\u8bed\u97f3\u6587\u4ef6<\/strong><\/h2>\n<p>os.system(&quot;mpg321 chinese_voice.mp3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b89\u88c5pyttsx3\u5e93<\/h4>\n<\/p>\n<p><p>pyttsx3\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684TTS\u5e93\uff0c\u5b83\u652f\u6301\u79bb\u7ebf\u8bed\u97f3\u751f\u6210\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5pyttsx3\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyttsx3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528pyttsx3\u751f\u6210\u4e2d\u6587\u8bed\u97f3<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u751f\u6210\u4e2d\u6587\u8bed\u97f3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<h2><strong>\u521d\u59cb\u5316pyttsx3\u5f15\u64ce<\/strong><\/h2>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u97f3\u5c5e\u6027<\/strong><\/h2>\n<p>engine.setProperty(&#39;rate&#39;, 150)  # \u8bed\u901f<\/p>\n<p>engine.setProperty(&#39;volume&#39;, 0.9)  # \u97f3\u91cf<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u8bed\u97f3<\/strong><\/h2>\n<p>voices = engine.getProperty(&#39;voices&#39;)<\/p>\n<p>for voice in voices:<\/p>\n<p>    if &quot;zh&quot; in voice.id:<\/p>\n<p>        engine.setProperty(&#39;voice&#39;, voice.id)<\/p>\n<p>        break<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u8f6c\u6362\u6210\u8bed\u97f3\u7684\u4e2d\u6587\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;\u4f60\u597d\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u8bed\u97f3\u793a\u4f8b\u3002&quot;<\/p>\n<h2><strong>\u751f\u6210\u8bed\u97f3<\/strong><\/h2>\n<p>engine.say(text)<\/p>\n<p>engine.runAndW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u4e2d\u6587\u8bed\u97f3\u5305<\/h3>\n<\/p>\n<p><p>\u6709\u4e9bTTS\u5e93\u9700\u8981\u914d\u7f6e\u4e2d\u6587\u8bed\u97f3\u5305\u624d\u80fd\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u4e2d\u6587\u8bed\u97f3\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4e0b\u8f7d\u548c\u5b89\u88c5\u4e2d\u6587\u8bed\u97f3\u5305<\/h4>\n<\/p>\n<p><p>\u4ee5espeak\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u4e2d\u6587\u8bed\u97f3\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install espeak<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u914d\u7f6eespeak\u4f7f\u7528\u4e2d\u6587\u8bed\u97f3\u5305<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u914d\u7f6eespeak\u4f7f\u7528\u4e2d\u6587\u8bed\u97f3\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<h2><strong>\u5b9a\u4e49\u8981\u8f6c\u6362\u6210\u8bed\u97f3\u7684\u4e2d\u6587\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;\u4f60\u597d\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e2d\u6587\u8bed\u97f3\u793a\u4f8b\u3002&quot;<\/p>\n<h2><strong>\u4f7f\u7528espeak\u751f\u6210\u8bed\u97f3\uff0c\u9009\u62e9\u4e2d\u6587<\/strong><\/h2>\n<p>command = f&#39;espeak -v zh &quot;{text}&quot;&#39;<\/p>\n<p>subprocess.run(command, shell=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u6574\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u6709\u4e9bTTS\u5e93\u4f9d\u8d56\u4e8e\u7cfb\u7edf\u7684\u8bed\u8a00\u8bbe\u7f6e\uff0c\u5982\u679c\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e\u4e3a\u4e2d\u6587\uff0c\u751f\u6210\u7684\u8bed\u97f3\u4f1a\u66f4\u52a0\u81ea\u7136\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbe\u7f6e\u7cfb\u7edf\u8bed\u8a00\u4e3a\u4e2d\u6587<\/h4>\n<\/p>\n<p><p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u7cfb\u7edf\u8bed\u8a00\u4e3a\u4e2d\u6587\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo update-locale LANG=zh_CN.UTF-8<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u63a7\u5236\u9762\u677f\u8bbe\u7f6e\u7cfb\u7edf\u8bed\u8a00\u4e3a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u91cd\u65b0\u542f\u52a8Python\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u66f4\u6539\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e\u540e\uff0c\u9700\u8981\u91cd\u65b0\u542f\u52a8Python\u73af\u5883\u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u9002\u5f53\u7684TTS\u5e93\u3001\u914d\u7f6e\u4e2d\u6587\u8bed\u97f3\u5305\u3001\u8c03\u6574\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u5c06Python\u8bed\u97f3\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u3002\u5176\u4e2d\uff0c\u4f7f\u7528gTTS\u548cpyttsx3\u5e93\u662f\u6700\u7b80\u5355\u6709\u6548\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u6210\u529f\u5b9e\u73b0Python\u4e2d\u6587\u8bed\u97f3\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e2d\u6587\u8bed\u97f3\u5408\u6210\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0\u4e2d\u6587\u8bed\u97f3\u5408\u6210\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\uff0c\u5982gTTS\uff08Google Text-to-Speech\uff09\u6216pyttsx3\u3002gTTS\u4f9d\u8d56\u4e92\u8054\u7f51\u8fde\u63a5\uff0c\u800cpyttsx3\u662f\u4e00\u4e2a\u79bb\u7ebf\u5e93\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u3002\u60a8\u53ea\u9700\u5b89\u88c5\u76f8\u5e94\u7684\u5e93\uff0c\u7136\u540e\u7f16\u5199\u7b80\u5355\u7684\u4ee3\u7801\u6765\u5408\u6210\u4e2d\u6587\u6587\u672c\u4e3a\u8bed\u97f3\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06Python\u7684\u8bed\u97f3\u8bc6\u522b\u529f\u80fd\u8bbe\u7f6e\u4e3a\u4e2d\u6587\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684\u8bed\u97f3\u8bc6\u522b\u529f\u80fd\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u3002\u53ef\u4ee5\u4f7f\u7528SpeechRecognition\u5e93\uff0c\u901a\u8fc7\u8bbe\u7f6e\u8bed\u8a00\u53c2\u6570\u4e3a\u201czh-CN\u201d\u6765\u8bc6\u522b\u4e2d\u6587\u3002\u786e\u4fdd\u60a8\u7684\u9ea6\u514b\u98ce\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u4e14\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5\u76f8\u5e94\u7684\u4e2d\u6587\u8bed\u97f3\u8bc6\u522b\u5f15\u64ce\u3002<\/p>\n<p><strong>\u4f7f\u7528\u4e2d\u6587\u8bed\u97f3\u5408\u6210\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u4e2d\u6587\u8bed\u97f3\u5408\u6210\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u8bed\u97f3\u5e93\u548c\u5f15\u64ce\u975e\u5e38\u91cd\u8981\u3002\u786e\u4fdd\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u81ea\u7136\u8bed\u97f3\u5f15\u64ce\uff0c\u4ee5\u63d0\u9ad8\u8bed\u97f3\u7684\u6d41\u7545\u6027\u548c\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u8c03\u6574\u8bed\u901f\u548c\u97f3\u8c03\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u5408\u6210\u7684\u8bed\u97f3\u66f4\u7b26\u5408\u60a8\u7684\u9700\u6c42\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u8bed\u97f3\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u9002\u5f53\u7684TTS\u5e93\u3001\u914d\u7f6e\u4e2d\u6587\u8bed\u97f3\u5305\u3001\u8c03\u6574\u7cfb\u7edf\u8bed\u8a00\u8bbe\u7f6e\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u9002\u5f53\u7684T [&hellip;]","protected":false},"author":3,"featured_media":1064592,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064584"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1064584"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064584\/revisions"}],"predecessor-version":[{"id":1064594,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064584\/revisions\/1064594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064592"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}