{"id":1025763,"date":"2024-12-31T10:36:50","date_gmt":"2024-12-31T02:36:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025763.html"},"modified":"2024-12-31T10:36:54","modified_gmt":"2024-12-31T02:36:54","slug":"python%e5%a6%82%e4%bd%95excel%e5%88%a0%e9%99%a4%e7%ac%ac%e4%b8%80%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025763.html","title":{"rendered":"python\u5982\u4f55excel\u5220\u9664\u7b2c\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/99f11081-d50a-41f2-a4ec-059947f1fbf4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55excel\u5220\u9664\u7b2c\u4e00\u884c\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\uff1apandas\u5e93\u3001openpyxl\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528pandas\u5e93\u6216\u8005openpyxl\u5e93\u5b9e\u73b0\u3002<strong>pandas\u5e93\u3001openpyxl\u5e93<\/strong>\u662f\u4e24\u4e2a\u5e38\u7528\u7684\u5904\u7406Excel\u6587\u4ef6\u7684\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e24\u4e2a\u5e93\u6765\u5220\u9664Excel\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\u548c\u64cd\u4f5c\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u3001\u4fee\u6539\u548c\u4fdd\u5b58Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528pandas\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528pandas\u5220\u9664Excel\u7b2c\u4e00\u884c<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;example.xlsx&#39;)<\/p>\n<h2><strong>\u5220\u9664\u7b2c\u4e00\u884c<\/strong><\/h2>\n<p>df = df.iloc[1:]<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6570\u636e\u5230\u65b0\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;example_modified.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>pd.read_excel<\/code>\u51fd\u6570\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>df.iloc[1:]<\/code>\u5220\u9664\u7b2c\u4e00\u884c\uff0c\u6700\u540e\u4f7f\u7528<code>df.to_excel<\/code>\u51fd\u6570\u5c06\u4fee\u6539\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><strong>df.iloc[1:]<\/strong>\u7684\u4f5c\u7528\u662f\u901a\u8fc7\u884c\u7d22\u5f15\u6765\u9009\u62e9\u6570\u636e\uff0c<code>iloc<\/code>\u51fd\u6570\u662f\u57fa\u4e8e\u4f4d\u7f6e\u9009\u62e9\u6570\u636e\u7684\uff0c<code>1:<\/code>\u8868\u793a\u4ece\u7b2c\u4e8c\u884c\u5f00\u59cb\u9009\u62e9\uff0c\u5ffd\u7565\u7b2c\u4e00\u884c\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\u548c\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528openpyxl\u5e93<\/h3>\n<\/p>\n<p><p>openpyxl\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u5904\u7406Excel\u6587\u4ef6\u7684\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u66f4\u5e95\u5c42\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5openpyxl\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528openpyxl\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86openpyxl\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528openpyxl\u5220\u9664Excel\u7b2c\u4e00\u884c<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528openpyxl\u5e93\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = load_workbook(&#39;example.xlsx&#39;)<\/p>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5220\u9664\u7b2c\u4e00\u884c<\/strong><\/h2>\n<p>ws.delete_rows(1)<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6570\u636e\u5230\u65b0\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;example_modified.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528<code>load_workbook<\/code>\u51fd\u6570\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u7136\u540e\u901a\u8fc7<code>ws.delete_rows(1)<\/code>\u5220\u9664\u7b2c\u4e00\u884c\uff0c\u6700\u540e\u4f7f\u7528<code>wb.save<\/code>\u51fd\u6570\u5c06\u4fee\u6539\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u8be6\u7ec6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p><strong>ws.delete_rows(1)<\/strong>\u7684\u4f5c\u7528\u662f\u5220\u9664\u5de5\u4f5c\u8868\u4e2d\u7684\u7b2c\u4e00\u884c\uff0c<code>delete_rows<\/code>\u51fd\u6570\u662fopenpyxl\u5e93\u63d0\u4f9b\u7684\u64cd\u4f5c\u5de5\u4f5c\u8868\u884c\u7684\u51fd\u6570\u3002\u4e0epandas\u4e0d\u540c\uff0copenpyxl\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5cExcel\u6587\u4ef6\u7684\u7ed3\u6784\uff0c\u8fd9\u4f7f\u5f97\u5b83\u66f4\u9002\u5408\u4e8e\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5bf9\u6bd4\u5206\u6790<\/h3>\n<\/p>\n<p><p><strong>pandas\u5e93<\/strong>\u548c<strong>openpyxl\u5e93<\/strong>\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u54ea\u4e00\u4e2a\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>pandas\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f18\u70b9\uff1a\u64cd\u4f5c\u7b80\u4fbf\uff0c\u652f\u6301\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u9002\u5408\u5927\u591a\u6570\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u5bf9Excel\u6587\u4ef6\u7684\u5e95\u5c42\u64cd\u4f5c\u652f\u6301\u6709\u9650\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>openpyxl\u5e93<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f18\u70b9\uff1a\u652f\u6301\u5bf9Excel\u6587\u4ef6\u7684\u5e95\u5c42\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u64cd\u4f5c\u76f8\u5bf9\u7e41\u7410\uff0c\u4e0d\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528Python\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d<strong>pandas\u5e93\u3001openpyxl\u5e93<\/strong>\u662f\u5e38\u7528\u7684\u9009\u62e9\u3002<strong>pandas\u5e93<\/strong>\u9002\u5408\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u4efb\u52a1\uff0c\u800c<strong>openpyxl\u5e93<\/strong>\u5219\u9002\u5408\u4e8e\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u590d\u6742\u64cd\u4f5c\u7684\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u4f7f\u4efb\u52a1\u66f4\u52a0\u9ad8\u6548\u548c\u4fbf\u6377\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u76f8\u5e94\u7684\u5e93\uff0c\u5e76\u719f\u6089\u5176\u57fa\u672c\u7528\u6cd5\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u5b66\u4e60\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e24\u79cd\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5220\u9664Excel\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u884c\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u9996\u5148\uff0c\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u4e4b\u540e\u5229\u7528<code>drop<\/code>\u65b9\u6cd5\u53bb\u6389\u7b2c\u4e00\u884c\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u4fdd\u5b58\u4e3a\u65b0\u7684Excel\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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;)\n\n# \u5220\u9664\u7b2c\u4e00\u884c\ndf = df.drop(index=0)\n\n# \u4fdd\u5b58\u4e3a\u65b0\u7684Excel\u6587\u4ef6\ndf.to_excel(&#39;\u65b0\u7684\u6587\u4ef6\u8def\u5f84.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u6211\u7684Excel\u6587\u4ef6\u5305\u542b\u591a\u4e2a\u5de5\u4f5c\u8868\uff0c\u5982\u4f55\u5220\u9664\u7b2c\u4e00\u884c\uff1f<\/strong><br \/>\u5728\u5904\u7406\u591a\u4e2a\u5de5\u4f5c\u8868\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u65f6\u6307\u5b9a\u5de5\u4f5c\u8868\u7684\u540d\u79f0\u6216\u7d22\u5f15\u3002\u4f7f\u7528<code>pd.read_excel<\/code>\u7684<code>sheet_name<\/code>\u53c2\u6570\u53ef\u4ee5\u9009\u62e9\u7279\u5b9a\u7684\u5de5\u4f5c\u8868\u3002\u5220\u9664\u7b2c\u4e00\u884c\u7684\u65b9\u5f0f\u4e0e\u5355\u5de5\u4f5c\u8868\u76f8\u540c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;)\ndf = df.drop(index=0)\ndf.to_excel(&#39;\u65b0\u7684\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5220\u9664\u7b2c\u4e00\u884c\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\uff1f<\/strong><br \/>\u5728\u5220\u9664\u7b2c\u4e00\u884c\u4e4b\u524d\uff0c\u5efa\u8bae\u5148\u5907\u4efd\u539f\u59cb\u6587\u4ef6\u3002\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u68c0\u67e5\uff0c\u786e\u4fdd\u5220\u9664\u64cd\u4f5c\u4e0d\u4f1a\u5f71\u54cd\u5230\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u3002\u53ef\u4ee5\u4f7f\u7528<code>df.head()<\/code>\u65b9\u6cd5\u67e5\u770b\u6570\u636e\u7684\u524d\u51e0\u884c\uff0c\u786e\u8ba4\u5220\u9664\u64cd\u4f5c\u662f\u5426\u7b26\u5408\u9884\u671f\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65b0\u6587\u4ef6\u65f6\uff0c\u6587\u4ef6\u683c\u5f0f\u548c\u6570\u636e\u7c7b\u578b\u4fdd\u6301\u4e00\u81f4\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u683c\u5f0f\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5220\u9664Excel\u4e2d\u7684\u7b2c\u4e00\u884c\uff1apandas\u5e93\u3001openpyxl\u5e93 \u5728Python\u4e2d\uff0c\u5220\u9664Excel [&hellip;]","protected":false},"author":3,"featured_media":1025776,"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\/1025763"}],"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=1025763"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025763\/revisions"}],"predecessor-version":[{"id":1025779,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025763\/revisions\/1025779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025776"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}