{"id":1056805,"date":"2024-12-31T15:02:37","date_gmt":"2024-12-31T07:02:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056805.html"},"modified":"2024-12-31T15:02:40","modified_gmt":"2024-12-31T07:02:40","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%96%87%e5%ad%97%e8%bd%ac%e4%b8%ba%e8%af%ad%e9%9f%b3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056805.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5c06\u6587\u5b57\u8f6c\u4e3a\u8bed\u97f3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/624e0eac-7cc7-4cb6-9d11-bfed081fc4a8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u5c06\u6587\u5b57\u8f6c\u4e3a\u8bed\u97f3\" \/><\/p>\n<p><p> <strong>\u8981\u5728 Python \u4e2d\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982 gTTS\u3001pyttsx3\u3001\u4ee5\u53ca\u5176\u4ed6 API \u5e73\u53f0\u3002gTTS \u662f\u4e00\u4e2a\u53ef\u4ee5\u8f7b\u677e\u4f7f\u7528 Google \u7684 Text-to-Speech API \u8fdb\u884c\u6587\u5b57\u8f6c\u8bed\u97f3\u7684\u5e93\uff0cpyttsx3 \u5219\u662f\u4e00\u4e2a\u652f\u6301\u79bb\u7ebf\u8f6c\u6362\u7684\u5e93<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u5e76\u4ecb\u7ecd\u4e00\u4e9b\u4f18\u5316\u548c\u9ad8\u7ea7\u4f7f\u7528\u6280\u5de7\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001gTTS\uff08Google Text-to-Speech\uff09<\/h3>\n<\/p>\n<p><p>gTTS (Google Text-to-Speech) \u662f\u4e00\u4e2a\u975e\u5e38\u53d7\u6b22\u8fce\u7684 Python \u5e93\uff0c\u5b83\u5229\u7528\u4e86 Google \u7684 TTS \u670d\u52a1\u6765\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u52bf\u662f\u64cd\u4f5c\u7b80\u5355\uff0c\u5e76\u4e14\u751f\u6210\u7684\u8bed\u97f3\u8d28\u91cf\u8f83\u9ad8\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 gTTS<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5 gTTS \u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u547d\u4ee4\u6765\u5b89\u88c5\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\u7528 gTTS \u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u5e76\u4fdd\u5b58\u4e3a MP3 \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gtts import gTTS<\/p>\n<p>text = &quot;Hello, this is a text to speech conversion using gTTS.&quot;<\/p>\n<p>tts = gTTS(text=text, lang=&#39;en&#39;)<\/p>\n<p>tts.save(&quot;output.mp3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u64ad\u653e\u751f\u6210\u7684\u97f3\u9891\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u7684 MP3 \u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u97f3\u9891\u64ad\u653e\u5668\u64ad\u653e\u3002\u5982\u679c\u4f60\u60f3\u5728\u4ee3\u7801\u4e2d\u64ad\u653e\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u5e93\u8c03\u7528\u7cfb\u7edf\u7684\u9ed8\u8ba4\u64ad\u653e\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>os.system(&quot;start output.mp3&quot;)  # Windows<\/p>\n<h2><strong>os.system(&quot;afplay output.mp3&quot;)  # macOS<\/strong><\/h2>\n<h2><strong>os.system(&quot;mpg321 output.mp3&quot;)  # Linux<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>gTTS \u7684\u4f18\u70b9\u662f\u6613\u4e8e\u4f7f\u7528\u548c\u8bed\u97f3\u8d28\u91cf\u9ad8\uff0c\u4f46\u5b83\u4f9d\u8d56\u4e8e\u4e92\u8054\u7f51\u8fde\u63a5\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528 Google \u7684\u5728\u7ebf\u670d\u52a1\u3002\u5982\u679c\u4f60\u9700\u8981\u79bb\u7ebf\u8f6c\u6362\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528 pyttsx3\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001pyttsx3<\/h3>\n<\/p>\n<p><p>pyttsx3 \u662f\u4e00\u4e2a\u79bb\u7ebf\u7684 Python \u5e93\uff0c\u5b83\u4f7f\u7528\u4e86\u672c\u5730 TTS \u5f15\u64ce\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u4e92\u8054\u7f51\u8fde\u63a5\u3002\u5b83\u652f\u6301\u591a\u79cd\u5e73\u53f0\uff0c\u5982 Windows\u3001macOS \u548c Linux\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 pyttsx3<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528 pip \u5b89\u88c5 pyttsx3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyttsx3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 pyttsx3 \u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u5e76\u76f4\u63a5\u64ad\u653e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<p>engine = pyttsx3.init()<\/p>\n<p>engine.say(&quot;Hello, this is a text to speech conversion using pyttsx3.&quot;)<\/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><h4>3\u3001\u8c03\u6574\u8bed\u97f3\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>pyttsx3 \u5141\u8bb8\u4f60\u8c03\u6574\u8bed\u97f3\u7684\u5c5e\u6027\uff0c\u5982\u901f\u7387\u3001\u97f3\u91cf\u548c\u8bed\u97f3\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8c03\u6574\u8bed\u901f<\/strong><\/h2>\n<p>rate = engine.getProperty(&#39;rate&#39;)<\/p>\n<p>engine.setProperty(&#39;rate&#39;, rate - 50)<\/p>\n<h2><strong>\u8c03\u6574\u97f3\u91cf<\/strong><\/h2>\n<p>volume = engine.getProperty(&#39;volume&#39;)<\/p>\n<p>engine.setProperty(&#39;volume&#39;, volume + 0.25)<\/p>\n<h2><strong>\u66f4\u6539\u8bed\u97f3<\/strong><\/h2>\n<p>voices = engine.getProperty(&#39;voices&#39;)<\/p>\n<p>engine.setProperty(&#39;voice&#39;, voices[1].id)  # 0\u4e3a\u9ed8\u8ba4\u7537\u6027\u8bed\u97f3\uff0c1\u4e3a\u5973\u6027\u8bed\u97f3<\/p>\n<p>engine.say(&quot;Hello, this is a text to speech conversion using pyttsx3 with customized settings.&quot;)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>pyttsx3 \u7684\u4f18\u70b9\u662f\u652f\u6301\u79bb\u7ebf\u8f6c\u6362\uff0c\u5e76\u4e14\u53ef\u4ee5\u8c03\u6574\u591a\u79cd\u5c5e\u6027\uff0c\u4f7f\u5176\u5177\u6709\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u7136\u800c\uff0c\u5b83\u7684\u8bed\u97f3\u8d28\u91cf\u53ef\u80fd\u4e0d\u5982 gTTS \u9ad8\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6 API \u5e73\u53f0<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 gTTS \u548c pyttsx3\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6 API \u5e73\u53f0\uff0c\u5982 IBM Watson TTS\u3001Microsoft Azure TTS \u548c Amazon Polly\u3002\u8fd9\u4e9b\u670d\u52a1\u901a\u5e38\u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u8bed\u97f3\u8d28\u91cf\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u6ce8\u518c\u548c\u83b7\u53d6 API \u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001IBM Watson TTS<\/h4>\n<\/p>\n<p><p>IBM Watson TTS \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684 TTS \u670d\u52a1\uff0c\u5b83\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u8bed\u97f3\u3002\u4f60\u9700\u8981\u6ce8\u518c IBM Cloud \u8d26\u6237\uff0c\u5e76\u83b7\u53d6 API \u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5 <code>ibm_watson<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install ibm-watson<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from ibm_watson import TextToSpeechV1<\/p>\n<p>from ibm_cloud_sdk_core.authenticators import IAMAuthenticator<\/p>\n<p>apikey = &#39;your_api_key&#39;<\/p>\n<p>url = &#39;your_service_url&#39;<\/p>\n<p>authenticator = IAMAuthenticator(apikey)<\/p>\n<p>tts = TextToSpeechV1(authenticator=authenticator)<\/p>\n<p>tts.set_service_url(url)<\/p>\n<p>with open(&#39;output.mp3&#39;, &#39;wb&#39;) as audio_file:<\/p>\n<p>    response = tts.synthesize(&#39;Hello, this is IBM Watson TTS.&#39;, accept=&#39;audio\/mp3&#39;, voice=&#39;en-US_AllisonV3Voice&#39;).get_result()<\/p>\n<p>    audio_file.write(response.content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001Microsoft Azure TTS<\/h4>\n<\/p>\n<p><p>Microsoft Azure TTS \u63d0\u4f9b\u9ad8\u8d28\u91cf\u7684\u8bed\u97f3\u8f6c\u6362\u670d\u52a1\u3002\u4f60\u9700\u8981\u6ce8\u518c Azure \u8d26\u6237\uff0c\u5e76\u83b7\u53d6 API \u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5 <code>azure-cognitiveservices-speech<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install azure-cognitiveservices-speech<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import azure.cognitiveservices.speech as speechsdk<\/p>\n<p>speech_key = &quot;your_speech_key&quot;<\/p>\n<p>service_region = &quot;your_service_region&quot;<\/p>\n<p>speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)<\/p>\n<p>audio_config = speechsdk.audio.AudioOutputConfig(filename=&quot;output.wav&quot;)<\/p>\n<p>speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)<\/p>\n<p>text = &quot;Hello, this is Microsoft Azure TTS.&quot;<\/p>\n<p>speech_synthesizer.speak_text_async(text).get()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001Amazon Polly<\/h4>\n<\/p>\n<p><p>Amazon Polly \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 TTS \u670d\u52a1\uff0c\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u8bed\u97f3\u3002\u4f60\u9700\u8981\u6ce8\u518c AWS \u8d26\u6237\uff0c\u5e76\u83b7\u53d6 API \u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><p>\u5b89\u88c5 <code>boto3<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install boto3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import boto3<\/p>\n<p>polly_client = boto3.Session(<\/p>\n<p>    aws_access_key_id=&#39;your_access_key&#39;,<\/p>\n<p>    aws_secret_access_key=&#39;your_secret_key&#39;,<\/p>\n<p>    region_name=&#39;your_region&#39;<\/p>\n<p>).client(&#39;polly&#39;)<\/p>\n<p>response = polly_client.synthesize_speech(<\/p>\n<p>    Text=&#39;Hello, this is Amazon Polly.&#39;,<\/p>\n<p>    OutputFormat=&#39;mp3&#39;,<\/p>\n<p>    VoiceId=&#39;Joanna&#39;<\/p>\n<p>)<\/p>\n<p>with open(&#39;output.mp3&#39;, &#39;wb&#39;) as audio_file:<\/p>\n<p>    audio_file.write(response[&#39;AudioStream&#39;].read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd9\u4e9b API \u5e73\u53f0\u901a\u5e38\u63d0\u4f9b\u66f4\u9ad8\u7684\u8bed\u97f3\u8d28\u91cf\u548c\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u4ed8\u8d39\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u548c\u9ad8\u7ea7\u4f7f\u7528\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u4f18\u5316 TTS \u7684\u6027\u80fd\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u6280\u672f\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7f13\u5b58\u751f\u6210\u7684\u97f3\u9891<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u591a\u6b21\u8f6c\u6362\u76f8\u540c\u7684\u6587\u5b57\uff0c\u53ef\u4ee5\u5c06\u751f\u6210\u7684\u97f3\u9891\u6587\u4ef6\u7f13\u5b58\u8d77\u6765\uff0c\u4ee5\u51cf\u5c11\u91cd\u590d\u8c03\u7528 TTS \u670d\u52a1\u7684\u6b21\u6570\u3002\u8fd9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u5728\u7ebf TTS \u670d\u52a1\u65f6\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408 NLP \u6280\u672f<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7ed3\u5408\u81ea\u7136\u8bed\u8a00\u5904\u7406 (NLP) \u6280\u672f\u6765\u9884\u5904\u7406\u6587\u672c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u7b49\u6280\u672f\u6765\u4f18\u5316\u8f93\u5165\u7684\u6587\u672c\uff0c\u4ece\u800c\u63d0\u9ad8\u751f\u6210\u8bed\u97f3\u7684\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u97f3\u9891\u683c\u5f0f\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u5c06\u751f\u6210\u7684\u97f3\u9891\u6587\u4ef6\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528 <code>pydub<\/code> \u5e93\u6765\u5b9e\u73b0\u97f3\u9891\u683c\u5f0f\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pydub import AudioSegment<\/p>\n<p>audio = AudioSegment.from_mp3(&quot;output.mp3&quot;)<\/p>\n<p>audio.export(&quot;output.wav&quot;, format=&quot;wav&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5b9e\u65f6\u8bed\u97f3\u751f\u6210<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u5982\u804a\u5929\u673a\u5668\u4eba\u548c\u865a\u62df\u52a9\u624b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9e\u65f6\u751f\u6210\u8bed\u97f3\u3002\u53ef\u4ee5\u5c06 TTS \u4e0e WebSocket \u7b49\u6280\u672f\u7ed3\u5408\uff0c\u5b9e\u73b0\u5b9e\u65f6\u8bed\u97f3\u751f\u6210\u548c\u64ad\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728 Python \u4e2d\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 gTTS\u3001pyttsx3 \u548c\u5176\u4ed6 API \u5e73\u53f0\u3002\u6211\u4eec\u8fd8\u63a2\u8ba8\u4e86\u5982\u4f55\u4f18\u5316 TTS \u7684\u6027\u80fd\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u6280\u672f\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u5c06\u6587\u5b57\u8f6c\u4e3a\u8bed\u97f3\uff1f<\/strong><br \/>Python\u4e2d\u6709\u51e0\u4e2a\u6d41\u884c\u7684\u5e93\u53ef\u4ee5\u5b9e\u73b0\u6587\u5b57\u8f6c\u8bed\u97f3\u529f\u80fd\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>gTTS<\/code>\uff08Google Text-to-Speech\uff09\u548c<code>pyttsx3<\/code>\u3002<code>gTTS<\/code>\u5229\u7528Google\u7684\u5728\u7ebf\u670d\u52a1\u6765\u8f6c\u6362\u6587\u5b57\uff0c\u800c<code>pyttsx3<\/code>\u5219\u662f\u4e00\u4e2a\u79bb\u7ebf\u5e93\uff0c\u652f\u6301\u591a\u79cd\u8bed\u97f3\u5f15\u64ce\u3002\u9009\u62e9\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff0c\u6bd4\u5982\u662f\u5426\u9700\u8981\u79bb\u7ebf\u529f\u80fd\u6216\u7279\u5b9a\u7684\u8bed\u97f3\u9009\u9879\u3002<\/p>\n<p><strong>\u5982\u4f55\u5b89\u88c5Python\u7684\u6587\u5b57\u8f6c\u8bed\u97f3\u5e93\uff1f<\/strong><br \/>\u8981\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177<code>pip<\/code>\u6765\u5b89\u88c5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install gTTS<\/code>\u5b89\u88c5gTTS\u5e93\uff0c\u6216\u8005\u4f7f\u7528<code>pip install pyttsx3<\/code>\u5b89\u88c5pyttsx3\u5e93\u3002\u786e\u4fdd\u5728\u5b89\u88c5\u4e4b\u524d\uff0c\u60a8\u7684Python\u73af\u5883\u5df2\u6b63\u786e\u914d\u7f6e\uff0c\u5e76\u4e14\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff08\u5bf9\u4e8egTTS\uff09\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528gTTS\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\u5e76\u4fdd\u5b58\u4e3a\u97f3\u9891\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528gTTS\u5e93\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a<code>gTTS<\/code>\u5bf9\u8c61\uff0c\u4f20\u5165\u8981\u8f6c\u6362\u7684\u6587\u5b57\u548c\u8bed\u8a00\u53c2\u6570\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528<code>save<\/code>\u65b9\u6cd5\u5c06\u751f\u6210\u7684\u8bed\u97f3\u4fdd\u5b58\u4e3a\u97f3\u9891\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from gtts import gTTS\n\ntext = &quot;\u4f60\u597d\uff0c\u6b22\u8fce\u4f7f\u7528Python\u8fdb\u884c\u6587\u5b57\u8f6c\u8bed\u97f3!&quot;\ntts = gTTS(text=text, lang=&#39;zh&#39;)\ntts.save(&quot;output.mp3&quot;)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u540e\uff0c\u60a8\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u627e\u5230\u540d\u4e3a<code>output.mp3<\/code>\u7684\u97f3\u9891\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728 Python \u4e2d\u5c06\u6587\u5b57\u8f6c\u6362\u4e3a\u8bed\u97f3\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982 gTTS\u3001pyttsx3\u3001\u4ee5\u53ca\u5176\u4ed6 API \u5e73\u53f0\u3002gTT [&hellip;]","protected":false},"author":3,"featured_media":1056817,"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\/1056805"}],"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=1056805"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056805\/revisions"}],"predecessor-version":[{"id":1056818,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056805\/revisions\/1056818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056817"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}