{"id":1072630,"date":"2025-01-08T11:17:58","date_gmt":"2025-01-08T03:17:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072630.html"},"modified":"2025-01-08T11:18:00","modified_gmt":"2025-01-08T03:18:00","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96xlsx%e8%a1%a8%e6%a0%bc%e9%87%8c%e6%95%b0%e6%8d%ae-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1072630.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u8bfb\u53d6xlsx\u8868\u683c\u91cc\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102813\/54b118fc-2003-47c3-b141-90a2c63ab44b.webp\" alt=\"Python\u4e2d\u5982\u4f55\u8bfb\u53d6xlsx\u8868\u683c\u91cc\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8bfb\u53d6xlsx\u8868\u683c\u91cc\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982pandas\u5e93\u3001openpyxl\u5e93\u548cxlrd\u5e93\u3002\u4f7f\u7528pandas\u5e93\u662f\u6700\u65b9\u4fbf\u3001\u529f\u80fd\u6700\u5f3a\u5927\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001PANDAS\u5e93\u8bfb\u53d6\u6570\u636e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u8bfb\u53d6xlsx\u6570\u636e\u975e\u5e38\u7b80\u5355\uff0c<strong>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\uff0c\u5305\u62ecExcel\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pandas\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p>pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/li>\n<\/ol>\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;file.xlsx&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6570\u636e<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>pandas\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u8fd8\u80fd\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\u3002\u5b83\u652f\u6301\u6309\u5de5\u4f5c\u8868\u540d\u79f0\u8bfb\u53d6\u4e0d\u540c\u7684\u5de5\u4f5c\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6e\u8bfb\u53d6\u7684\u6570\u636e\u8303\u56f4\u3002<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7279\u5b9a\u5de5\u4f5c\u8868<\/p>\n<p>df = pd.read_excel(&#39;file.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;file.xlsx&#39;, usecols=&#39;A:C&#39;, nrows=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001OPENPYXL\u5e93\u8bfb\u53d6\u6570\u636e<\/p>\n<\/p>\n<p><p>openpyxl\u5e93\u662f\u4e13\u95e8\u7528\u4e8e\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5openpyxl\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = load_workbook(&#39;file.xlsx&#39;)<\/p>\n<h2><strong>\u9009\u62e9\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb[&#39;Sheet1&#39;]<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row in ws.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>openpyxl\u5e93\u63d0\u4f9b\u4e86\u66f4\u5e95\u5c42\u7684\u64cd\u4f5c\uff0c\u9002\u5408\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u7ec6\u7c92\u5ea6\u63a7\u5236\u7684\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001XLRD\u5e93\u8bfb\u53d6\u6570\u636e<\/p>\n<\/p>\n<p><p>xlrd\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u5e93\uff0c\u4f46\u5b83\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6\u65e7\u7248\u7684xls\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5xlrd\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install xlrd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<h2><strong>\u6253\u5f00Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = xlrd.open_workbook(&#39;file.xls&#39;)<\/p>\n<h2><strong>\u9009\u62e9\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>sheet = workbook.sheet_by_name(&#39;Sheet1&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row_idx in range(sheet.nrows):<\/p>\n<p>    print(sheet.row(row_idx))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5c3d\u7ba1xlrd\u5e93\u4e0d\u518d\u652f\u6301xlsx\u6587\u4ef6\uff0c\u4f46\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u65e7\u7248xls\u6587\u4ef6\u7684\u573a\u666f\u4ecd\u7136\u6709\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001\u6bd4\u8f83\u4e0d\u540c\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<ol>\n<li><strong>pandas\u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u529f\u80fd\u5f3a\u5927\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u3002<\/li>\n<li>\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\uff0c\u5185\u5b58\u5360\u7528\u8f83\u591a\u3002<\/li>\n<li>\u9700\u8981\u989d\u5916\u5b89\u88c5openpyxl\u5e93\u6765\u652f\u6301xlsx\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>openpyxl\u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e13\u95e8\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\uff0c\u652f\u6301xlsx\/xlsm\/xltx\/xltm\u683c\u5f0f\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u7ec6\u7c92\u5ea6\u7684\u64cd\u4f5c\u63a7\u5236\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u76f8\u5bf9\u4e8epandas\uff0c\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u529f\u80fd\u8f83\u5f31\u3002<\/li>\n<li>\u64cd\u4f5c\u590d\u6742\u5ea6\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>xlrd\u5e93\uff1a<\/strong><\/li>\n<\/ol>\n<p><p>\u4f18\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9002\u7528\u4e8e\u5904\u7406\u65e7\u7248xls\u6587\u4ef6\u3002<\/li>\n<li>\u64cd\u4f5c\u7b80\u5355\u3002<\/li>\n<\/ul>\n<p><p>\u7f3a\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u518d\u652f\u6301xlsx\u6587\u4ef6\u3002<\/li>\n<li>\u529f\u80fd\u8f83\u4e3a\u6709\u9650\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u5b9e\u8df5\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u5e76\u5904\u7406Excel\u6587\u4ef6\u7684\u5b9e\u9645\u6848\u4f8b\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;sales_data.xlsx&#39;)<\/p>\n<h2><strong>\u663e\u793a\u524d5\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<h2><strong>\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d<\/strong><\/h2>\n<p>product_sales = df.groupby(&#39;Product&#39;)[&#39;Sales&#39;].sum()<\/p>\n<p>print(product_sales)<\/p>\n<h2><strong>\u7b5b\u9009\u51fa\u9500\u552e\u989d\u5927\u4e8e1000\u7684\u4ea7\u54c1<\/strong><\/h2>\n<p>high_sales_products = product_sales[product_sales &gt; 1000]<\/p>\n<p>print(high_sales_products)<\/p>\n<h2><strong>\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u65b0\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>high_sales_products.to_excel(&#39;high_sales_products.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u9500\u552e\u6570\u636e\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u4ea7\u54c1\u7684\u603b\u9500\u552e\u989d\uff0c\u7b5b\u9009\u51fa\u9500\u552e\u989d\u5927\u4e8e1000\u7684\u4ea7\u54c1\uff0c\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p><strong>\u5728Python\u4e2d\u8bfb\u53d6xlsx\u8868\u683c\u91cc\u7684\u6570\u636e\uff0c\u63a8\u8350\u4f7f\u7528pandas\u5e93\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\uff0c\u64cd\u4f5c\u7b80\u4fbf\uff0c\u5e76\u4e14\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u3002\u5982\u679c\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9009\u62e9openpyxl\u5e93\u3002\u5bf9\u4e8e\u5904\u7406\u65e7\u7248xls\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528xlrd\u5e93\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8bfb\u53d6\u548c\u5904\u7406Excel\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u66f4\u597d\u5730\u8bfb\u53d6\u548c\u5904\u7406xlsx\u8868\u683c\u6570\u636e\u3002\u5982\u679c\u4f60\u6709\u66f4\u591a\u95ee\u9898\u6216\u9700\u6c42\uff0c\u6b22\u8fce\u7ee7\u7eed\u4ea4\u6d41\u548c\u63a2\u8ba8\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6xlsx\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bfb\u53d6xlsx\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>openpyxl<\/code>\u548c<code>pandas<\/code>\u3002\u4f7f\u7528<code>openpyxl<\/code>\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5cExcel\u6587\u4ef6\uff0c\u9002\u5408\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u8f83\u590d\u6742\u7684\u64cd\u4f5c\u3002\u800c<code>pandas<\/code>\u5219\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u65b9\u5f0f\uff0c\u5c24\u5176\u9002\u5408\u6570\u636e\u5206\u6790\u3002\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\u540e\uff0c\u53ef\u4ee5\u5206\u522b\u4f7f\u7528<code>load_workbook()<\/code>\u548c<code>read_excel()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u8bfb\u53d6xlsx\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u8868\u683c\u4e2d\u7684\u7a7a\u767d\u5355\u5143\u683c\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6xlsx\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7a7a\u767d\u5355\u5143\u683c\u3002\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>fillna()<\/code>\u65b9\u6cd5\u6765\u586b\u5145\u7a7a\u767d\u5355\u5143\u683c\uff0c\u9009\u62e9\u75280\u3001\u7a7a\u5b57\u7b26\u4e32\u6216\u5176\u4ed6\u503c\u66ff\u6362\u7a7a\u767d\u5185\u5bb9\u3002\u5982\u679c\u4f7f\u7528<code>openpyxl<\/code>\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u540e\u624b\u52a8\u68c0\u67e5\u5355\u5143\u683c\u5185\u5bb9\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u7279\u5b9a\u7684\u5de5\u4f5c\u8868\u6765\u8bfb\u53d6\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679cxlsx\u6587\u4ef6\u4e2d\u5305\u542b\u591a\u4e2a\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728<code>openpyxl<\/code>\u4e2d\u4f7f\u7528<code>active<\/code>\u5c5e\u6027\u6216<code>get_sheet_by_name()<\/code>\u65b9\u6cd5\u6765\u9009\u62e9\u7279\u5b9a\u7684\u5de5\u4f5c\u8868\u3002<code>pandas<\/code>\u7684<code>read_excel()<\/code>\u51fd\u6570\u4e5f\u5141\u8bb8\u901a\u8fc7<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\u540d\u79f0\u6216\u7d22\u5f15\uff0c\u4ece\u800c\u65b9\u4fbf\u5730\u63d0\u53d6\u6240\u9700\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bfb\u53d6xlsx\u8868\u683c\u91cc\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5982pandas\u5e93\u3001openpyxl\u5e93\u548cxlrd\u5e93\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1072638,"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\/1072630"}],"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=1072630"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072630\/revisions"}],"predecessor-version":[{"id":1072641,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072630\/revisions\/1072641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072638"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}