{"id":1089146,"date":"2025-01-08T13:50:54","date_gmt":"2025-01-08T05:50:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089146.html"},"modified":"2025-01-08T13:50:57","modified_gmt":"2025-01-08T05:50:57","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%ae%9e%e7%8e%b0%e8%af%ad%e9%9f%b3%e6%92%ad%e6%8a%a5%e6%97%b6%e9%97%b4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089146.html","title":{"rendered":"\u5982\u4f55\u7528python\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202204\/13403003-4623-40f7-bc16-c32cf77b0bea.webp\" alt=\"\u5982\u4f55\u7528python\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u6765\u5b9e\u73b0\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>pyttsx3<\/code>\u3001<code>datetime<\/code>\u5e93\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528<code>pyttsx3<\/code>\u5e93\u3001\u4f7f\u7528<code>datetime<\/code>\u5e93\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u7ed3\u5408\u4e24\u8005\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u3002<\/strong>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u91ca\u6bcf\u4e00\u6b65\u9aa4\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u5fc5\u8981\u7684\u5e93\u3002\u6700\u91cd\u8981\u7684\u5e93\u662f<code>pyttsx3<\/code>\uff0c\u8fd9\u662f\u4e00\u4e2a\u6587\u672c\u8f6c\u8bed\u97f3\u8f6c\u6362\u5e93\uff0c\u53ef\u4ee5\u5728\u79bb\u7ebf\u73af\u5883\u4e2d\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981<code>datetime<\/code>\u5e93\uff0c\u8fd9\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u7528\u4e8e\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyttsx3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>datetime<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/p>\n<\/p>\n<p><p><code>datetime<\/code>\u5e93\u662fPython\u5185\u7f6e\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u5f53\u524d\u7684\u5c0f\u65f6\u548c\u5206\u949f\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>datetime<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<p>print(&quot;Current Time =&quot;, current_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4ee5\u5c0f\u65f6\u548c\u5206\u949f\u7684\u683c\u5f0f\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>pyttsx3<\/code>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5<\/p>\n<\/p>\n<p><p><code>pyttsx3<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u6587\u672c\u8f6c\u8bed\u97f3\u5e93\uff0c\u652f\u6301\u591a\u79cdTTS\u5f15\u64ce\uff0c\u5305\u62ecSAPI5\u3001nsss\u548cespeak\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>pyttsx3<\/code>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<h2><strong>\u521d\u59cb\u5316TTS\u5f15\u64ce<\/strong><\/h2>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u901f<\/strong><\/h2>\n<p>engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf<\/strong><\/h2>\n<p>engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<h2><strong>\u8981\u64ad\u62a5\u7684\u6587\u672c<\/strong><\/h2>\n<p>text = &quot;Hello, this is a test.&quot;<\/p>\n<h2><strong>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5<\/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><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521d\u59cb\u5316TTS\u5f15\u64ce\uff0c\u8bbe\u7f6e\u8bed\u901f\u548c\u97f3\u91cf\uff0c\u7136\u540e\u64ad\u62a5\u6307\u5b9a\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408<code>datetime<\/code>\u548c<code>pyttsx3<\/code>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4<\/p>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u5c06\u7ed3\u5408\u4ee5\u4e0a\u4e24\u4e2a\u6b65\u9aa4\uff0c\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4f7f\u7528<code>pyttsx3<\/code>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<h2><strong>\u521d\u59cb\u5316TTS\u5f15\u64ce<\/strong><\/h2>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u901f<\/strong><\/h2>\n<p>engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf<\/strong><\/h2>\n<p>engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<h2><strong>\u8981\u64ad\u62a5\u7684\u6587\u672c<\/strong><\/h2>\n<p>text = f&quot;The current time is {current_time}&quot;<\/p>\n<h2><strong>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5<\/strong><\/h2>\n<p>engine.say(text)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u8bed\u97f3\u8fdb\u884c\u64ad\u62a5\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u63d0\u9ad8\u8bed\u97f3\u64ad\u62a5\u7684\u7528\u6237\u4f53\u9a8c<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u8bed\u97f3\u64ad\u62a5\u7684\u7528\u6237\u4f53\u9a8c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u5f53\u524d\u65f6\u95f4\u7684\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u4e0d\u540c\u7684\u64ad\u62a5\uff0c\u6216\u8005\u6dfb\u52a0\u989d\u5916\u7684\u63d0\u793a\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6839\u636e\u65f6\u95f4\u6bb5\u8fdb\u884c\u4e0d\u540c\u7684\u64ad\u62a5\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>hour = now.strftime(&quot;%H&quot;)<\/p>\n<p>minute = now.strftime(&quot;%M&quot;)<\/p>\n<h2><strong>\u521d\u59cb\u5316TTS\u5f15\u64ce<\/strong><\/h2>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u901f<\/strong><\/h2>\n<p>engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf<\/strong><\/h2>\n<p>engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<h2><strong>\u6839\u636e\u65f6\u95f4\u6bb5\u64ad\u62a5\u4e0d\u540c\u7684\u5185\u5bb9<\/strong><\/h2>\n<p>if int(hour) &lt; 12:<\/p>\n<p>    period = &quot;morning&quot;<\/p>\n<p>elif int(hour) &lt; 18:<\/p>\n<p>    period = &quot;afternoon&quot;<\/p>\n<p>else:<\/p>\n<p>    period = &quot;evening&quot;<\/p>\n<h2><strong>\u8981\u64ad\u62a5\u7684\u6587\u672c<\/strong><\/h2>\n<p>text = f&quot;Good {period}, the current time is {hour}:{minute}&quot;<\/p>\n<h2><strong>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5<\/strong><\/h2>\n<p>engine.say(text)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6dfb\u52a0\u989d\u5916\u7684\u63d0\u793a\u4fe1\u606f\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4<\/strong><\/h2>\n<p>now = datetime.now()<\/p>\n<p>hour = now.strftime(&quot;%H&quot;)<\/p>\n<p>minute = now.strftime(&quot;%M&quot;)<\/p>\n<h2><strong>\u521d\u59cb\u5316TTS\u5f15\u64ce<\/strong><\/h2>\n<p>engine = pyttsx3.init()<\/p>\n<h2><strong>\u8bbe\u7f6e\u8bed\u901f<\/strong><\/h2>\n<p>engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<h2><strong>\u8bbe\u7f6e\u97f3\u91cf<\/strong><\/h2>\n<p>engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<h2><strong>\u6dfb\u52a0\u989d\u5916\u7684\u63d0\u793a\u4fe1\u606f<\/strong><\/h2>\n<p>text = f&quot;The current time is {hour}:{minute}. Don&#39;t forget to take regular breaks!&quot;<\/p>\n<h2><strong>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5<\/strong><\/h2>\n<p>engine.say(text)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u63d0\u5347\u8bed\u97f3\u64ad\u62a5\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u63d0\u4f9b\u66f4\u52a0\u4eba\u6027\u5316\u7684\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5c06\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u529f\u80fd\u96c6\u6210\u5230\u5b9e\u9645\u5e94\u7528\u4e2d<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u529f\u80fd\u96c6\u6210\u5230\u66f4\u590d\u6742\u7684\u7cfb\u7edf\u4e2d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u5176\u96c6\u6210\u5230\u684c\u9762\u5e94\u7528\u3001Web\u5e94\u7528\u6216\u8005\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u96c6\u6210\u5230\u684c\u9762\u5e94\u7528\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>tkinter<\/code>\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u684c\u9762\u5e94\u7528\uff0c\u5f53\u7528\u6237\u70b9\u51fb\u6309\u94ae\u65f6\u64ad\u62a5\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<p>def get_time():<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<p>    return current_time<\/p>\n<p>def speak_time():<\/p>\n<p>    time = get_time()<\/p>\n<p>    engine = pyttsx3.init()<\/p>\n<p>    engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<p>    engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<p>    text = f&quot;The current time is {time}&quot;<\/p>\n<p>    engine.say(text)<\/p>\n<p>    engine.runAndWait()<\/p>\n<h2><strong>\u521b\u5efaTkinter\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Time Announcer&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6309\u94ae\u5e76\u7ed1\u5b9a\u4e8b\u4ef6<\/strong><\/h2>\n<p>btn = tk.Button(root, text=&quot;Announce Time&quot;, command=speak_time)<\/p>\n<p>btn.pack(pady=20)<\/p>\n<h2><strong>\u8fd0\u884cTkinter\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u96c6\u6210\u5230Web\u5e94\u7528\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>Flask<\/code>\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u5e94\u7528\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\u7279\u5b9aURL\u65f6\u64ad\u62a5\u5f53\u524d\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>def get_time():<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<p>    return current_time<\/p>\n<p>@app.route(&#39;\/announce_time&#39;)<\/p>\n<p>def announce_time():<\/p>\n<p>    time = get_time()<\/p>\n<p>    engine = pyttsx3.init()<\/p>\n<p>    engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<p>    engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<p>    text = f&quot;The current time is {time}&quot;<\/p>\n<p>    engine.say(text)<\/p>\n<p>    engine.runAndWait()<\/p>\n<p>    return &quot;Time announced&quot;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u96c6\u6210\u5230\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u5c06\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u529f\u80fd\u96c6\u6210\u5230\u667a\u80fd\u5bb6\u5c45\u7cfb\u7edf\u4e2d\uff0c\u4f8b\u5982\u4f7f\u7528<code>Home Assistant<\/code>\u5e73\u53f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from homeassistant.components import automation<\/p>\n<p>from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<p>def get_time():<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<p>    return current_time<\/p>\n<p>def speak_time():<\/p>\n<p>    time = get_time()<\/p>\n<p>    engine = pyttsx3.init()<\/p>\n<p>    engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<p>    engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<p>    text = f&quot;The current time is {time}&quot;<\/p>\n<p>    engine.say(text)<\/p>\n<p>    engine.runAndWait()<\/p>\n<p>automation.trigger(&#39;announce_time_event&#39;, speak_time)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u529f\u80fd\u65e0\u7f1d\u96c6\u6210\u5230\u5404\u79cd\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63d0\u4f9b\u66f4\u52a0\u667a\u80fd\u548c\u4fbf\u6377\u7684\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u5904\u7406\u4e0d\u540c\u7684\u8bed\u8a00\u548c\u58f0\u97f3<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u540c\u7684\u8bed\u8a00\u548c\u58f0\u97f3\u3002<code>pyttsx3<\/code>\u5e93\u652f\u6301\u591a\u79cd\u8bed\u8a00\u548c\u58f0\u97f3\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u548c\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6e\u4e0d\u540c\u7684\u8bed\u8a00\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<p>engine = pyttsx3.init()<\/p>\n<p>voices = engine.getProperty(&#39;voices&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e3a\u897f\u73ed\u7259\u8bed<\/strong><\/h2>\n<p>for voice in voices:<\/p>\n<p>    if &quot;spanish&quot; in voice.languages:<\/p>\n<p>        engine.setProperty(&#39;voice&#39;, voice.id)<\/p>\n<p>        break<\/p>\n<p>text = &quot;Hola, \u00bfc\u00f3mo est\u00e1s?&quot;<\/p>\n<p>engine.say(text)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbe\u7f6e\u4e0d\u540c\u7684\u58f0\u97f3\uff1a<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pyttsx3<\/p>\n<p>engine = pyttsx3.init()<\/p>\n<p>voices = engine.getProperty(&#39;voices&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e3a\u5973\u6027\u58f0\u97f3<\/strong><\/h2>\n<p>for voice in voices:<\/p>\n<p>    if &quot;female&quot; in voice.gender:<\/p>\n<p>        engine.setProperty(&#39;voice&#39;, voice.id)<\/p>\n<p>        break<\/p>\n<p>text = &quot;Hello, this is a female voice.&quot;<\/p>\n<p>engine.say(text)<\/p>\n<p>engine.runAndWait()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u8bed\u8a00\u548c\u58f0\u97f3\uff0c\u63d0\u4f9b\u66f4\u52a0\u591a\u6837\u5316\u7684\u8bed\u97f3\u64ad\u62a5\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u5b9a\u65f6\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5b9a\u65f6\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u6bcf\u5c0f\u65f6\u81ea\u52a8\u64ad\u62a5\u4e00\u6b21\u5f53\u524d\u65f6\u95f4\u3002\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u5e93\u5b9e\u73b0\u5b9a\u65f6\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>from datetime import datetime<\/p>\n<p>import pyttsx3<\/p>\n<p>def get_time():<\/p>\n<p>    now = datetime.now()<\/p>\n<p>    current_time = now.strftime(&quot;%H:%M&quot;)<\/p>\n<p>    return current_time<\/p>\n<p>def speak_time():<\/p>\n<p>    time = get_time()<\/p>\n<p>    engine = pyttsx3.init()<\/p>\n<p>    engine.setProperty(&#39;rate&#39;, 150)<\/p>\n<p>    engine.setProperty(&#39;volume&#39;, 1.0)<\/p>\n<p>    text = f&quot;The current time is {time}&quot;<\/p>\n<p>    engine.say(text)<\/p>\n<p>    engine.runAndWait()<\/p>\n<p>def start_timer():<\/p>\n<p>    speak_time()<\/p>\n<p>    threading.Timer(3600, start_timer).start()<\/p>\n<h2><strong>\u5f00\u59cb\u5b9a\u65f6\u5668<\/strong><\/h2>\n<p>start_timer()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5b9a\u65f6\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u7684\u529f\u80fd\uff0c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u7528Python\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u4f7f\u7528<code>datetime<\/code>\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3001\u4f7f\u7528<code>pyttsx3<\/code>\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u3001\u7ed3\u5408\u4e24\u8005\u8fdb\u884c\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u3001\u63d0\u9ad8\u8bed\u97f3\u64ad\u62a5\u7684\u7528\u6237\u4f53\u9a8c\u3001\u5c06\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u529f\u80fd\u96c6\u6210\u5230\u5b9e\u9645\u5e94\u7528\u4e2d\u3001\u5904\u7406\u4e0d\u540c\u7684\u8bed\u8a00\u548c\u58f0\u97f3\u4ee5\u53ca\u5b9a\u65f6\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u7684\u529f\u80fd\uff0c\u63d0\u4f9b\u66f4\u52a0\u667a\u80fd\u548c\u4fbf\u6377\u7684\u670d\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\uff1f<\/strong><br \/>Python\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u529f\u80fd\uff0c\u5982<code>pyttsx3<\/code>\u548c<code>gTTS<\/code>\u3002<code>pyttsx3<\/code>\u662f\u4e00\u4e2a\u6587\u672c\u8f6c\u8bed\u97f3\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u672c\u5730\u8fdb\u884c\u8bed\u97f3\u5408\u6210\uff0c\u800c<code>gTTS<\/code>\u5219\u662f\u57fa\u4e8eGoogle\u7684\u6587\u672c\u8f6c\u8bed\u97f3\u670d\u52a1\u3002\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u5e93\u540e\uff0c\u60a8\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u4f7f\u7528\u8be5\u5e93\u5c06\u65f6\u95f4\u4ee5\u8bed\u97f3\u5f62\u5f0f\u64ad\u62a5\u51fa\u6765\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5e76\u683c\u5f0f\u5316\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>datetime<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u3002<code>datetime.now()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u7ed3\u5408<code>strftime<\/code>\u65b9\u6cd5\u53ef\u4ee5\u683c\u5f0f\u5316\u4e3a\u60a8\u60f3\u8981\u7684\u65f6\u95f4\u683c\u5f0f\uff0c\u4f8b\u5982\u201c\u73b0\u5728\u662f\u4e0b\u5348\u4e09\u70b9\u56db\u5341\u4e94\u5206\u201d\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u8bed\u97f3\u64ad\u62a5\u529f\u80fd\u4e0e\u5b9a\u65f6\u5668\u7ed3\u5408\u4f7f\u7528\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>time.sleep()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6bcf\u9694\u4e00\u5b9a\u65f6\u95f4\u5c31\u8c03\u7528\u8bed\u97f3\u64ad\u62a5\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u6bcf\u5c0f\u65f6\u64ad\u62a5\u4e00\u6b21\u5f53\u524d\u65f6\u95f4\uff0c\u6216\u8005\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u70b9\u8fdb\u884c\u64ad\u62a5\u3002\u7ed3\u5408\u5faa\u73af\u548c\u65f6\u95f4\u51fd\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5b9a\u65f6\u7684\u8bed\u97f3\u64ad\u62a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u8bed\u97f3\u64ad\u62a5\u65f6\u95f4\u7684\u6b65\u9aa4\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u7684\u5e93\u6765\u5b9e\u73b0\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pyttsx3\u3001da [&hellip;]","protected":false},"author":3,"featured_media":1089152,"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\/1089146"}],"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=1089146"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089146\/revisions"}],"predecessor-version":[{"id":1089154,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089146\/revisions\/1089154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089152"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}