{"id":1024837,"date":"2024-12-30T14:17:11","date_gmt":"2024-12-30T06:17:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024837.html"},"modified":"2024-12-30T14:17:13","modified_gmt":"2024-12-30T06:17:13","slug":"vba%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8python%e7%a8%8b%e5%ba%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024837.html","title":{"rendered":"vba\u5982\u4f55\u8c03\u7528python\u7a0b\u5e8f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/24392ec2-2203-4ba3-8e77-7f23af8735e1.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"vba\u5982\u4f55\u8c03\u7528python\u7a0b\u5e8f\" \/><\/p>\n<p><p> <strong>VBA\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u6765\u6267\u884cPython\u811a\u672c<\/strong>\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709<strong>Shell\u51fd\u6570\u3001CreateObject\u51fd\u6570\u3001\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c<strong>Shell\u51fd\u6570<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u6613\u7528\u4e14\u9002\u7528\u4e8e\u5927\u90e8\u5206\u9700\u6c42\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7VBA\u8c03\u7528Python\u7a0b\u5e8f\uff0c\u5e76\u4e3e\u4f8b\u8bf4\u660e\u6bcf\u79cd\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001Shell\u51fd\u6570\u8c03\u7528Python\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Shell\u51fd\u6570\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Shell\u51fd\u6570\u7528\u4e8e\u5728Microsoft Windows\u4e2d\u542f\u52a8\u5176\u4ed6\u7a0b\u5e8f\u3002\u901a\u8fc7Shell\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u6765\u6267\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScript()<\/p>\n<p>    Dim pythonPath As String<\/p>\n<p>    Dim scriptPath As String<\/p>\n<p>    Dim shellCommand As String<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonPath = &quot;C:\\Python39\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u8981\u6267\u884c\u7684Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    scriptPath = &quot;C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    &#39; \u6784\u5efaShell\u547d\u4ee4<\/p>\n<p>    shellCommand = pythonPath &amp; &quot; &quot; &amp; scriptPath<\/p>\n<p>    &#39; \u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884cPython\u811a\u672c<\/p>\n<p>    Shell shellCommand, vbNormalFocus<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8bbe\u7f6e\u4e86Python\u89e3\u91ca\u5668\u548cPython\u811a\u672c\u7684\u8def\u5f84\uff0c\u7136\u540e\u6784\u5efaShell\u547d\u4ee4\u5e76\u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884cPython\u811a\u672c\u3002<strong>\u6ce8\u610f<\/strong>\uff0c\u8def\u5f84\u4e2d\u5305\u542b\u7a7a\u683c\u65f6\u9700\u8981\u4f7f\u7528\u53cc\u5f15\u53f7\u62ec\u8d77\u6765\uff0c\u4ee5\u907f\u514d\u8def\u5f84\u89e3\u6790\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001CreateObject\u51fd\u6570\u8c03\u7528Python\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>CreateObject\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5cAutomation\u5bf9\u8c61\u3002\u901a\u8fc7CreateObject\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528Windows\u811a\u672c\u5bbf\u4e3b\uff08WSH\uff09\u6765\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScriptWithWSH()<\/p>\n<p>    Dim shell As Object<\/p>\n<p>    Dim pythonPath As String<\/p>\n<p>    Dim scriptPath As String<\/p>\n<p>    Dim shellCommand As String<\/p>\n<p>    &#39; \u521b\u5efaWindows\u811a\u672c\u5bbf\u4e3b\u5bf9\u8c61<\/p>\n<p>    Set shell = CreateObject(&quot;WScript.Shell&quot;)<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonPath = &quot;C:\\Python39\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u8981\u6267\u884c\u7684Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    scriptPath = &quot;C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    &#39; \u6784\u5efaShell\u547d\u4ee4<\/p>\n<p>    shellCommand = pythonPath &amp; &quot; &quot; &amp; scriptPath<\/p>\n<p>    &#39; \u4f7f\u7528Windows\u811a\u672c\u5bbf\u4e3b\u8fd0\u884cPython\u811a\u672c<\/p>\n<p>    shell.Run shellCommand, 1, True<\/p>\n<p>    &#39; \u91ca\u653e\u5bf9\u8c61<\/p>\n<p>    Set shell = Nothing<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7CreateObject\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2aWindows\u811a\u672c\u5bbf\u4e3b\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528shell.Run\u65b9\u6cd5\u6765\u8fd0\u884cPython\u811a\u672c\u3002<strong>shell.Run<\/strong>\u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3aTrue\u65f6\u8868\u793a\u7b49\u5f85Python\u811a\u672c\u6267\u884c\u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u6267\u884cVBA\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6279\u5904\u7406\u6587\u4ef6\u8c03\u7528Python\u7a0b\u5e8f<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4e3a\u4e86\u7b80\u5316\u8c03\u7528\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5148\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\u6765\u8fd0\u884cPython\u811a\u672c\uff0c\u7136\u540e\u5728VBA\u4e2d\u8c03\u7528\u8fd9\u4e2a\u6279\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u6279\u5904\u7406\u6587\u4ef6\uff08\u4f8b\u5982run_python.bat\uff09\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-batch\">@echo off<\/p>\n<p>C:\\Python39\\python.exe C:\\path\\to\\your\\script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5728VBA\u4e2d\u8c03\u7528\u8fd9\u4e2a\u6279\u5904\u7406\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-vba\">Sub RunPythonScriptWithBatch()<\/p>\n<p>    Dim batchFilePath As String<\/p>\n<p>    &#39; \u8bbe\u7f6e\u6279\u5904\u7406\u6587\u4ef6\u7684\u8def\u5f84<\/p>\n<p>    batchFilePath = &quot;C:\\path\\to\\your\\run_python.bat&quot;<\/p>\n<p>    &#39; \u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884c\u6279\u5904\u7406\u6587\u4ef6<\/p>\n<p>    Shell batchFilePath, vbNormalFocus<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6240\u6709Python\u811a\u672c\u76f8\u5173\u7684\u8def\u5f84\u548c\u53c2\u6570\u914d\u7f6e\u90fd\u653e\u5728\u6279\u5904\u7406\u6587\u4ef6\u4e2d\uff0c\u7b80\u5316\u4e86VBA\u4ee3\u7801\u4e2d\u7684\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001Python\u811a\u672c\u4e0eVBA\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5c06Python\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u8fd4\u56de\u7ed9VBA\u7a0b\u5e8f\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u6587\u4ef6\u3001\u526a\u8d34\u677f\u6216\u6807\u51c6\u8f93\u51fa\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528\u6587\u4ef6\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5c06\u7ed3\u679c\u5199\u5165\u4e00\u4e2a\u6587\u4ef6\uff0c\u7136\u540e\u5728VBA\u4e2d\u8bfb\u53d6\u8be5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>Python\u811a\u672c\uff08script.py\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># script.py<\/p>\n<p>result = &quot;Hello from Python!&quot;<\/p>\n<p>with open(&quot;C:\\\\path\\\\to\\\\result.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>VBA\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScriptAndReadResult()<\/p>\n<p>    Dim pythonPath As String<\/p>\n<p>    Dim scriptPath As String<\/p>\n<p>    Dim shellCommand As String<\/p>\n<p>    Dim resultFilePath As String<\/p>\n<p>    Dim fileContent As String<\/p>\n<p>    Dim fileNumber As Integer<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonPath = &quot;C:\\Python39\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u8981\u6267\u884c\u7684Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    scriptPath = &quot;C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    &#39; \u6784\u5efaShell\u547d\u4ee4<\/p>\n<p>    shellCommand = pythonPath &amp; &quot; &quot; &amp; scriptPath<\/p>\n<p>    &#39; \u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884cPython\u811a\u672c<\/p>\n<p>    Shell shellCommand, vbNormalFocus<\/p>\n<p>    &#39; \u8bbe\u7f6e\u7ed3\u679c\u6587\u4ef6\u7684\u8def\u5f84<\/p>\n<p>    resultFilePath = &quot;C:\\path\\to\\result.txt&quot;<\/p>\n<p>    &#39; \u7b49\u5f85Python\u811a\u672c\u6267\u884c\u5b8c\u6210\u5e76\u751f\u6210\u7ed3\u679c\u6587\u4ef6<\/p>\n<p>    Application.W<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t (Now + TimeValue(&quot;0:00:05&quot;))<\/p>\n<p>    &#39; \u8bfb\u53d6\u7ed3\u679c\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    fileNumber = FreeFile<\/p>\n<p>    Open resultFilePath For Input As #fileNumber<\/p>\n<p>    Line Input #fileNumber, fileContent<\/p>\n<p>    Close #fileNumber<\/p>\n<p>    &#39; \u663e\u793a\u7ed3\u679c<\/p>\n<p>    MsgBox fileContent<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528\u526a\u8d34\u677f\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5c06\u7ed3\u679c\u590d\u5236\u5230\u526a\u8d34\u677f\uff0c\u7136\u540e\u5728VBA\u4e2d\u8bfb\u53d6\u526a\u8d34\u677f\u5185\u5bb9\u3002\u9700\u8981\u5b89\u88c5pyperclip\u5e93\u6765\u64cd\u4f5c\u526a\u8d34\u677f\u3002<\/p>\n<\/p>\n<p><p>Python\u811a\u672c\uff08script.py\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># script.py<\/p>\n<p>import pyperclip<\/p>\n<p>result = &quot;Hello from Python!&quot;<\/p>\n<p>pyperclip.copy(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>VBA\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScriptAndReadClipboard()<\/p>\n<p>    Dim pythonPath As String<\/p>\n<p>    Dim scriptPath As String<\/p>\n<p>    Dim shellCommand As String<\/p>\n<p>    Dim clipboardContent As String<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonPath = &quot;C:\\Python39\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u8981\u6267\u884c\u7684Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    scriptPath = &quot;C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    &#39; \u6784\u5efaShell\u547d\u4ee4<\/p>\n<p>    shellCommand = pythonPath &amp; &quot; &quot; &amp; scriptPath<\/p>\n<p>    &#39; \u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884cPython\u811a\u672c<\/p>\n<p>    Shell shellCommand, vbNormalFocus<\/p>\n<p>    &#39; \u7b49\u5f85Python\u811a\u672c\u6267\u884c\u5b8c\u6210\u5e76\u590d\u5236\u7ed3\u679c\u5230\u526a\u8d34\u677f<\/p>\n<p>    Application.Wait (Now + TimeValue(&quot;0:00:05&quot;))<\/p>\n<p>    &#39; \u8bfb\u53d6\u526a\u8d34\u677f\u5185\u5bb9<\/p>\n<p>    clipboardContent = GetClipboardText()<\/p>\n<p>    &#39; \u663e\u793a\u7ed3\u679c<\/p>\n<p>    MsgBox clipboardContent<\/p>\n<p>End Sub<\/p>\n<p>Function GetClipboardText() As String<\/p>\n<p>    Dim DataObj As MSForms.DataObject<\/p>\n<p>    Set DataObj = New MSForms.DataObject<\/p>\n<p>    On Error GoTo ErrHandler<\/p>\n<p>    DataObj.GetFromClipboard<\/p>\n<p>    GetClipboardText = DataObj.GetText(1)<\/p>\n<p>    Exit Function<\/p>\n<p>ErrHandler:<\/p>\n<p>    GetClipboardText = &quot;&quot;<\/p>\n<p>End Function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u6807\u51c6\u8f93\u51fa\u8fdb\u884c\u6570\u636e\u4ea4\u6362<\/li>\n<\/ol>\n<p><p>\u5728Python\u811a\u672c\u4e2d\u5c06\u7ed3\u679c\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\uff0c\u7136\u540e\u5728VBA\u4e2d\u901a\u8fc7Shell\u51fd\u6570\u7684\u91cd\u5b9a\u5411\u8bfb\u53d6\u6807\u51c6\u8f93\u51fa\u5185\u5bb9\u3002\u5b9e\u73b0\u8fd9\u4e00\u70b9\u9700\u8981\u4f7f\u7528Windows\u547d\u4ee4\u884c\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>Python\u811a\u672c\uff08script.py\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># script.py<\/p>\n<p>result = &quot;Hello from Python!&quot;<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>VBA\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScriptAndReadStdout()<\/p>\n<p>    Dim pythonPath As String<\/p>\n<p>    Dim scriptPath As String<\/p>\n<p>    Dim shellCommand As String<\/p>\n<p>    Dim resultFilePath As String<\/p>\n<p>    Dim fileContent As String<\/p>\n<p>    Dim fileNumber As Integer<\/p>\n<p>    &#39; \u8bbe\u7f6ePython\u89e3\u91ca\u5668\u7684\u8def\u5f84<\/p>\n<p>    pythonPath = &quot;C:\\Python39\\python.exe&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u8981\u6267\u884c\u7684Python\u811a\u672c\u7684\u8def\u5f84<\/p>\n<p>    scriptPath = &quot;C:\\path\\to\\your\\script.py&quot;<\/p>\n<p>    &#39; \u8bbe\u7f6e\u7ed3\u679c\u6587\u4ef6\u7684\u8def\u5f84<\/p>\n<p>    resultFilePath = &quot;C:\\path\\to\\result.txt&quot;<\/p>\n<p>    &#39; \u6784\u5efaShell\u547d\u4ee4\uff0c\u5e76\u5c06\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u7ed3\u679c\u6587\u4ef6<\/p>\n<p>    shellCommand = pythonPath &amp; &quot; &quot; &amp; scriptPath &amp; &quot; &gt; &quot; &amp; resultFilePath<\/p>\n<p>    &#39; \u4f7f\u7528Shell\u51fd\u6570\u8fd0\u884cPython\u811a\u672c<\/p>\n<p>    Shell shellCommand, vbNormalFocus<\/p>\n<p>    &#39; \u7b49\u5f85Python\u811a\u672c\u6267\u884c\u5b8c\u6210\u5e76\u751f\u6210\u7ed3\u679c\u6587\u4ef6<\/p>\n<p>    Application.Wait (Now + TimeValue(&quot;0:00:05&quot;))<\/p>\n<p>    &#39; \u8bfb\u53d6\u7ed3\u679c\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    fileNumber = FreeFile<\/p>\n<p>    Open resultFilePath For Input As #fileNumber<\/p>\n<p>    Line Input #fileNumber, fileContent<\/p>\n<p>    Close #fileNumber<\/p>\n<p>    &#39; \u663e\u793a\u7ed3\u679c<\/p>\n<p>    MsgBox fileContent<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0VBA\u4e0ePython\u811a\u672c\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7684\u4ea4\u4e92\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\u7684\u51e0\u79cd\u65b9\u6cd5\uff0cVBA\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u7528Python\u7a0b\u5e8f\u5e76\u5b9e\u73b0\u4e0ePython\u811a\u672c\u7684\u4ea4\u4e92\u3002\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684Shell\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u662f\u901a\u8fc7\u6587\u4ef6\u3001\u526a\u8d34\u677f\u6216\u6807\u51c6\u8f93\u51fa\u8fdb\u884c\u6570\u636e\u4ea4\u6362\uff0c\u90fd\u53ef\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728VBA\u4e2d\u8c03\u7528Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728VBA\u4e2d\u8c03\u7528Python\u7a0b\u5e8f\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u901a\u8fc7Shell\u547d\u4ee4\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2aVBA\u5b8f\uff0c\u4f7f\u7528Shell\u51fd\u6570\u6765\u6267\u884cPython\u811a\u672c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u7f16\u5199\u5982\u4e0b\u4ee3\u7801\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>\u786e\u4fdd\u5c06\u8def\u5f84\u66ff\u6362\u4e3a\u60a8\u7684Python\u89e3\u91ca\u5668\u548c\u811a\u672c\u7684\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728VBA\u4e2d\u5982\u4f55\u5904\u7406Python\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u83b7\u53d6Python\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u8f93\u51fa\u5199\u5165\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u6216Excel\u5355\u5143\u683c\u3002\u5728Python\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5199\u5165\u529f\u80fd\u6765\u5c06\u7ed3\u679c\u4fdd\u5b58\u3002\u7136\u540e\u5728VBA\u4e2d\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\u6216\u76f4\u63a5\u4eceExcel\u4e2d\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u662f\u5426\u9700\u8981\u5b89\u88c5\u7279\u5b9a\u7684\u5e93\u624d\u80fd\u5728VBA\u4e2d\u8c03\u7528Python\uff1f<\/strong><br \/>\u5728VBA\u4e2d\u8c03\u7528Python\u7a0b\u5e8f\u672c\u8eab\u4e0d\u9700\u8981\u989d\u5916\u7684\u5e93\uff0c\u4f46\u4e3a\u4e86\u8ba9Python\u80fd\u591f\u6267\u884c\u7279\u5b9a\u7684\u4efb\u52a1\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684Python\u5e93\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0c\u786e\u4fdd\u5728Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u60a8\u7684Python\u73af\u5883\u5df2\u6b63\u786e\u914d\u7f6e\u5e76\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u4ee5\u4fbf\u5728Shell\u547d\u4ee4\u4e2d\u8c03\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"VBA\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u5916\u90e8\u7a0b\u5e8f\u6765\u6267\u884cPython\u811a\u672c\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709Shell\u51fd\u6570\u3001CreateObject\u51fd\u6570\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1024841,"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\/1024837"}],"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=1024837"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024837\/revisions"}],"predecessor-version":[{"id":1024846,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024837\/revisions\/1024846"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024841"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}