{"id":1064618,"date":"2024-12-31T16:10:39","date_gmt":"2024-12-31T08:10:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1064618.html"},"modified":"2024-12-31T16:10:41","modified_gmt":"2024-12-31T08:10:41","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%96%87%e4%bb%b6%e5%a4%b9%e8%b0%83%e7%94%a8excel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1064618.html","title":{"rendered":"python\u5982\u4f55\u5728\u6587\u4ef6\u5939\u8c03\u7528excel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1395d114-522e-4c66-8364-6084bedd852a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5728\u6587\u4ef6\u5939\u8c03\u7528excel\" \/><\/p>\n<p><p> <strong>Python \u5982\u4f55\u5728\u6587\u4ef6\u5939\u8c03\u7528 Excel<\/strong><\/p>\n<\/p>\n<p><p><strong>Python \u8c03\u7528 Excel \u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e93\u5982 <code>openpyxl<\/code>\u3001<code>pandas<\/code>\u3001<code>xlrd<\/code>\u3001<code>xlwt<\/code>\u3001<code>xlutils<\/code> \u548c <code>pywin32<\/code> \u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>pandas<\/code> \u548c <code>openpyxl<\/code> \u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u8bfb\u53d6\u3001\u5199\u5165\u548c\u64cd\u4f5c Excel \u6587\u4ef6\u3002\u8981\u5728\u6587\u4ef6\u5939\u4e2d\u8c03\u7528 Excel \u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u7279\u5b9a\u8def\u5f84\u4e0b\u7684\u6587\u4ef6\uff0c\u5e76\u5229\u7528\u8fd9\u4e9b\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>1\u3001\u5b89\u88c5\u76f8\u5173\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u6240\u9700\u7684\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5 <code>pandas<\/code> \u548c <code>openpyxl<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2\u3001\u8bfb\u53d6 Excel \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528 <code>pandas<\/code> \u8bfb\u53d6 Excel \u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u8bfb\u53d6\u6587\u4ef6\u5939\u4e2d\u7684 Excel \u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u52a0\u8f7d\u5230 DataFrame \u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &#39;path\/to\/your\/excel\/file.xlsx&#39;<\/p>\n<p>df = pd.read_excel(file_path)<\/p>\n<h2><strong>\u663e\u793a DataFrame \u7684\u524d\u51e0\u884c<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3\u3001\u5904\u7406 Excel \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u8bfb\u53d6 Excel \u6587\u4ef6\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u7b5b\u9009\u6570\u636e\u3001\u4fee\u6539\u6570\u636e\u3001\u6dfb\u52a0\u65b0\u6570\u636e\u7b49\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u7b5b\u9009\u51fa\u7279\u5b9a\u5217\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u51fa\u7279\u5b9a\u5217\u7684\u6570\u636e<\/p>\n<p>filtered_df = df[[&#39;Column1&#39;, &#39;Column2&#39;]]<\/p>\n<p>print(filtered_df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4\u3001\u5199\u5165 Excel \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u5904\u7406\u5b8c\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed3\u679c\u5199\u56de\u5230 Excel \u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u5c06 DataFrame \u5199\u5165\u65b0\u7684 Excel \u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u65b0\u7684 Excel \u6587\u4ef6<\/p>\n<p>output_file_path = &#39;path\/to\/your\/output\/file.xlsx&#39;<\/p>\n<p>df.to_excel(output_file_path, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5\u3001\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u591a\u4e2a Excel \u6587\u4ef6<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5904\u7406\u6587\u4ef6\u5939\u4e2d\u6240\u6709\u7684 Excel \u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528 <code>os<\/code> \u6a21\u5757\u6765\u904d\u5386\u6587\u4ef6\u5939\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u6587\u4ef6\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>folder_path = &#39;path\/to\/your\/folder&#39;<\/p>\n<p>for file_name in os.listdir(folder_path):<\/p>\n<p>    if file_name.endswith(&#39;.xlsx&#39;):<\/p>\n<p>        file_path = os.path.join(folder_path, file_name)<\/p>\n<p>        df = pd.read_excel(file_path)<\/p>\n<p>        # \u5bf9 DataFrame \u8fdb\u884c\u5904\u7406<\/p>\n<p>        print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>6\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u6587\u4ef6\u5939\u4e2d\u8c03\u7528\u5e76\u5904\u7406 Excel \u6587\u4ef6\u3002Python \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5e93\u548c\u5de5\u5177\uff0c\u4f7f\u5f97\u5904\u7406 Excel \u6587\u4ef6\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u9ad8\u6548\u3002<strong>\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u8bfb\u53d6 Excel \u6587\u4ef6\u3001\u5904\u7406\u6570\u636e\u3001\u5199\u5165 Excel \u6587\u4ef6\u3001\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u591a\u4e2a Excel \u6587\u4ef6<\/strong>\uff0c\u8fd9\u4e9b\u6b65\u9aa4\u6784\u6210\u4e86\u5b8c\u6574\u7684\u5de5\u4f5c\u6d41\u7a0b\uff0c\u5e2e\u52a9\u6211\u4eec\u9ad8\u6548\u5730\u5904\u7406 Excel \u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6Excel\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u5e93\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install pandas openpyxl\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u8bfb\u53d6Excel\u6587\u4ef6\ndf = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;)\nprint(df)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5c06<code>\u6587\u4ef6\u8def\u5f84.xlsx<\/code>\u66ff\u6362\u4e3a\u4f60\u7684Excel\u6587\u4ef6\u7684\u5b9e\u9645\u8def\u5f84\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\uff0c\u4f60\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2aDataFrame\ndata = {&#39;\u5217\u540d1&#39;: [1, 2, 3], &#39;\u5217\u540d2&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]}\ndf = pd.DataFrame(data)\n\n# \u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6\ndf.to_excel(&#39;\u8f93\u51fa\u6587\u4ef6\u8def\u5f84.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>\u8f93\u51fa\u6587\u4ef6\u8def\u5f84.xlsx<\/code>\u662f\u4f60\u5e0c\u671b\u4fdd\u5b58Excel\u6587\u4ef6\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u7ed3\u5408<code>pandas<\/code>\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport pandas as pd\n\n# \u8bbe\u7f6e\u8981\u904d\u5386\u7684\u6587\u4ef6\u5939\u8def\u5f84\nfolder_path = &#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;\n\n# \u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\nfor filename in os.listdir(folder_path):\n    if filename.endswith(&#39;.xlsx&#39;):\n        file_path = os.path.join(folder_path, filename)\n        df = pd.read_excel(file_path)\n        print(f&#39;\u5185\u5bb9\u6765\u81ea {filename}:&#39;)\n        print(df)\n<\/code><\/pre>\n<p>\u5c06<code>\u6587\u4ef6\u5939\u8def\u5f84<\/code>\u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u6587\u4ef6\u5939\u8def\u5f84\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u6bcf\u4e2aExcel\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5982\u4f55\u5728\u6587\u4ef6\u5939\u8c03\u7528 Excel Python \u8c03\u7528 Excel \u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5e93\u5982 op [&hellip;]","protected":false},"author":3,"featured_media":1064627,"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\/1064618"}],"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=1064618"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064618\/revisions"}],"predecessor-version":[{"id":1064630,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1064618\/revisions\/1064630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1064627"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1064618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1064618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1064618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}