{"id":947527,"date":"2024-12-26T23:52:26","date_gmt":"2024-12-26T15:52:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/947527.html"},"modified":"2024-12-26T23:52:28","modified_gmt":"2024-12-26T15:52:28","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%baexecl","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/947527.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51faexecl"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083252\/2241a925-e0bf-455c-bc39-bba766b02a92.webp\" alt=\"python\u5982\u4f55\u8f93\u51faexecl\" \/><\/p>\n<p><p> <strong>Python\u8f93\u51faExcel\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001OpenPyXL\u5e93\u3001XlsxWriter\u5e93\u3002Pandas\u5e93\u66f4\u4e3a\u7b80\u5355\u3001\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u6570\u636e\u6846\uff0cOpenPyXL\u5e93\u5219\u9002\u5408\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u64cd\u4f5c\uff0cXlsxWriter\u5e93\u4e13\u6ce8\u4e8e\u751f\u6210Excel\u6587\u4ef6\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecdPandas\u5e93\u8fdb\u884cExcel\u6587\u4ef6\u8f93\u51fa\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Pandas\u5e93\u8f93\u51faExcel<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u548c\u8f93\u51fa\u5404\u79cd\u683c\u5f0f\u7684\u6570\u636e\u6587\u4ef6\uff0c\u5305\u62ecExcel\u3002\u4f7f\u7528Pandas\u8f93\u51faExcel\u6587\u4ef6\u53ea\u9700\u51e0\u884c\u4ee3\u7801\uff0c\u4e0b\u9762\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pandas\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\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>1.2 \u8bfb\u53d6\u6570\u636e\u5230DataFrame<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u6709\u4e00\u4e9b\u6570\u636e\uff0c\u5e76\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u529f\u80fd\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],<\/p>\n<p>    &#39;Age&#39;: [25, 30, 35],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;, &#39;Chicago&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3 \u8f93\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>to_excel<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u8f93\u51fa\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>index=False<\/code>\u53c2\u6570\u7528\u4e8e\u907f\u514d\u5c06DataFrame\u7684\u7d22\u5f15\u8f93\u51fa\u5230Excel\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001OpenPyXL\u5e93\u8f93\u51faExcel<\/h3>\n<\/p>\n<p><p>OpenPyXL\u662f\u4e00\u4e2a\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\u7684\u5e93\uff0c\u9002\u5408\u9700\u8981\u5bf9Excel\u6587\u4ef6\u8fdb\u884c\u66f4\u7ec6\u81f4\u64cd\u4f5c\u7684\u573a\u5408\uff0c\u6bd4\u5982\u4fee\u6539\u5df2\u6709\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u5b89\u88c5OpenPyXL<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u5728\u4f7f\u7528OpenPyXL\u4e4b\u524d\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u521b\u5efa\u548c\u8f93\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4f7f\u7528OpenPyXL\u521b\u5efa\u5e76\u8f93\u51faExcel\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u5de5\u4f5c\u7c3f<\/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>\u5199\u5165\u6570\u636e\u5230\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws.append([&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;])<\/p>\n<p>ws.append([&#39;Alice&#39;, 25, &#39;New York&#39;])<\/p>\n<p>ws.append([&#39;Bob&#39;, 30, &#39;Los Angeles&#39;])<\/p>\n<p>ws.append([&#39;Charlie&#39;, 35, &#39;Chicago&#39;])<\/p>\n<h2><strong>\u4fdd\u5b58Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output_openpyxl.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001XlsxWriter\u5e93\u8f93\u51faExcel<\/h3>\n<\/p>\n<p><p>XlsxWriter\u662f\u4e13\u95e8\u7528\u4e8e\u521b\u5efaExcel\u6587\u4ef6\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u5b89\u88c5XlsxWriter<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5XlsxWriter\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install XlsxWriter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528XlsxWriter\u8f93\u51faExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528XlsxWriter\u521b\u5efa\u5e76\u8f93\u51faExcel\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;output_xlsxwriter.xlsx&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e\u5230\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 25, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 30, &#39;Los Angeles&#39;],<\/p>\n<p>    [&#39;Charlie&#39;, 35, &#39;Chicago&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u9010\u884c\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>for row_num, row_data in enumerate(data):<\/p>\n<p>    worksheet.write_row(row_num, 0, row_data)<\/p>\n<h2><strong>\u5173\u95ed\u5e76\u4fdd\u5b58Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6bd4\u8f83Pandas\u3001OpenPyXL\u548cXlsxWriter<\/h3>\n<\/p>\n<p><p>\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\uff0c\u9009\u62e9\u54ea\u4e2a\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>4.1 Pandas<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u9002\u5408\u5904\u7406\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u5c24\u5176\u662f\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u8fc7\u6ee4\u3001\u5206\u7ec4\u3001\u805a\u5408\u7b49\u3002<\/li>\n<li>\u8bed\u6cd5\u7b80\u5355\uff0c\u6613\u4e8e\u5b66\u4e60\u548c\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u6846\u64cd\u4f5c\uff0c\u683c\u5f0f\u5316\u529f\u80fd\u4e0d\u5982XlsxWriter\u4e30\u5bcc\u3002<\/li>\n<\/ul>\n<p><h4>4.2 OpenPyXL<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u652f\u6301\u5bf9\u73b0\u6709Excel\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/li>\n<li>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u9009\u9879\uff0c\u9002\u5408\u9700\u8981\u590d\u6742\u64cd\u4f5c\u7684\u573a\u5408\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u6027\u80fd\u53ef\u80fd\u4e0d\u5982Pandas\u3002<\/li>\n<\/ul>\n<p><h4>4.3 XlsxWriter<\/h4>\n<\/p>\n<p><p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e13\u6ce8\u4e8e\u521b\u5efaExcel\u6587\u4ef6\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u548c\u56fe\u8868\u529f\u80fd\u3002<\/li>\n<li>\u751f\u6210\u7684Excel\u6587\u4ef6\u4f53\u79ef\u8f83\u5c0f\uff0c\u9002\u5408\u9700\u8981\u7cbe\u7f8e\u62a5\u544a\u7684\u573a\u5408\u3002<\/li>\n<\/ul>\n<p><p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4e0d\u80fd\u8bfb\u5199\u73b0\u6709\u7684Excel\u6587\u4ef6\uff0c\u53ea\u80fd\u7528\u4e8e\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8f93\u51faExcel\u6587\u4ef6\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u5408\u3002\u5bf9\u4e8e\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0cPandas\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u5bf9\u73b0\u6709Excel\u6587\u4ef6\u8fdb\u884c\u64cd\u4f5c\uff0cOpenPyXL\u662f\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u800c\u5bf9\u4e8e\u521b\u5efa\u7cbe\u7f8e\u7684Excel\u62a5\u544a\uff0cXlsxWriter\u662f\u7406\u60f3\u7684\u5de5\u5177\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5199\u5165Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5199\u5165Excel\u6587\u4ef6\uff0c\u5e38\u7528\u7684\u5e93\u662f<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u3002\u4f7f\u7528<code>pandas<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>DataFrame<\/code>\u5bf9\u8c61\u8f7b\u677e\u5730\u521b\u5efa\u8868\u683c\u6570\u636e\uff0c\u5e76\u8c03\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u5176\u8f93\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u786e\u4fdd\u5b89\u88c5<code>openpyxl<\/code>\u5e93\u4ee5\u652f\u6301Excel\u683c\u5f0f\uff0c\u4f7f\u7528\u547d\u4ee4<code>pip install pandas openpyxl<\/code>\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>Python\u652f\u6301\u54ea\u4e9bExcel\u6587\u4ef6\u683c\u5f0f\uff1f<\/strong><br \/>Python\u901a\u5e38\u652f\u6301<code>.xlsx<\/code>\u548c<code>.xls<\/code>\u4e24\u79cdExcel\u6587\u4ef6\u683c\u5f0f\u3002<code>pandas<\/code>\u5e93\u9ed8\u8ba4\u8f93\u51fa\u4e3a<code>.xlsx<\/code>\u683c\u5f0f\uff0c\u800c<code>openpyxl<\/code>\u5e93\u4e13\u95e8\u7528\u4e8e\u5904\u7406<code>.xlsx<\/code>\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u8f83\u65e7\u7684<code>.xls<\/code>\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>xlrd<\/code>\u548c<code>xlwt<\/code>\u5e93\uff0c\u4f46\u5efa\u8bae\u5c3d\u91cf\u4f7f\u7528\u8f83\u65b0\u7684<code>.xlsx<\/code>\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8bfb\u53d6Excel\u6587\u4ef6\u3002\u8c03\u7528<code>pandas.read_excel()<\/code>\u65b9\u6cd5\uff0c\u5e76\u4f20\u5165\u6587\u4ef6\u8def\u5f84\u5373\u53ef\u3002\u6b64\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u4fbf\u4e8e\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u786e\u4fdd\u5b89\u88c5\u4e86<code>openpyxl<\/code>\u6216<code>xlrd<\/code>\u5e93\uff0c\u4ee5\u652f\u6301\u4e0d\u540c\u7684Excel\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8f93\u51faExcel\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Pandas\u5e93\u3001OpenPyXL\u5e93\u3001XlsxWrite [&hellip;]","protected":false},"author":3,"featured_media":947537,"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\/947527"}],"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=947527"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947527\/revisions"}],"predecessor-version":[{"id":947541,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/947527\/revisions\/947541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/947537"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=947527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=947527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=947527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}