{"id":1011997,"date":"2024-12-27T11:34:59","date_gmt":"2024-12-27T03:34:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011997.html"},"modified":"2024-12-27T11:35:03","modified_gmt":"2024-12-27T03:35:03","slug":"python%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e5%ad%98%e5%85%a5excel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011997.html","title":{"rendered":"python\u6570\u7ec4\u5982\u4f55\u5b58\u5165excel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085940\/a33ed0d5-68be-4d34-b68b-fc46d2b79236.webp\" alt=\"python\u6570\u7ec4\u5982\u4f55\u5b58\u5165excel\" \/><\/p>\n<p><p> \u5c06Python\u6570\u7ec4\u5b58\u5165Excel\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlwt\u5e93<\/strong>\u3002\u5176\u4e2d\uff0cpandas\u5e93\u4ee5\u5176\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u548c\u7b80\u5355\u7684API\uff0c\u6210\u4e3a\u4e86\u6700\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u3002\u5728\u6b64\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b9e\u73b0Python\u6570\u7ec4\u5b58\u5165Excel\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u5e93\uff0c\u652f\u6301\u591a\u79cd\u6570\u636e\u683c\u5f0f\u7684\u8bfb\u5199\u3002\u4f7f\u7528Pandas\u5c06Python\u6570\u7ec4\u5b58\u5165Excel\u6587\u4ef6\u662f\u4e00\u79cd\u975e\u5e38\u7b80\u4fbf\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Pandas<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528Pandas\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p>pip install openpyxl  # \u7528\u4e8eExcel\u6587\u4ef6\u7684\u8bfb\u5199<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u5e76\u4fdd\u5b58<\/li>\n<\/ol>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u60f3\u8981\u5c06\u5176\u5b58\u5165Excel\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u8fd9\u662f\u6211\u4eec\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])<\/p>\n<h2><strong>\u5c06DataFrame\u5b58\u5165Excel<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u5c06\u5176\u4fdd\u5b58\u5230Excel\u6587\u4ef6\u4e2d\u3002<code>index=False<\/code>\u53c2\u6570\u7528\u4e8e\u907f\u514d\u5c06\u7d22\u5f15\u5217\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Openpyxl\u5e93<\/p>\n<\/p>\n<p><p>Openpyxl\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u548c\u5199\u5165Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u66f4\u7ec6\u7c92\u5ea6\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u5728Excel\u6587\u4ef6\u4e2d\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Openpyxl<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5Openpyxl\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u6570\u7ec4\u5199\u5165Excel\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Openpyxl\u5c06\u6570\u7ec4\u5199\u5165Excel\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Workbook<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u6fc0\u6d3b\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5047\u8bbe\u8fd9\u662f\u6211\u4eec\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5199\u5165\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output_openpyxl.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684Workbook\u5bf9\u8c61\uff0c\u5e76\u5c06\u6570\u7ec4\u7684\u6570\u636e\u9010\u884c\u5199\u5165\u5de5\u4f5c\u8868\uff0c\u6700\u540e\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Xlwt\u5e93<\/p>\n<\/p>\n<p><p>Xlwt\u5e93\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u5199\u5165Excel\u683c\u5f0f\uff08xls\uff09\u7684Python\u5e93\u3002\u5b83\u9002\u7528\u4e8e\u8f83\u65e7\u7248\u672c\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Xlwt<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5Xlwt\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xlwt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5c06\u6570\u7ec4\u5199\u5165Excel\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Xlwt\u5c06\u6570\u7ec4\u5199\u5165Excel\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlwt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aWorkbook\u5bf9\u8c61<\/strong><\/h2>\n<p>workbook = xlwt.Workbook()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>sheet = workbook.add_sheet(&#39;Sheet1&#39;)<\/p>\n<h2><strong>\u5047\u8bbe\u8fd9\u662f\u6211\u4eec\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [1, 2, 3],<\/p>\n<p>    [4, 5, 6],<\/p>\n<p>    [7, 8, 9]<\/p>\n<p>]<\/p>\n<h2><strong>\u5c06\u6570\u636e\u5199\u5165\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>for i, row in enumerate(data):<\/p>\n<p>    for j, value in enumerate(row):<\/p>\n<p>        sheet.write(i, j, value)<\/p>\n<h2><strong>\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>workbook.save(&#39;output_xlwt.xls&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aWorkbook\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u904d\u5386\u6570\u7ec4\u5c06\u6570\u636e\u9010\u4e2a\u5355\u5143\u683c\u5199\u5165\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528Numpy\u4e0ePandas\u7ed3\u5408<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u9886\u57df\uff0cNumpy\u548cPandas\u7ecf\u5e38\u7ed3\u5408\u4f7f\u7528\u3002\u53ef\u4ee5\u5229\u7528Numpy\u521b\u5efa\u6570\u7ec4\u5e76\u901a\u8fc7Pandas\u8f7b\u677e\u5bfc\u51fa\u5230Excel\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Numpy<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7ed3\u5408\u4f7f\u7528Numpy\u548cPandas<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumpy\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u5c06Numpy\u6570\u7ec4\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(array, columns=[&#39;Column1&#39;, &#39;Column2&#39;, &#39;Column3&#39;])<\/p>\n<h2><strong>\u5bfc\u51fa\u5230Excel<\/strong><\/h2>\n<p>df.to_excel(&#39;output_numpy_pandas.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aNumpy\u6570\u7ec4\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aPandas DataFrame\uff0c\u6700\u540e\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u51e0\u79cd\u5c06Python\u6570\u7ec4\u5b58\u5165Excel\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<strong>Pandas\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u5408\u5927\u591a\u6570\u60c5\u51b5\uff0c\u800cOpenpyxl\u548cXlwt\u9002\u5408\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u63a7\u5236\u7684\u573a\u666f<\/strong>\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0Python\u6570\u636e\u7684Excel\u5bfc\u51fa\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u4fbf\u5229\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u4e2d\u7684\u6570\u7ec4\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06Python\u4e2d\u7684\u6570\u7ec4\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u9996\u5148\uff0c\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a<code>DataFrame<\/code>\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u7ec4\ndata = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\ndf = pd.DataFrame(data)\n\n# \u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u5b89\u88c5\u4e86<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u5e93\uff0c\u4ee5\u4fbf\u80fd\u591f\u521b\u5efaExcel\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5c06\u6570\u7ec4\u4fdd\u5b58\u4e3aExcel\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>pandas<\/code>\u3001<code>openpyxl<\/code>\u548c<code>xlsxwriter<\/code>\u3002<code>pandas<\/code>\u662f\u6700\u6d41\u884c\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u652f\u6301\u6570\u636e\u5904\u7406\uff0c\u8fd8\u80fd\u8f7b\u677e\u5bfc\u51fa\u6570\u636e\u5230Excel\u683c\u5f0f\u3002\u800c<code>openpyxl<\/code>\u548c<code>xlsxwriter<\/code>\u5219\u66f4\u9002\u5408\u9700\u8981\u7cbe\u7ec6\u63a7\u5236Excel\u6587\u4ef6\u683c\u5f0f\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u4fdd\u5b58\u6570\u7ec4\u5230Excel\u65f6\uff0c\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\uff1f<\/strong><br \/>\u5728\u4fdd\u5b58\u6570\u7ec4\u5230Excel\u65f6\uff0c\u9700\u6ce8\u610f\u6570\u7ec4\u7684\u5f62\u72b6\u548c\u6570\u636e\u7c7b\u578b\u3002\u786e\u4fdd\u6570\u7ec4\u662f\u4e8c\u7ef4\u7684\uff0c\u4ee5\u4fbf\u6b63\u786e\u6620\u5c04\u5230Excel\u7684\u884c\u548c\u5217\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>index=False<\/code>\u53c2\u6570\u53ef\u4ee5\u907f\u514d\u5728Excel\u4e2d\u5199\u5165\u884c\u7d22\u5f15\uff0c\u8fd9\u901a\u5e38\u4f1a\u4f7f\u6570\u636e\u66f4\u52a0\u6574\u6d01\u3002\u5982\u679c\u6570\u636e\u4e2d\u5b58\u5728\u7f3a\u5931\u503c\uff0c\u4f7f\u7528<code>pandas<\/code>\u5904\u7406\u65f6\u4f1a\u81ea\u52a8\u586b\u5145\u4e3aNaN\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5c06Python\u6570\u7ec4\u5b58\u5165Excel\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1012012,"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\/1011997"}],"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=1011997"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011997\/revisions"}],"predecessor-version":[{"id":1012014,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011997\/revisions\/1012014"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1012012"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}