{"id":953786,"date":"2024-12-27T01:47:56","date_gmt":"2024-12-26T17:47:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/953786.html"},"modified":"2024-12-27T01:47:58","modified_gmt":"2024-12-26T17:47:58","slug":"%e6%98%93%e8%af%ad%e8%a8%80-%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/953786.html","title":{"rendered":"\u6613\u8bed\u8a00 \u5982\u4f55\u8c03\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094205\/1c497727-ebb1-4a2d-995e-045bf6c59dd6.webp\" alt=\"\u6613\u8bed\u8a00 \u5982\u4f55\u8c03\u7528python\" \/><\/p>\n<p><p> <strong>\u6613\u8bed\u8a00\u8c03\u7528Python\u53ef\u4ee5\u901a\u8fc7<\/strong>\u201c\u4f7f\u7528\u547d\u4ee4\u884c\u3001\u8c03\u7528DLL\u6587\u4ef6\u3001\u4f7f\u7528COM\u7ec4\u4ef6\u201d\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002<strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5<\/strong>\u3002\u901a\u8fc7\u547d\u4ee4\u884c\u65b9\u5f0f\uff0c\u6613\u8bed\u8a00\u53ef\u4ee5\u6267\u884cPython\u811a\u672c\uff0c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u7ed3\u679c\u3002\u4f7f\u7528DLL\u6587\u4ef6\u7684\u65b9\u5f0f\u5219\u9700\u8981\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210DLL\u6587\u4ef6\uff0c\u7136\u540e\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528\u8fd9\u4e2aDLL\u3002\u4f7f\u7528COM\u7ec4\u4ef6\u7684\u65b9\u5f0f\u5219\u53ef\u4ee5\u901a\u8fc7Python\u7684COM\u63a5\u53e3\u5c06Python\u4ee3\u7801\u5bfc\u5165\u5230\u6613\u8bed\u8a00\u4e2d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u5b9e\u73b0\u6613\u8bed\u8a00\u8c03\u7528Python\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Python<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Python\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6613\u8bed\u8a00\u53ef\u4ee5\u76f4\u63a5\u6267\u884cPython\u811a\u672c\uff0c\u5e76\u8bfb\u53d6\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbe\u7f6ePython\u73af\u5883\u53d8\u91cf<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528\u547d\u4ee4\u884c\u8c03\u7528Python\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5Python\uff0c\u5e76\u4e14\u8bbe\u7f6e\u4e86\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u76f4\u63a5\u4f7f\u7528\u201cpython\u201d\u547d\u4ee4\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199Python\u811a\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c\uff0c\u4f8b\u5982\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cexample.py\u201d\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello from Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528Python\u811a\u672c<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6613\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u201c\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u201d\u7684\u547d\u4ee4\u6765\u8c03\u7528Python\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-e\">.\u7248\u672c 2<\/p>\n<p>.\u652f\u6301\u5e93 shell<\/p>\n<p>.\u5b50\u7a0b\u5e8f _\u6309\u94ae1_\u88ab\u5355\u51fb<\/p>\n<p>.\u5c40\u90e8\u53d8\u91cf \u7ed3\u679c, \u6587\u672c\u578b<\/p>\n<p>\u7ed3\u679c \uff1d \u6267\u884c\u5916\u90e8\u7a0b\u5e8f (\u201cpython example.py\u201d, \u771f, \u5047)<\/p>\n<p>\u4fe1\u606f\u6846 (\u7ed3\u679c, 0, \u201cPython\u8f93\u51fa\u201d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7\u201c\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u201d\u51fd\u6570\u6267\u884c\u4e86Python\u811a\u672c\uff0c\u5e76\u5c06\u8f93\u51fa\u7ed3\u679c\u663e\u793a\u5728\u4fe1\u606f\u6846\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8c03\u7528Python\u7f16\u8bd1\u7684DLL\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5c06Python\u4ee3\u7801\u7f16\u8bd1\u6210DLL\u6587\u4ef6\uff0c\u6613\u8bed\u8a00\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9bDLL\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5PyInstaller<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86PyInstaller\uff0c\u7528\u4e8e\u5c06Python\u811a\u672c\u6253\u5305\u6210DLL\u6587\u4ef6\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyinstaller<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199Python\u4ee3\u7801\u5e76\u751f\u6210DLL<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u5199Python\u4ee3\u7801\u5e76\u4f7f\u7528PyInstaller\u751f\u6210DLL\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cexample.py\u201d\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def greet():<\/p>\n<p>    return &quot;Hello from Python DLL!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210DLL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pyinstaller --onefile --dll example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528DLL<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6613\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528DLL\u8c03\u7528\u76f8\u5173\u547d\u4ee4\u6765\u8c03\u7528Python\u7f16\u8bd1\u7684DLL\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-e\">.\u7248\u672c 2<\/p>\n<p>.\u7a0b\u5e8f\u96c6\u5f15\u7528<\/p>\n<p>.\u7a0b\u5e8f\u96c6(\u201c\u7cfb\u7edf\u201d)<\/p>\n<p>.\u7a0b\u5e8f\u96c6\u53d8\u91cf<\/p>\n<p>.\u7a0b\u5e8f\u96c6\u53d8\u91cf(\u201cPythonDLL\u201d, \u201c\u52a8\u6001\u94fe\u63a5\u5e93\u201d, \u201cexample.dll\u201d)<\/p>\n<p>.\u5b50\u7a0b\u5e8f _\u6309\u94ae1_\u88ab\u5355\u51fb<\/p>\n<p>.\u5c40\u90e8\u53d8\u91cf \u7ed3\u679c, \u6587\u672c\u578b<\/p>\n<p>PythonDLL.\u5bfc\u51fa\u5b50\u7a0b\u5e8f(\u201cgreet\u201d) \u7ed3\u679c<\/p>\n<p>\u4fe1\u606f\u6846 (\u7ed3\u679c, 0, \u201cPython DLL\u8f93\u51fa\u201d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728\u6613\u8bed\u8a00\u4e2d\u6210\u529f\u8c03\u7528Python\u7f16\u8bd1\u7684DLL\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528COM\u7ec4\u4ef6\u8c03\u7528Python<\/p>\n<\/p>\n<p><p>\u901a\u8fc7COM\u7ec4\u4ef6\u7684\u65b9\u5f0f\uff0c\u6613\u8bed\u8a00\u53ef\u4ee5\u901a\u8fc7Python\u7684COM\u63a5\u53e3\u8c03\u7528Python\u4ee3\u7801\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5pywin32<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86pywin32\u6a21\u5757\uff0c\u7528\u4e8e\u521b\u5efaPython COM\u670d\u52a1\u5668\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7f16\u5199Python COM\u670d\u52a1\u5668\u4ee3\u7801<\/strong><\/li>\n<\/ol>\n<p><p>\u7f16\u5199Python\u4ee3\u7801\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2aCOM\u670d\u52a1\u5668\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cexample.py\u201d\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.server.register<\/p>\n<p>class PythonCOM:<\/p>\n<p>    _public_methods_ = [&#39;Greet&#39;]<\/p>\n<p>    _reg_progid_ = &quot;PythonCOM.Greeter&quot;<\/p>\n<p>    _reg_clsid_ = &quot;{8CE4A4D0-0F2A-4DF1-AB3F-7C2DAA4B5D7A}&quot;<\/p>\n<p>    def Greet(self):<\/p>\n<p>        return &quot;Hello from Python COM!&quot;<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    win32com.server.register.UseCommandLine(PythonCOM)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6ce8\u518cCOM\u7ec4\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python example.py --register<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528COM\u7ec4\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6613\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528COM\u63a5\u53e3\u76f8\u5173\u547d\u4ee4\u6765\u8c03\u7528Python COM\u7ec4\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-e\">.\u7248\u672c 2<\/p>\n<p>.\u652f\u6301\u5e93 com<\/p>\n<p>.\u5b50\u7a0b\u5e8f _\u6309\u94ae1_\u88ab\u5355\u51fb<\/p>\n<p>.\u5c40\u90e8\u53d8\u91cf PythonCOM, \u5bf9\u8c61\u578b<\/p>\n<p>.\u5c40\u90e8\u53d8\u91cf \u7ed3\u679c, \u6587\u672c\u578b<\/p>\n<p>PythonCOM \uff1d \u521b\u5efa\u5bf9\u8c61 (\u201cPythonCOM.Greeter\u201d)<\/p>\n<p>\u7ed3\u679c \uff1d PythonCOM.\u65b9\u6cd5 (\u201cGreet\u201d)<\/p>\n<p>\u4fe1\u606f\u6846 (\u7ed3\u679c, 0, \u201cPython COM\u8f93\u51fa\u201d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5728\u6613\u8bed\u8a00\u4e2d\u6210\u529f\u8c03\u7528Python\u7684COM\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u5f0f\uff0c\u6613\u8bed\u8a00\u53ef\u4ee5\u4e0ePython\u8fdb\u884c\u4ea4\u4e92\uff0c\u8c03\u7528Python\u7684\u529f\u80fd\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u73af\u5883\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7cfb\u7edf\u547d\u4ee4\u6765\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u5229\u7528\u6613\u8bed\u8a00\u7684\u201c\u8fd0\u884c\u201d\u529f\u80fd\uff0c\u8c03\u7528Python\u89e3\u91ca\u5668\u5e76\u6267\u884c\u6307\u5b9a\u7684\u811a\u672c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<code>\u8fd0\u884c(&quot;python your_script.py&quot;)<\/code>\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u53d8\u91cf\u5df2\u6b63\u786e\u914d\u7f6e\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5de5\u5177\u624d\u80fd\u5728\u6613\u8bed\u8a00\u4e2d\u4f7f\u7528Python\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728\u6613\u8bed\u8a00\u4e2d\u8c03\u7528Python\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5Python\u73af\u5883\uff0c\u5e76\u4e14\u5c06Python\u7684\u5b89\u88c5\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u6b64\u5916\uff0c\u6613\u8bed\u8a00\u7684IDE\u4e5f\u9700\u8981\u652f\u6301\u5916\u90e8\u547d\u4ee4\u8c03\u7528\u529f\u80fd\uff0c\u8fd9\u6837\u624d\u80fd\u987a\u5229\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<p><strong>\u8c03\u7528Python\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u9047\u5230\u54ea\u4e9b\u5e38\u89c1\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u8c03\u7528Python\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u8def\u5f84\u9519\u8bef\u3001Python\u672a\u5b89\u88c5\u3001\u6216\u662f\u811a\u672c\u672c\u8eab\u5b58\u5728\u8bed\u6cd5\u9519\u8bef\u7b49\u95ee\u9898\u3002\u786e\u4fdd\u8def\u5f84\u6b63\u786e\uff0c\u811a\u672c\u53ef\u5728\u547d\u4ee4\u884c\u4e2d\u72ec\u7acb\u8fd0\u884c\uff0c\u5e76\u68c0\u67e5\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u662f\u5426\u5b8c\u5584\uff0c\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u53c2\u6570\u4f20\u9012\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u6765\u63d0\u9ad8\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6613\u8bed\u8a00\u8c03\u7528Python\u53ef\u4ee5\u901a\u8fc7\u201c\u4f7f\u7528\u547d\u4ee4\u884c\u3001\u8c03\u7528DLL\u6587\u4ef6\u3001\u4f7f\u7528COM\u7ec4\u4ef6\u201d\u7b49\u65b9\u5f0f\u5b9e\u73b0\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u662f\u6700\u76f4 [&hellip;]","protected":false},"author":3,"featured_media":953791,"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\/953786"}],"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=953786"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953786\/revisions"}],"predecessor-version":[{"id":953792,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/953786\/revisions\/953792"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/953791"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=953786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=953786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=953786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}