{"id":302554,"date":"2024-05-20T18:38:05","date_gmt":"2024-05-20T10:38:05","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/302554.html"},"modified":"2024-05-20T18:38:20","modified_gmt":"2024-05-20T10:38:20","slug":"%e5%a6%82%e4%bd%95%e7%94%a8-python-%e5%81%9a%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84%e7%bf%bb%e8%af%91%e5%b7%a5%e5%85%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/302554.html","title":{"rendered":"\u5982\u4f55\u7528 Python  \u505a\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26214817\/0764d122-aab0-43ec-b472-8f695aa401f3.webp\" alt=\"\u5982\u4f55\u7528 Python  \u505a\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177\" \/><\/p>\n<p><p>\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7b2c\u4e09\u65b9API\u670d\u52a1\u3001\u4f7f\u7528\u73b0\u6210\u7684\u5e93\u5982googletrans\u3001\u6216\u8005\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u6a21\u578b\u6765\u5b9e\u73b0\u3002<strong>\u6700\u5feb\u901f\u4e14\u6613\u4e8e\u5b9e\u73b0\u7684\u65b9\u5f0f<\/strong>\u662f\u5229\u7528Google Translate API\u6216\u7c7b\u4f3c\u7684\u670d\u52a1\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u63a5\u53e3\u5e76\u652f\u6301\u591a\u79cd\u8bed\u8a00\u3002<strong>\u6269\u5c55\u6027\u826f\u597d\u4e14\u6613\u4e8e\u4f7f\u7528<\/strong>\u7684\u7279\u6027\uff0c\u4f7f\u5f97Python\u6210\u4e3a\u5f00\u53d1\u6b64\u7c7b\u5de5\u5177\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001API\u670d\u52a1\u7684\u9009\u62e9\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684API\u670d\u52a1<\/strong>\u662f\u5b9e\u73b0\u7ffb\u8bd1\u5de5\u5177\u7684\u5173\u952e\u3002\u5e02\u9762\u4e0a\u6709\u5f88\u591a\u7ffb\u8bd1\u670d\u52a1\u63d0\u4f9b\u5546\uff0c\u5982Google Translate\u3001Microsoft Translator\u3001Yandex Translate\u7b49\u3002\u5b83\u4eec\u5927\u90fd\u63d0\u4f9b\u6709\u9650\u514d\u8d39\u914d\u989d\uff0c\u8d85\u51fa\u90e8\u5206\u5219\u9700\u8981\u4ed8\u8d39\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u9009\u7528Google Translate API\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u6ce8\u518cGoogle Cloud\u670d\u52a1\uff0c\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\u5e76\u542f\u7528Google Translate API\u3002\u7136\u540e\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2aAPI\u5bc6\u94a5\u3002\u4fdd\u62a4\u8fd9\u4e2a\u5bc6\u94a5\u4e0d\u88ab\u6cc4\u9732\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u6709\u4e86\u5b83\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u4f7f\u7528\u4f60\u7684\u914d\u989d\u8fdb\u884c\u7ffb\u8bd1\u3002<\/p>\n<\/p>\n<p><h4>\u4e8c\u3001\u8bbe\u7f6e\u5f00\u53d1\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u9996\u5148\u786e\u4fdd\u4f60\u5b89\u88c5\u4e86Python\u3002\u7136\u540e\u4f7f\u7528pip\u5b89\u88c5googletrans\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u514d\u8d39\u4e14\u4e0d\u9700\u8981API\u5bc6\u94a5\u7684Python\u63a5\u53e3\uff0c\u7528\u4e8e\u8c03\u7528Google Translate\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install googletrans==4.0.0-rc1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e09\u3001\u7f16\u5199\u7ffb\u8bd1\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u7f16\u5199\u7ffb\u8bd1\u5de5\u5177\u7684\u4ee3\u7801\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u5bfc\u5165\u5e93\u5e76\u521b\u5efa\u7ffb\u8bd1\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from googletrans import Translator<\/p>\n<p>def translate_text(text, dest=&#039;en&#039;):<\/p>\n<p>    translator = Translator()<\/p>\n<p>    translation = translator.translate(text, dest)<\/p>\n<p>    return translation.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5b9a\u4e49\u4e86translate_text\u51fd\u6570\uff0c\u5b83\u63a5\u6536\u8981\u7ffb\u8bd1\u7684\u6587\u672c\u548c\u76ee\u6807\u8bed\u8a00\u4ee3\u7801\uff08\u9ed8\u8ba4\u4e3a&#039;\u82f1\u6587&#039;\uff09\uff0c\u7136\u540e\u8fd4\u56de\u7ffb\u8bd1\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>\u56db\u3001\u589e\u5f3a\u5de5\u5177\u529f\u80fd<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u7ffb\u8bd1\u5de5\u5177\u66f4\u52a0\u5b9e\u7528\uff0c\u4f60\u53ef\u4ee5\u589e\u52a0\u8bed\u8a00\u63a2\u6d4b\u529f\u80fd\uff0c\u8ba9\u5de5\u5177\u80fd\u81ea\u52a8\u8bc6\u522b\u8f93\u5165\u6587\u672c\u7684\u8bed\u8a00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def detect_language(text):<\/p>\n<p>    translator = Translator()<\/p>\n<p>    detection = translator.detect(text)<\/p>\n<p>    return detection.lang<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2adetect_language\u51fd\u6570\u5c06\u8fd4\u56de\u8f93\u5165\u6587\u672c\u7684\u8bed\u8a00\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u4e94\u3001\u6784\u5efa\u57fa\u4e8e\u6a21\u578b\u7684\u7ffb\u8bd1\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u60f3\u5728\u6ca1\u6709\u7b2c\u4e09\u65b9API\u7684\u60c5\u51b5\u4e0b\u8fd0\u884c\u4f60\u7684\u7ffb\u8bd1\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5f00\u6e90\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\u6765\u6784\u5efa\u81ea\u5df1\u7684\u7ffb\u8bd1\u6a21\u578b\u3002\u8fd9\u65e0\u7591\u9700\u8981\u66f4\u591a\u7684\u5de5\u4f5c\uff0c\u4f46\u4f1a\u63d0\u4f9b\u66f4\u591a\u7684\u81ea\u7531\u5ea6\u548c\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528OpenNMT\u6216TensorFlow\u7684\u5e8f\u5217\u5230\u5e8f\u5217\u6a21\u578b(sequence-to-sequence models)\u53ef\u4ee5\u8bad\u7ec3\u81ea\u5df1\u7684\u7ffb\u8bd1\u7cfb\u7edf\u3002\u7136\u800c\uff0c\u8fd9\u5c31\u9700\u8981\u5927\u91cf\u7684\u53cc\u8bed\u6570\u636e\u96c6\u6765\u8bad\u7ec3\u6a21\u578b\uff0c\u5e76\u4e14\u6d89\u53ca\u5230\u81ea\u7136\u8bed\u8a00\u5904\u7406\u548c\u673a\u5668\u5b66\u4e60\u7684\u6df1\u5165\u77e5\u8bc6\u3002<\/p>\n<\/p>\n<p><h4>\u516d\u3001\u7528\u6237\u754c\u9762\u6784\u5efa<\/h4>\n<\/p>\n<p><p>\u7ffb\u8bd1\u5de5\u5177\u7684\u540e\u7aef\u903b\u8f91\u5b8c\u6210\u4e4b\u540e\uff0c\u4f60\u53ef\u80fd\u8fd8\u4f1a\u5e0c\u671b\u4e3a\u5176\u6dfb\u52a0\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u754c\u9762\uff0c\u4ee5\u6539\u5584\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tkinter import *<\/p>\n<p>root = Tk()<\/p>\n<p>root.title(&quot;Python \u7ffb\u8bd1\u5de5\u5177&quot;)<\/p>\n<h2><strong>\u7528\u6237\u8f93\u5165\u6587\u672c\u7684\u6587\u672c\u6846<\/strong><\/h2>\n<p>text_to_translate = Text(root, height=10, width=50)<\/p>\n<p>text_to_translate.pack()<\/p>\n<h2><strong>\u663e\u793a\u7ffb\u8bd1\u7ed3\u679c\u7684\u6807\u7b7e<\/strong><\/h2>\n<p>translated_text = Label(root, text=&quot;&quot;, height=10, width=50, bg=&quot;light yellow&quot;)<\/p>\n<p>translated_text.pack()<\/p>\n<h2><strong>\u7ffb\u8bd1\u6309\u94ae\u548c\u5176\u903b\u8f91<\/strong><\/h2>\n<p>def on_translate():<\/p>\n<p>    input_text = text_to_translate.get(&quot;1.0&quot;, &quot;end-1c&quot;)<\/p>\n<p>    translated = translate_text(input_text)<\/p>\n<p>    translated_text.config(text=translated)<\/p>\n<p>translate_button = Button(root, text=&quot;\u7ffb\u8bd1&quot;, command=on_translate)<\/p>\n<p>translate_button.pack()<\/p>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u6784\u5efa\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u8f93\u5165\u6587\u672c\u3001\u70b9\u51fb\u7ffb\u8bd1\u6309\u94ae\u5e76\u67e5\u770b\u7ffb\u8bd1\u540e\u7684\u6587\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b89\u5168\u548c\u9690\u79c1\u8003\u91cf<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u7ffb\u8bd1\u5de5\u5177\u65f6\uff0c<strong>\u8003\u8651\u5230\u5b89\u5168\u548c\u9690\u79c1\u662f\u81f3\u5173\u91cd\u8981\u7684<\/strong>\u3002\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9API\u670d\u52a1\u6765\u5904\u7406\u7ffb\u8bd1\u4efb\u52a1\uff0c\u8bf7\u786e\u4fdd\u7528\u6237\u7684\u6570\u636e\u901a\u8fc7\u5b89\u5168\u7684\u8fde\u63a5\u4f20\u8f93\uff0c\u800c\u4e14\u4e0d\u4f1a\u975e\u6cd5\u5b58\u50a8\u6216\u5904\u7406\u7528\u6237\u7684\u79c1\u5bc6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u5982\u679c\u4f60\u51b3\u5b9a\u81ea\u5df1\u5b58\u50a8\u7528\u6237\u6570\u636e\uff0c\u786e\u4fdd\u9075\u5b88\u9002\u7528\u7684\u6570\u636e\u4fdd\u62a4\u6cd5\u89c4\uff0c\u4f8b\u5982GDPR\u6216CCPA\uff0c\u5e76\u5728\u5de5\u5177\u4e2d\u660e\u786e\u5730\u6307\u51fa\u4f60\u7684\u6570\u636e\u5904\u7406\u548c\u5b58\u50a8\u653f\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1aPython \u4e2d\u5982\u4f55\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177\uff1f<\/strong><\/p>\n<p>\u5e38\u7528\u7684Python\u5e93\u6709\u54ea\u4e9b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177\u5462\uff1f\u60a8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5982<code>googletrans<\/code>\u3001<code>translate<\/code>\u6216\u8005<code>pydeepl<\/code>\u7b49\u5f00\u6e90\u5e93\u6765\u5b9e\u73b0\u6587\u5b57\u7ffb\u8bd1\u529f\u80fd\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u7b80\u6d01\u6613\u7528\u7684API\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7ffb\u8bd1\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u4f7f\u7528 Python \u8fdb\u884c\u6587\u672c\u7ffb\u8bd1\uff1f<\/strong><\/p>\n<p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6587\u672c\u7ffb\u8bd1\u65f6\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u7ffb\u8bd1\u5e93\u3002\u4e00\u65e6\u60a8\u5b89\u88c5\u4e86\u9009\u5b9a\u7684\u7ffb\u8bd1\u5e93\uff0c\u60a8\u53ef\u4ee5\u6309\u7167\u5e93\u63d0\u4f9b\u7684\u6587\u6863\u548c\u793a\u4f8b\u4ee3\u7801\u6765\u8c03\u7528\u7ffb\u8bd1\u529f\u80fd\u3002\u901a\u5e38\uff0c\u60a8\u9700\u8981\u63d0\u4f9b\u5f85\u7ffb\u8bd1\u7684\u6587\u672c\u548c\u76ee\u6807\u8bed\u8a00\uff0c\u7136\u540e\u901a\u8fc7API\u8c03\u7528\u7ffb\u8bd1\u51fd\u6570\u6765\u83b7\u53d6\u7ffb\u8bd1\u7ed3\u679c\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a\u6709\u6ca1\u6709\u4ec0\u4e48\u6ce8\u610f\u4e8b\u9879\u53ef\u4ee5\u5e2e\u52a9\u6211\u66f4\u597d\u5730\u4f7f\u7528 Python \u8fdb\u884c\u7ffb\u8bd1\u5de5\u5177\u7684\u5f00\u53d1\uff1f<\/strong><\/p>\n<p>\u786e\u4fdd\u5728\u4f7f\u7528Python\u5f00\u53d1\u7ffb\u8bd1\u5de5\u5177\u65f6\uff0c\u60a8\u5df2\u7ecf\u914d\u7f6e\u597d\u6240\u6709\u5fc5\u9700\u7684\u4f9d\u8d56\u9879\uff0c\u5e76\u719f\u6089\u6240\u9009\u5e93\u7684\u529f\u80fd\u3002\u53e6\u5916\uff0c\u6ce8\u610f\u5e93\u7684\u4f7f\u7528\u89c4\u8303\uff0c\u4f8b\u5982\u8bf7\u9075\u5faaAPI\u7684\u4f7f\u7528\u9650\u5236\uff0c\u907f\u514d\u9891\u7e41\u8c03\u7528\u63a5\u53e3\u88ab\u5c01\u7981\u3002\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u5bf9\u7ffb\u8bd1\u7ed3\u679c\u8fdb\u884c\u4e00\u5b9a\u7a0b\u5ea6\u7684\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u7ffb\u8bd1\u8d28\u91cf\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7ffb\u8bd1\u5de5\u5177\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u7b2c\u4e09\u65b9API\u670d\u52a1\u3001\u4f7f\u7528\u73b0\u6210\u7684\u5e93\u5982googletrans\u3001\u6216\u8005 [&hellip;]","protected":false},"author":3,"featured_media":302573,"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\/302554"}],"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=302554"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/302554\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/302573"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=302554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=302554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=302554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}