{"id":927551,"date":"2024-12-26T16:14:13","date_gmt":"2024-12-26T08:14:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927551.html"},"modified":"2024-12-26T16:14:16","modified_gmt":"2024-12-26T08:14:16","slug":"vba%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/927551.html","title":{"rendered":"vba\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\/25063632\/7fa79078-a7bc-431e-96f6-88ffa2519130.webp\" alt=\"vba\u5982\u4f55\u8c03\u7528python\" \/><\/p>\n<p><p> <strong>\u5728VBA\u4e2d\u8c03\u7528Python\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u5e76\u4f7f\u7528Shell\u547d\u4ee4\u8fd0\u884c\u3001\u4f7f\u7528COM\u5bf9\u8c61\u548c\u901a\u8fc7VBA\u8c03\u7528Python API\u6765\u5b9e\u73b0<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Shell\u547d\u4ee4\u8fd0\u884cPython\u811a\u672c\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4f46\u529f\u80fd\u8f83\u4e3a\u6709\u9650\uff1b\u4f7f\u7528COM\u5bf9\u8c61\u9700\u8981\u4e00\u4e9b\u989d\u5916\u7684\u914d\u7f6e\uff0c\u4f46\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u590d\u6742\u7684\u529f\u80fd\uff1b\u800c\u901a\u8fc7VBA\u8c03\u7528Python API\u5219\u662f\u6700\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u9002\u5408\u9700\u8981\u9891\u7e41\u4ea4\u4e92\u7684\u573a\u666f\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Shell\u547d\u4ee4\u8fd0\u884cPython\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Shell\u547d\u4ee4\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u5728VBA\u4e2d\u8c03\u7528Shell\u51fd\u6570\u6765\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaPython\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>script.py<\/code>\u7684\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u542b\u7b80\u5355\u7684Python\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># script.py<\/p>\n<p>print(&quot;Hello from Python!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VBA\u4e2d\u8c03\u7528Python\u811a\u672c<\/strong><\/p>\n<\/p>\n<p><p>\u5728VBA\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Shell\u51fd\u6570\u6765\u8c03\u7528Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScript()<\/p>\n<p>    Dim pyScriptPath As String<\/p>\n<p>    Dim pythonExe As String<\/p>\n<p>    Dim command As String<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonExe = &quot;C:\\Path\\To\\Python\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    pyScriptPath = &quot;C:\\Path\\To\\Script\\script.py&quot;<\/p>\n<p>    &#39; \u62fc\u63a5\u547d\u4ee4<\/p>\n<p>    command = pythonExe &amp; &quot; &quot; &amp; pyScriptPath<\/p>\n<p>    &#39; \u8c03\u7528Shell\u51fd\u6570\u6267\u884c\u547d\u4ee4<\/p>\n<p>    Shell command, vbNormalFocus<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a\u8981\u786e\u4fddPython\u7684\u8def\u5f84\u548c\u811a\u672c\u7684\u8def\u5f84\u662f\u6b63\u786e\u7684\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528COM\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528COM\u5bf9\u8c61\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u590d\u6742\u7684\u529f\u80fd\uff0c\u5e76\u5141\u8bb8VBA\u4e0ePython\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u3002\u53ef\u4ee5\u4f7f\u7528<code>win32com<\/code>\u5e93\u5728Python\u4e2d\u521b\u5efaCOM\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5<code>pywin32<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5<code>pywin32<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaPython COM\u670d\u52a1\u5668<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\uff0c\u4f8b\u5982<code>com_server.py<\/code>\uff0c\u4f5c\u4e3aCOM\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pythoncom<\/p>\n<p>from win32com.server.dispatcher import DefaultQueryInterface<\/p>\n<p>class PythonCOMServer:<\/p>\n<p>    _public_methods_ = [&#39;Hello&#39;]<\/p>\n<p>    _reg_progid_ = &quot;PythonCOMServer&quot;<\/p>\n<p>    _reg_clsid_ = pythoncom.CreateGuid()<\/p>\n<p>    def Hello(self):<\/p>\n<p>        return &quot;Hello from Python COM Server!&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>    import win32com.server.register<\/p>\n<p>    win32com.server.register.UseCommandLine(PythonCOMServer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u6ce8\u518cCOM\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python com_server.py --register<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VBA\u4e2d\u4f7f\u7528COM\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u5728VBA\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efaCOM\u5bf9\u8c61\u6765\u8c03\u7528Python\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub UsePythonCOMServer()<\/p>\n<p>    Dim pythonServer As Object<\/p>\n<p>    Set pythonServer = CreateObject(&quot;PythonCOMServer&quot;)<\/p>\n<p>    MsgBox pythonServer.Hello()<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd\u6ce8\u518cCOM\u670d\u52a1\u5668\u65f6\u4f7f\u7528\u7684\u662f\u7ba1\u7406\u5458\u6743\u9650\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u901a\u8fc7VBA\u8c03\u7528Python API<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7VBA\u8c03\u7528Python API\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u548c\u52a8\u6001\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u9700\u8981\u9891\u7e41\u4ea4\u4e92\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5<code>pywin32<\/code>\u548c<code>pyxll<\/code>\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9700\u8981\u4f7f\u7528<code>pywin32<\/code>\u548c<code>pyxll<\/code>\u5e93\u6765\u5b9e\u73b0Python API\u8c03\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32 pyxll<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efaPython API<\/strong><\/p>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aPython API\u6a21\u5757\uff0c\u4f8b\u5982<code>api.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VBA\u4e2d\u8c03\u7528Python API<\/strong><\/p>\n<\/p>\n<p><p>\u5728VBA\u4e2d\uff0c\u4f7f\u7528<code>pyxll<\/code>\u6765\u8c03\u7528Python API\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub CallPythonAPI()<\/p>\n<p>    Dim result As Variant<\/p>\n<p>    &#39; \u8c03\u7528Python\u7684add\u51fd\u6570<\/p>\n<p>    result = PyXLL.Call(&quot;api.add&quot;, 5, 3)<\/p>\n<p>    MsgBox &quot;Addition Result: &quot; &amp; result<\/p>\n<p>    &#39; \u8c03\u7528Python\u7684subtract\u51fd\u6570<\/p>\n<p>    result = PyXLL.Call(&quot;api.subtract&quot;, 5, 3)<\/p>\n<p>    MsgBox &quot;Subtraction Result: &quot; &amp; result<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6ce8\u610f<\/strong>\uff1a\u786e\u4fdd<code>pyxll<\/code>\u5df2\u7ecf\u6b63\u786e\u914d\u7f6e\uff0c\u5e76\u4e14Python API\u6a21\u5757\u7684\u8def\u5f84\u8bbe\u7f6e\u6b63\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728VBA\u4e2d\u8c03\u7528Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u9700\u6c42\u7684\u590d\u6742\u6027\u548c\u4ea4\u4e92\u9891\u7387\u3002\u4f7f\u7528Shell\u547d\u4ee4\u9002\u5408\u7b80\u5355\u811a\u672c\u6267\u884c\uff0c\u4f7f\u7528COM\u5bf9\u8c61\u9002\u5408\u9700\u8981\u53cc\u5411\u901a\u4fe1\u7684\u573a\u666f\uff0c\u800c\u901a\u8fc7VBA\u8c03\u7528Python API\u5219\u9002\u5408\u590d\u6742\u7684\u4ea4\u4e92\u9700\u6c42\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fddPython\u73af\u5883\u914d\u7f6e\u6b63\u786e\uff0c\u5e76\u4e14VBA\u548cPython\u4e4b\u95f4\u7684\u8def\u5f84\u548c\u5f15\u7528\u8bbe\u7f6e\u6b63\u786e\uff0c\u4ee5\u4fdd\u8bc1\u8c03\u7528\u7684\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728VBA\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u8981\u5728VBA\u4e2d\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528Shell\u547d\u4ee4\u3002\u9996\u5148\uff0c\u786e\u4fddPython\u5df2\u5b89\u88c5\u5e76\u914d\u7f6e\u5728\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-vba\">Sub RunPythonScript()\n    Dim Ret_Val\n    Ret_Val = Shell(&quot;C:\\Path\\To\\Python.exe C:\\Path\\To\\YourScript.py&quot;, vbNormalFocus)\nEnd Sub\n<\/code><\/pre>\n<p>\u66ff\u6362\u8def\u5f84\u4e3a\u5b9e\u9645\u7684Python\u53ef\u6267\u884c\u6587\u4ef6\u548c\u811a\u672c\u4f4d\u7f6e\u3002\u8fd0\u884c\u6b64\u5b8f\u65f6\uff0cPython\u811a\u672c\u5c06\u88ab\u6267\u884c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728VBA\u4e2d\u83b7\u53d6Python\u811a\u672c\u7684\u8f93\u51fa\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u6587\u672c\u6587\u4ef6\u6765\u6355\u83b7Python\u811a\u672c\u7684\u8f93\u51fa\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u5c06\u7ed3\u679c\u5199\u5165\u6587\u4ef6\uff0c\u7136\u540e\u5728VBA\u4e2d\u8bfb\u53d6\u8be5\u6587\u4ef6\u3002\u793a\u4f8bPython\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    f.write(&quot;Hello from Python!&quot;)\n<\/code><\/pre>\n<p>\u5728VBA\u4e2d\u53ef\u4ee5\u4f7f\u7528FileSystemObject\u8bfb\u53d6\u8be5\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728VBA\u4e2d\u5982\u4f55\u5904\u7406Python\u811a\u672c\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728VBA\u4e2d\uff0c\u5904\u7406Python\u811a\u672c\u7684\u9519\u8bef\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5Shell\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u3002\u8fd4\u56de\u503c\u4e3a0\u8868\u793a\u811a\u672c\u672a\u6210\u529f\u6267\u884c\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\uff0c\u786e\u4fdd\u5728\u9519\u8bef\u53d1\u751f\u65f6\u8f93\u51fa\u8be6\u7ec6\u4fe1\u606f\u5230\u65e5\u5fd7\u6587\u4ef6\u3002\u8fd9\u6837\u53ef\u4ee5\u5728VBA\u4e2d\u8bfb\u53d6\u65e5\u5fd7\u6587\u4ef6\u6765\u83b7\u53d6\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728VBA\u4e2d\u8c03\u7528Python\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u5e76\u4f7f\u7528Shell\u547d\u4ee4\u8fd0\u884c\u3001\u4f7f\u7528COM\u5bf9\u8c61\u548c\u901a\u8fc7VBA [&hellip;]","protected":false},"author":3,"featured_media":927554,"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\/927551"}],"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=927551"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927551\/revisions"}],"predecessor-version":[{"id":927555,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/927551\/revisions\/927555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/927554"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=927551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=927551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=927551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}