{"id":1172883,"date":"2025-01-15T16:55:56","date_gmt":"2025-01-15T08:55:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172883.html"},"modified":"2025-01-15T16:55:59","modified_gmt":"2025-01-15T08:55:59","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e6%95%b0%e7%bb%84%e5%88%b0xls","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1172883.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\u6570\u7ec4\u5230xls"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074835\/3cb3cf2f-877e-4eee-90a3-45adc7d85a4a.webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u6570\u7ec4\u5230xls\" \/><\/p>\n<p><p> <strong>Python\u4fdd\u5b58\u6570\u7ec4\u5230xls\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001xlwt\u5e93\u3001openpyxl\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528pandas\u5e93\u662f\u6700\u7b80\u5355\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002<strong>pandas\u5e93\u529f\u80fd\u5f3a\u5927\u3001\u4ee3\u7801\u7b80\u6d01\u3001\u6613\u4e8e\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u5e93\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>pandas\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u8bfb\u5199\uff0c\u5305\u62ecExcel\u6587\u4ef6\u3002\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff0c\u5e76\u4fdd\u5b58\u4e3axls\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pandas\u5e93\uff1a\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>pip install pandas<\/code>\u6765\u5b89\u88c5pandas\u5e93\u3002<\/li>\n<li>\u5bfc\u5165pandas\u5e93\uff1a\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528<code>import pandas as pd<\/code>\u5bfc\u5165pandas\u5e93\u3002<\/li>\n<li>\u521b\u5efaDataFrame\uff1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\u3002<\/li>\n<li>\u4fdd\u5b58\u4e3axls\u6587\u4ef6\uff1a\u4f7f\u7528DataFrame\u5bf9\u8c61\u7684<code>to_excel<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3axls\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3axls\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame(array)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3axls\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xls&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528Pandas\u5e93<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5\u548c\u5bfc\u5165Pandas\u5e93<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86pandas\u5e93\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u521b\u5efaDataFrame<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6570\u7ec4\u8f6c\u6362\u4e3apandas\u7684DataFrame\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u4fdd\u5b58\u4e3axls\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528DataFrame\u5bf9\u8c61\u7684<code>to_excel<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u4fdd\u5b58\u4e3axls\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_excel(&#39;output.xls&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>&#39;output.xls&#39;<\/code>\u662f\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\uff0c<code>index=False<\/code>\u8868\u793a\u4e0d\u4fdd\u5b58\u7d22\u5f15\u5217\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528xlwt\u5e93<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5\u548c\u5bfc\u5165xlwt\u5e93<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86xlwt\u5e93\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install xlwt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165xlwt\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlwt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u521b\u5efaWorkbook\u548cSheet<\/strong><\/p>\n<p>\u4f7f\u7528xlwt\u5e93\u521b\u5efa\u4e00\u4e2aWorkbook\u5bf9\u8c61\u548c\u4e00\u4e2aSheet\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook = xlwt.Workbook()<\/p>\n<p>sheet = workbook.add_sheet(&#39;Sheet1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u5199\u5165\u6570\u636e<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6570\u7ec4\u9010\u884c\u5199\u5165Sheet\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i, row in enumerate(array):<\/p>\n<p>    for j, value in enumerate(row):<\/p>\n<p>        sheet.write(i, j, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u4fdd\u5b58\u4e3axls\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528Workbook\u5bf9\u8c61\u7684<code>save<\/code>\u65b9\u6cd5\u5c06Workbook\u4fdd\u5b58\u4e3axls\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook.save(&#39;output.xls&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>&#39;output.xls&#39;<\/code>\u662f\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528openpyxl\u5e93<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5\u548c\u5bfc\u5165openpyxl\u5e93<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86openpyxl\u5e93\u3002\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-shell\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165openpyxl\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u521b\u5efaWorkbook\u548cSheet<\/strong><\/p>\n<p>\u4f7f\u7528openpyxl\u5e93\u521b\u5efa\u4e00\u4e2aWorkbook\u5bf9\u8c61\u548c\u4e00\u4e2aSheet\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook = Workbook()<\/p>\n<p>sheet = workbook.active<\/p>\n<p>sheet.title = &#39;Sheet1&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u5199\u5165\u6570\u636e<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6570\u7ec4\u9010\u884c\u5199\u5165Sheet\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i, row in enumerate(array):<\/p>\n<p>    for j, value in enumerate(row):<\/p>\n<p>        sheet.cell(row=i+1, column=j+1, value=value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u4fdd\u5b58\u4e3axls\u6587\u4ef6<\/strong><\/p>\n<p>\u4f7f\u7528Workbook\u5bf9\u8c61\u7684<code>save<\/code>\u65b9\u6cd5\u5c06Workbook\u4fdd\u5b58\u4e3axls\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">workbook.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0copenpyxl\u5e93\u53ea\u652f\u6301Excel 2007\u53ca\u4ee5\u4e0a\u7248\u672c\u7684xlsx\u683c\u5f0f\uff0c\u4e0d\u652f\u6301xls\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u5bf9\u6bd4\u4e09\u79cd\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u6613\u7528\u6027<\/strong><\/p>\n<p>pandas\u5e93\u7684\u4ee3\u7801\u6700\u7b80\u6d01\uff0c\u4f7f\u7528\u6700\u65b9\u4fbf\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002xlwt\u5e93\u7684\u4ee3\u7801\u8f83\u590d\u6742\uff0c\u4f46\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u81ea\u5b9a\u4e49\u6837\u5f0f\u7b49\u9ad8\u7ea7\u529f\u80fd\u7684\u573a\u666f\u3002openpyxl\u5e93\u652f\u6301xlsx\u683c\u5f0f\uff0c\u4f46\u4e0d\u652f\u6301xls\u683c\u5f0f\uff0c\u9002\u5408\u5904\u7406\u8f83\u65b0\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6027\u80fd<\/strong><\/p>\n<p>\u5728\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0cpandas\u5e93\u7684\u6027\u80fd\u8f83\u597d\uff0c\u56e0\u4e3a\u5b83\u5185\u90e8\u4f7f\u7528\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u3002xlwt\u5e93\u7684\u6027\u80fd\u4e5f\u4e0d\u9519\uff0c\u4f46\u5728\u5904\u7406\u975e\u5e38\u5927\u7684\u6570\u636e\u96c6\u65f6\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u95ee\u9898\u3002openpyxl\u5e93\u7684\u6027\u80fd\u76f8\u5bf9\u8f83\u5dee\uff0c\u4f46\u53ef\u4ee5\u5904\u7406\u8f83\u65b0\u7684Excel\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u529f\u80fd<\/strong><\/p>\n<p>pandas\u5e93\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u8bfb\u5199\uff0c\u529f\u80fd\u975e\u5e38\u5f3a\u5927\u3002xlwt\u5e93\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u548c\u4fee\u6539Excel\u6587\u4ef6\uff0c\u652f\u6301\u81ea\u5b9a\u4e49\u6837\u5f0f\u3001\u516c\u5f0f\u7b49\u9ad8\u7ea7\u529f\u80fd\u3002openpyxl\u5e93\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u548c\u4fee\u6539xlsx\u683c\u5f0f\u7684Excel\u6587\u4ef6\uff0c\u652f\u6301\u8bfb\u53d6\u548c\u5199\u5165\u590d\u6742\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9009\u62e9<\/strong><\/p>\n<\/p>\n<p><p><strong>1. \u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u8981\u76ee\u7684\u662f\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0c\u5efa\u8bae\u4f7f\u7528pandas\u5e93\u3002pandas\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u8bfb\u5199\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u8f6c\u6362\u3001\u6c47\u603b\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u751f\u6210\u548c\u4fee\u6539Excel\u6587\u4ef6<\/strong><\/p>\n<p>\u5982\u679c\u4e3b\u8981\u76ee\u7684\u662f\u751f\u6210\u548c\u4fee\u6539Excel\u6587\u4ef6\uff0c\u5efa\u8bae\u4f7f\u7528xlwt\u5e93\u6216openpyxl\u5e93\u3002xlwt\u5e93\u652f\u6301xls\u683c\u5f0f\uff0c\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u9700\u8981\u81ea\u5b9a\u4e49\u6837\u5f0f\u3001\u516c\u5f0f\u7b49\u9ad8\u7ea7\u529f\u80fd\u7684\u573a\u666f\u3002openpyxl\u5e93\u652f\u6301xlsx\u683c\u5f0f\uff0c\u9002\u5408\u5904\u7406\u8f83\u65b0\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u5904\u7406\u5927\u6570\u636e\u91cf<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u6570\u636e\u91cf\uff0c\u5efa\u8bae\u4f7f\u7528pandas\u5e93\u3002pandas\u5e93\u5185\u90e8\u4f7f\u7528\u4e86\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u6570\u636e\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0cpandas\u5e93\u662f\u4fdd\u5b58\u6570\u7ec4\u5230xls\u6587\u4ef6\u7684\u9996\u9009\u65b9\u6cd5\uff0c\u4ee3\u7801\u7b80\u6d01\uff0c\u4f7f\u7528\u65b9\u4fbf\uff0c\u529f\u80fd\u5f3a\u5927\u3002xlwt\u5e93\u548copenpyxl\u5e93\u4e5f\u5404\u6709\u4f18\u70b9\uff0c\u9002\u5408\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u6839\u636e\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\uff0c\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u5904\u7406\u548c\u6587\u4ef6\u751f\u6210\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u6570\u7ec4\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8f7b\u677e\u5730\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u5e93\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install pandas openpyxl<\/code>\u6765\u5b89\u88c5\u5b83\u4eec\u3002\u63a5\u4e0b\u6765\uff0c\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\uff0c\u7136\u540e\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4\ndata = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\ndf = pd.DataFrame(data)\n\n# \u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528NumPy\u6570\u7ec4\u65f6\uff0c\u5982\u4f55\u5c06\u5176\u4fdd\u5b58\u4e3aExcel\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u4f7f\u7528<code>NumPy<\/code>\u6570\u7ec4\uff0c\u53ef\u4ee5\u5148\u5c06\u5176\u8f6c\u6362\u4e3a<code>pandas<\/code>\u7684DataFrame\uff0c\u7136\u540e\u6309\u7167\u4e0a\u8ff0\u65b9\u6cd5\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4\narray = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\ndf = pd.DataFrame(array)\n\n# \u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5728\u4fdd\u5b58\u6570\u7ec4\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49Excel\u7684\u5de5\u4f5c\u8868\u540d\u79f0\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sheet_name<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u5de5\u4f5c\u8868\u7684\u540d\u79f0\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">df.to_excel(&#39;output.xlsx&#39;, sheet_name=&#39;MyData&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbe\u7f6e\u5de5\u4f5c\u8868\u7684\u540d\u79f0\uff0c\u4ee5\u4f7fExcel\u6587\u4ef6\u66f4\u52a0\u6613\u4e8e\u7ba1\u7406\u548c\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4fdd\u5b58\u6570\u7ec4\u5230xls\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001xlwt\u5e93\u3001openpyxl\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1172891,"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\/1172883"}],"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=1172883"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172883\/revisions"}],"predecessor-version":[{"id":1172894,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172883\/revisions\/1172894"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172891"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}