{"id":932803,"date":"2024-12-26T18:05:09","date_gmt":"2024-12-26T10:05:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932803.html"},"modified":"2024-12-26T18:05:11","modified_gmt":"2024-12-26T10:05:11","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8vba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932803.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528vba"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070512\/3ee4fe91-5862-4317-a4fb-8d9237586053.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528vba\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528VBA\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c<strong>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>win32com.client<\/code>\u5e93\u3001\u901a\u8fc7VBA\u5b8f\u548c\u811a\u672c\u3001\u4f7f\u7528Python\u4e0eExcel\u4ea4\u4e92\u3001\u5229\u7528VBA\u4e0ePython\u7684\u7ec4\u5408<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7<code>win32com.client<\/code>\u5e93\u4e0eExcel\u8fdb\u884c\u81ea\u52a8\u5316\u64cd\u4f5c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u8c03\u7528VBA\u5b8f\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528<code>win32com.client<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><p><code>win32com.client<\/code>\u5e93\u662fPython\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8Python\u4e0eWindows\u5e94\u7528\u7a0b\u5e8f\u8fdb\u884c\u901a\u4fe1\u3002\u901a\u8fc7\u8be5\u5e93\uff0c\u4f60\u53ef\u4ee5\u521b\u5efaCOM\u5bf9\u8c61\u5e76\u4e0e\u5176\u4ea4\u4e92\u3002\u5728\u4e0eExcel\u7684\u4ea4\u4e92\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8be5\u5e93\u8c03\u7528VBA\u5b8f\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e00\u3001\u5b89\u88c5win32com.client<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>win32com.client<\/code>\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pywin32<\/code>\u5e93\uff0c\u8fd9\u662fPython\u7684Windows\u6269\u5c55\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>pip<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165<code>win32com.client<\/code>\u6a21\u5757\uff0c\u5e76\u5f00\u59cb\u4e0eExcel\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u4e8c\u3001\u4f7f\u7528win32com.client\u8c03\u7528VBA\u5b8f<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaExcel\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2aExcel\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7<code>win32com.client<\/code>\u5e93\u4e2d\u7684<code>Dispatch<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32com.client<\/p>\n<p>excel = win32com.client.Dispatch(&quot;Excel.Application&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u542f\u52a8Excel\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2aExcel\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6253\u5f00Excel\u5de5\u4f5c\u7c3f<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u6253\u5f00\u5305\u542bVBA\u5b8f\u7684Excel\u5de5\u4f5c\u7c3f\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Workbooks.Open<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook = excel.Workbooks.Open(r&quot;Path\\To\\Your\\Workbook.xlsm&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u4f60\u9700\u8981\u6307\u5b9aExcel\u5de5\u4f5c\u7c3f\u7684\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u7528VBA\u5b8f<\/strong><\/p>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u8c03\u7528VBA\u5b8f\u3002\u53ef\u4ee5\u4f7f\u7528<code>Application.Run<\/code>\u65b9\u6cd5\u6765\u6267\u884c\u5b8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">excel.Application.Run(&quot;YourMacroName&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u786e\u4fddVBA\u5b8f\u7684\u540d\u79f0\u6b63\u786e\uff0c\u5e76\u4e14\u5b8f\u5b58\u5728\u4e8e\u6253\u5f00\u7684\u5de5\u4f5c\u7c3f\u4e2d\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5173\u95ed\u5de5\u4f5c\u7c3f\u548cExcel\u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<\/p>\n<p><p>\u5b8c\u6210\u5b8f\u7684\u8c03\u7528\u540e\uff0c\u4f60\u53ef\u4ee5\u5173\u95ed\u5de5\u4f5c\u7c3f\u548cExcel\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook.Close(SaveChanges=0)<\/p>\n<p>excel.Application.Quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5173\u95ed\u5de5\u4f5c\u7c3f\uff0c\u5e76\u9000\u51faExcel\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e09\u3001\u5728VBA\u4e2d\u4e0ePython\u811a\u672c\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5728Python\u4e2d\u8c03\u7528VBA\u5b8f\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5728VBA\u4e2d\u8c03\u7528Python\u811a\u672c\u3002\u901a\u8fc7Shell\u547d\u4ee4\uff0c\u53ef\u4ee5\u5728VBA\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaVBA\u5b8f<\/strong><\/p>\n<\/p>\n<p><p>\u5728Excel\u4e2d\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684VBA\u5b8f\u3002\u5728VBA\u7f16\u8f91\u5668\u4e2d\uff0c\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u4ee5\u8c03\u7528Python\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-vba\">Sub RunPythonScript()<\/p>\n<p>    Dim objShell As Object<\/p>\n<p>    Set objShell = VBA.CreateObject(&quot;WScript.Shell&quot;)<\/p>\n<p>    objShell.Run &quot;python Path\\To\\Your\\Script.py&quot;<\/p>\n<p>End Sub<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u786e\u4fddPython\u7684\u5b89\u88c5\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>\u6267\u884cVBA\u5b8f<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728Excel\u4e2d\u6267\u884c\u8be5\u5b8f\uff0c\u4ece\u800c\u8fd0\u884c\u6307\u5b9a\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u56db\u3001\u7ed3\u5408\u4f7f\u7528Python\u548cVBA\u8fdb\u884c\u590d\u6742\u4efb\u52a1<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528Python\u548cVBA\uff0c\u4f60\u53ef\u4ee5\u5728Excel\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u81ea\u52a8\u5316\u4efb\u52a1\u3002Python\u64c5\u957f\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u800cVBA\u64c5\u957f\u4e0eExcel\u7684\u4ea4\u4e92\u3002\u901a\u8fc7\u4e24\u8005\u7684\u7ed3\u5408\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u4ee5\u4e0b\u5e94\u7528\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u590d\u6742\u7684\u8ba1\u7b97\uff0c\u5c06\u7ed3\u679c\u4f20\u9012\u7ed9Excel\u8fdb\u884c\u5c55\u793a\u3002Python\u53ef\u4ee5\u5904\u7406\u5927\u6570\u636e\u96c6\uff0c\u800cVBA\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u62a5\u544a\u548c\u56fe\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u52a8\u5316\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528VBA\u63a7\u5236Excel\u7684\u754c\u9762\u548c\u7528\u6237\u4ea4\u4e92\uff0c\u4f7f\u7528Python\u8fdb\u884c\u540e\u53f0\u6570\u636e\u5904\u7406\u548c\u81ea\u52a8\u5316\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f7f\u7528VBA\u6536\u96c6\u7528\u6237\u8f93\u5165\uff0c\u901a\u8fc7Python\u5904\u7406\u6570\u636e\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5728Excel\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7VBA\u521b\u5efaUDF\uff08\u7528\u6237\u5b9a\u4e49\u51fd\u6570\uff09\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528Python\u6765\u5b9e\u73b0\u590d\u6742\u7684\u51fd\u6570\u903b\u8f91\uff0c\u5e76\u901a\u8fc7VBA\u8c03\u7528\u5b83\u4eec\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<hr>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e0eVBA\u7684\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5145\u5206\u5229\u7528\u4e24\u8005\u7684\u4f18\u52bf\uff0c\u5b9e\u73b0Excel\u4e2d\u7684\u81ea\u52a8\u5316\u548c\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u901a\u8fc7<code>win32com.client<\/code>\u5e93\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8c03\u7528VBA\u5b8f\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u64cd\u4f5c\u3002\u53cd\u4e4b\uff0c\u901a\u8fc7VBA\u4e2d\u7684Shell\u547d\u4ee4\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728VBA\u4e2d\u8fd0\u884cPython\u811a\u672c\uff0c\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e24\u79cd\u6280\u672f\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u548c\u6570\u636e\u5904\u7406\u80fd\u529b\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 \/>\u5728VBA\u4e2d\u8c03\u7528Python\u811a\u672c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Shell\u547d\u4ee4\u6765\u5b9e\u73b0\u3002\u60a8\u9700\u8981\u786e\u4fddPython\u5df2\u5b89\u88c5\u5728\u60a8\u7684\u8ba1\u7b97\u673a\u4e0a\uff0c\u5e76\u4e14\u5df2\u5c06Python\u7684\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5728VBA\u4e2d\u8fd0\u884cPython\u811a\u672c\uff1a<\/p>\n<pre><code class=\"language-vba\">Sub RunPythonScript()\n    Dim objShell As Object\n    Set objShell = CreateObject(&quot;WScript.Shell&quot;)\n    objShell.Run &quot;python C:\\path\\to\\your\\script.py&quot;\nEnd Sub\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06\u8def\u5f84\u66ff\u6362\u4e3a\u60a8\u5b9e\u9645\u7684Python\u811a\u672c\u8def\u5f84\u3002<\/p>\n<p><strong>Python\u4e0eVBA\u7684\u96c6\u6210\u6709\u4ec0\u4e48\u4f18\u70b9\uff1f<\/strong><br \/>\u5c06Python\u4e0eVBA\u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u8ba9\u60a8\u5145\u5206\u5229\u7528Python\u7684\u5f3a\u5927\u5e93\u548c\u6570\u636e\u5904\u7406\u80fd\u529b\uff0c\u540c\u65f6\u4fdd\u7559VBA\u5728Excel\u7b49Microsoft\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u4fbf\u6377\u6027\u3002\u8fd9\u79cd\u96c6\u6210\u53ef\u4ee5\u8ba9\u60a8\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u548c\u81ea\u52a8\u5316\u64cd\u4f5c\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>\u5728VBA\u4e2d\u5982\u4f55\u4f20\u9012\u53c2\u6570\u7ed9Python\u811a\u672c\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728Shell\u547d\u4ee4\u4e2d\u6dfb\u52a0\u53c2\u6570\u6765\u4f20\u9012\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684Python\u811a\u672c\u9700\u8981\u63a5\u6536\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-vba\">Sub RunPythonScriptWithArgs()\n    Dim objShell As Object\n    Dim filePath As String\n    filePath = &quot;C:\\path\\to\\your\\datafile.txt&quot;\n    Set objShell = CreateObject(&quot;WScript.Shell&quot;)\n    objShell.Run &quot;python C:\\path\\to\\your\\script.py &quot; &amp; filePath\nEnd Sub\n<\/code><\/pre>\n<p>\u5728Python\u811a\u672c\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528sys\u6a21\u5757\u6765\u83b7\u53d6\u4f20\u9012\u7684\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528VBA\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528win32com.client\u5e93\u3001\u901a\u8fc7VBA\u5b8f\u548c\u811a [&hellip;]","protected":false},"author":3,"featured_media":932805,"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\/932803"}],"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=932803"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932803\/revisions"}],"predecessor-version":[{"id":932806,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932803\/revisions\/932806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932805"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}