{"id":1183214,"date":"2025-01-15T19:12:01","date_gmt":"2025-01-15T11:12:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183214.html"},"modified":"2025-01-15T19:12:03","modified_gmt":"2025-01-15T11:12:03","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8vb%e4%b8%ad%e4%bd%bf%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183214.html","title":{"rendered":"\u5982\u4f55\u5728vb\u4e2d\u4f7f\u7528python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133042\/d7787414-f38a-4a2f-b69d-16fe83cb54ac.webp\" alt=\"\u5982\u4f55\u5728vb\u4e2d\u4f7f\u7528python\" \/><\/p>\n<p><p> <strong>\u5728VB\u4e2d\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Python\u811a\u672c\u3001\u4f7f\u7528COM\u63a5\u53e3\u3001\u4f7f\u7528IronPython\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528COM\u63a5\u53e3\u662f\u4e00\u4e2a\u8f83\u4e3a\u76f4\u63a5\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aCOM\u5bf9\u8c61\uff0c\u4f60\u53ef\u4ee5\u5728VB\u4e2d\u8c03\u7528Python\u51fd\u6570\u5e76\u83b7\u53d6\u5176\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u63cf\u8ff0\u548c\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8c03\u7528Python\u811a\u672c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7f16\u5199Python\u811a\u672c<\/strong>\uff1a\u9996\u5148\uff0c\u5728Python\u4e2d\u7f16\u5199\u4f60\u9700\u8981\u6267\u884c\u7684\u811a\u672c\uff0c\u5e76\u4fdd\u5b58\u4e3a\u4e00\u4e2a.py\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Python\u811a\u672c <code>example.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    import sys<\/p>\n<p>    a = int(sys.argv[1])<\/p>\n<p>    b = int(sys.argv[2])<\/p>\n<p>    print(add(a, b))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VB\u4e2d\u8c03\u7528Python\u811a\u672c<\/strong>\uff1a\u5728VB\u4e2d\uff0c\u901a\u8fc7Shell\u547d\u4ee4\u8c03\u7528Python\u811a\u672c\uff0c\u5e76\u6355\u83b7\u5176\u8f93\u51fa\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vb\">Dim pythonScript As String<\/p>\n<p>pythonScript = &quot;C:\\path\\to\\example.py&quot;<\/p>\n<p>Dim a As Integer<\/p>\n<p>Dim b As Integer<\/p>\n<p>a = 10<\/p>\n<p>b = 20<\/p>\n<p>Dim command As String<\/p>\n<p>command = &quot;python &quot; &amp; pythonScript &amp; &quot; &quot; &amp; a &amp; &quot; &quot; &amp; b<\/p>\n<p>Dim shell As Object<\/p>\n<p>Set shell = CreateObject(&quot;WScript.Shell&quot;)<\/p>\n<p>Dim exec As Object<\/p>\n<p>Set exec = shell.Exec(command)<\/p>\n<p>Dim output As String<\/p>\n<p>output = exec.StdOut.ReadAll()<\/p>\n<p>MsgBox &quot;The result is: &quot; &amp; output<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528COM\u63a5\u53e3<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python for Windows Extensions<\/strong>\uff1a\u786e\u4fdd\u5b89\u88c5\u4e86<code>pywin32<\/code>\u5e93\uff0c\u8fd9\u662fPython\u7684Windows\u6269\u5c55\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9COM\u63a5\u53e3\u7684\u652f\u6301\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<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u811a\u672c<\/strong>\uff1a\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u6ce8\u518c\u4e00\u4e2aCOM\u5bf9\u8c61\u3002\u4f8b\u5982\uff0c\u521b\u5efa <code>com_example.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.server.register<\/p>\n<p>class PythonCOMServer:<\/p>\n<p>    _reg_clsid_ = &quot;{00000000-0000-0000-0000-000000000000}&quot;<\/p>\n<p>    _reg_desc_ = &quot;Python COM Server&quot;<\/p>\n<p>    _reg_progid_ = &quot;PythonCOMServer.Application&quot;<\/p>\n<p>    _public_methods_ = [&#39;Add&#39;]<\/p>\n<p>    def Add(self, a, b):<\/p>\n<p>        return a + b<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    win32com.server.register.UseCommandLine(PythonCOMServer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u6b64\u811a\u672c\u4ee5\u6ce8\u518cCOM\u670d\u52a1\u5668\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python com_example.py --register<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VB\u4e2d\u4f7f\u7528COM\u5bf9\u8c61<\/strong>\uff1a\u5728VB\u4e2d\u521b\u5efaCOM\u5bf9\u8c61\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vb\">Dim pythonCom As Object<\/p>\n<p>Set pythonCom = CreateObject(&quot;PythonCOMServer.Application&quot;)<\/p>\n<p>Dim result As Integer<\/p>\n<p>result = pythonCom.Add(10, 20)<\/p>\n<p>MsgBox &quot;The result is: &quot; &amp; result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528IronPython<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5IronPython<\/strong>\uff1a\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5IronPython\uff0c\u8fd9\u662f\u4e00\u4e2a\u8fd0\u884c\u5728.NET\u6846\u67b6\u4e0a\u7684Python\u5b9e\u73b0\u3002\u53ef\u4ee5\u4ece<a href=\"http:\/\/ironpython.net\/\">IronPython\u5b98\u7f51<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199Python\u811a\u672c<\/strong>\uff1a\u7f16\u5199\u4e00\u4e2aPython\u811a\u672c\uff0c\u5e76\u4fdd\u5b58\u4e3a <code>.py<\/code> \u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa <code>example.py<\/code>\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5728VB\u4e2d\u8c03\u7528IronPython<\/strong>\uff1a\u5728VB\u4e2d\u4f7f\u7528IronPython\u7684<code>Microsoft.Scripting.Hosting<\/code>\u5e93\u6765\u6267\u884cPython\u811a\u672c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vb\">Imports Microsoft.Scripting.Hosting<\/p>\n<p>Imports IronPython.Hosting<\/p>\n<p>Module Module1<\/p>\n<p>    Sub Main()<\/p>\n<p>        Dim pyEngine As ScriptEngine<\/p>\n<p>        Dim pyScope As ScriptScope<\/p>\n<p>        pyEngine = Python.CreateEngine()<\/p>\n<p>        pyScope = pyEngine.CreateScope()<\/p>\n<p>        pyEngine.ExecuteFile(&quot;C:\\path\\to\\example.py&quot;, pyScope)<\/p>\n<p>        Dim addFunction As Object<\/p>\n<p>        addFunction = pyScope.GetVariable(&quot;add&quot;)<\/p>\n<p>        Dim result As Integer<\/p>\n<p>        result = addFunction(10, 20)<\/p>\n<p>        MsgBox(&quot;The result is: &quot; &amp; result)<\/p>\n<p>    End Sub<\/p>\n<p>End Module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5d4c\u5165Python\u89e3\u91ca\u5668<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Python<\/strong>\uff1a\u786e\u4fdd\u5b89\u88c5\u4e86Python\uff0c\u5e76\u4e14Python\u7684\u8def\u5f84\u5df2\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Python C API<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7Python\u7684C API\u5728VB\u4e2d\u5d4c\u5165Python\u89e3\u91ca\u5668\u3002\u8fd9\u79cd\u65b9\u6cd5\u8f83\u4e3a\u590d\u6742\uff0c\u9700\u8981\u4f7f\u7528C\/C++\u7f16\u5199\u4e00\u4e2a\u52a8\u6001\u94fe\u63a5\u5e93\uff08DLL\uff09\uff0c\u5e76\u5728VB\u4e2d\u8c03\u7528\u8be5DLL\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5316\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>2.1 <strong>\u7f16\u5199C\/C++\u4ee3\u7801<\/strong>\uff1a\u7f16\u5199\u4e00\u4e2aC\/C++\u4ee3\u7801\uff0c\u5d4c\u5165Python\u89e3\u91ca\u5668\u3002\u4f8b\u5982\uff0c\u521b\u5efa <code>embed_python.cpp<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">#include &lt;Python.h&gt;<\/p>\n<p>extern &quot;C&quot; __declspec(dllexport) int Add(int a, int b) {<\/p>\n<p>    Py_Initialize();<\/p>\n<p>    PyObject* pName, * <a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/52627.html\" target=\"_blank\">PMO<\/a>dule, * pFunc;<\/p>\n<p>    PyObject* pArgs, * pValue;<\/p>\n<p>    int result = 0;<\/p>\n<p>    pName = PyUnicode_DecodeFSDefault(&quot;example&quot;);<\/p>\n<p>    pModule = PyImport_Import(pName);<\/p>\n<p>    Py_DECREF(pName);<\/p>\n<p>    if (pModule != NULL) {<\/p>\n<p>        pFunc = PyObject_GetAttrString(pModule, &quot;add&quot;);<\/p>\n<p>        if (PyCallable_Check(pFunc)) {<\/p>\n<p>            pArgs = PyTuple_Pack(2, PyLong_FromLong(a), PyLong_FromLong(b));<\/p>\n<p>            pValue = PyObject_CallObject(pFunc, pArgs);<\/p>\n<p>            Py_DECREF(pArgs);<\/p>\n<p>            if (pValue != NULL) {<\/p>\n<p>                result = PyLong_AsLong(pValue);<\/p>\n<p>                Py_DECREF(pValue);<\/p>\n<p>            }<\/p>\n<p>        }<\/p>\n<p>        Py_XDECREF(pFunc);<\/p>\n<p>        Py_DECREF(pModule);<\/p>\n<p>    }<\/p>\n<p>    Py_Finalize();<\/p>\n<p>    return result;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2 <strong>\u7f16\u8bd1\u4e3aDLL<\/strong>\uff1a\u5c06\u4e0a\u8ff0\u4ee3\u7801\u7f16\u8bd1\u4e3aDLL\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Visual Studio\u521b\u5efa\u4e00\u4e2aDLL\u9879\u76ee\uff0c\u5e76\u5c06\u4ee3\u7801\u6dfb\u52a0\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n<\/p>\n<p><p>2.3 <strong>\u5728VB\u4e2d\u8c03\u7528DLL<\/strong>\uff1a\u5728VB\u4e2d\u58f0\u660e\u5e76\u8c03\u7528DLL\u4e2d\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vb\">Declare Function Add Lib &quot;C:\\path\\to\\embed_python.dll&quot; (ByVal a As Integer, ByVal b As Integer) As Integer<\/p>\n<p>Sub Main()<\/p>\n<p>    Dim result As Integer<\/p>\n<p>    result = Add(10, 20)<\/p>\n<p>    MsgBox(&quot;The result is: &quot; &amp; result)<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728VB\u4e2d\u4f7f\u7528Python\uff0c\u5b9e\u73b0\u8de8\u8bed\u8a00\u8c03\u7528\u548c\u6570\u636e\u5904\u7406\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728VB\u4e2d\u8c03\u7528Python\u811a\u672c\uff1f<\/strong><br \/>\u5728VB\u4e2d\u8c03\u7528Python\u811a\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528Process\u7c7b\u6765\u542f\u52a8\u4e00\u4e2a\u65b0\u7684\u8fdb\u7a0b\uff0c\u8fd0\u884cPython\u89e3\u91ca\u5668\u5e76\u4f20\u5165\u811a\u672c\u8def\u5f84\u3002\u9996\u5148\uff0c\u9700\u8981\u786e\u4fddPython\u5df2\u5b89\u88c5\u5e76\u4e14\u5728\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\u8bbe\u7f6e\u4e86\u8def\u5f84\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u5b9e\u73b0\uff1a  <\/p>\n<pre><code class=\"language-vb\">Dim psi As New ProcessStartInfo(&quot;python.exe&quot;, &quot;C:\\path\\to\\your_script.py&quot;)\npsi.UseShellExecute = False\npsi.RedirectStandardOutput = True\nDim process As Process = Process.Start(psi)\nDim output As String = process.StandardOutput.ReadToEnd()\nprocess.WaitForExit()\nConsole.WriteLine(output)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5c31\u80fd\u5728VB\u4e2d\u6267\u884cPython\u811a\u672c\u5e76\u83b7\u53d6\u5176\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5728VB\u4e2d\u5982\u4f55\u5904\u7406Python\u8fd4\u56de\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u5904\u7406Python\u811a\u672c\u8fd4\u56de\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u6807\u51c6\u8f93\u51fa\u8fdb\u884c\u6355\u6349\u3002\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\u4e2d\u4f7f\u7528<code>RedirectStandardOutput<\/code>\u5c5e\u6027\u6765\u83b7\u53d6Python\u811a\u672c\u7684\u8f93\u51fa\u3002\u53ef\u4ee5\u5c06\u8f93\u51fa\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u8fdb\u4e00\u6b65\u8fdb\u884c\u89e3\u6790\u6216\u663e\u793a\u3002\u6839\u636e\u9700\u8981\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5206\u5272\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728VB\u9879\u76ee\u4e2d\u7ba1\u7406Python\u5e93\u548c\u4f9d\u8d56\uff1f<\/strong><br \/>\u5728VB\u9879\u76ee\u4e2d\u4f7f\u7528Python\u65f6\uff0c\u786e\u4fdd\u6240\u9700\u7684Python\u5e93\u548c\u4f9d\u8d56\u5df2\u7ecf\u5b89\u88c5\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3002\u4f8b\u5982\uff0c\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165<code>pip install package_name<\/code>\u6765\u5b89\u88c5\u5e93\u3002\u5728VB\u4e2d\u8c03\u7528Python\u811a\u672c\u65f6\uff0c\u786e\u4fdd\u8fd9\u4e9b\u5e93\u5728Python\u73af\u5883\u4e2d\u53ef\u7528\u3002\u4e3a\u4e86\u907f\u514d\u7248\u672c\u51b2\u7a81\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u9694\u79bb\u9879\u76ee\u4f9d\u8d56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728VB\u4e2d\u4f7f\u7528Python\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528Python\u811a\u672c\u3001\u4f7f\u7528COM\u63a5\u53e3\u3001\u4f7f\u7528IronPython\u3001\u5d4c\u5165Pytho [&hellip;]","protected":false},"author":3,"featured_media":1183223,"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\/1183214"}],"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=1183214"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183214\/revisions"}],"predecessor-version":[{"id":1183225,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183214\/revisions\/1183225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183223"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}