{"id":990899,"date":"2024-12-27T08:24:00","date_gmt":"2024-12-27T00:24:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/990899.html"},"modified":"2024-12-27T08:24:02","modified_gmt":"2024-12-27T00:24:02","slug":"python-%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90exl","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/990899.html","title":{"rendered":"python \u5982\u4f55\u81ea\u52a8\u751f\u6210exl"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065424\/29a9ed59-ade0-40fc-aba3-40d4b6a43e96.webp\" alt=\"python \u5982\u4f55\u81ea\u52a8\u751f\u6210exl\" \/><\/p>\n<p><p> <strong>Python\u81ea\u52a8\u751f\u6210Excel\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u548cxlsxwriter\u5e93\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0cpandas\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3001openpyxl\u652f\u6301\u5bf9\u73b0\u6709Excel\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3001xlsxwriter\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u5316\u9009\u9879\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528pandas\u5e93\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u5e93\u751f\u6210Excel\u6587\u4ef6\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaDataFrame<\/strong>\uff1a\u9996\u5148\uff0c\u4f7f\u7528pandas\u5e93\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0cDataFrame\u53ef\u4ee5\u770b\u4f5c\u662f\u4e00\u4e2a\u8868\u683c\u6570\u636e\u7ed3\u6784\uff0c\u7c7b\u4f3c\u4e8eExcel\u4e2d\u7684\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528pandas\u7684<code>to_excel<\/code>\u65b9\u6cd5\u5c06DataFrame\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u6b64\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u8def\u5f84\u5373\u53ef\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u5de5\u4f5c\u8868\u540d\u79f0\u548c\u683c\u5f0f<\/strong>\uff1a\u5728\u5bfc\u51fa\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u5de5\u4f5c\u8868\u7684\u540d\u79f0\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>ExcelWriter<\/code>\u5bf9\u8c61\u8fdb\u884c\u66f4\u591a\u7684\u683c\u5f0f\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8fdb\u4e00\u6b65\u5c55\u5f00\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u5e76\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528PANDAS\u5e93\u751f\u6210Excel\u6587\u4ef6<\/strong><\/h2>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u79d1\u5b66\u9886\u57df\u3002\u5b83\u63d0\u4f9b\u4e86\u6613\u4e8e\u4f7f\u7528\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u4f7f\u5f97\u5904\u7406\u548c\u751f\u6210Excel\u6587\u4ef6\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><h2>1. \u521b\u5efaDataFrame<\/h2>\n<\/p>\n<p><p>\u5728\u751f\u6210Excel\u6587\u4ef6\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\u3002DataFrame\u662fpandas\u5e93\u4e2d\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u7535\u5b50\u8868\u683c\u6216SQL\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684DataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u59d3\u540d\u3001\u5e74\u9f84\u548c\u57ce\u5e02\u4fe1\u606f\u7684DataFrame\u3002<\/p>\n<\/p>\n<p><h2>2. \u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u6709\u4e86DataFrame\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51faDataFrame\u4e3aExcel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>index=False<\/code>\u8868\u793a\u4e0d\u5c06DataFrame\u7684\u7d22\u5f15\u5199\u5165Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>3. \u8bbe\u7f6e\u5de5\u4f5c\u8868\u540d\u79f0\u548c\u683c\u5f0f<\/h2>\n<\/p>\n<p><p>\u5728\u5bfc\u51faExcel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ExcelWriter<\/code>\u5bf9\u8c61\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u8bbe\u7f6e\uff0c\u6bd4\u5982\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528ExcelWriter\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0<\/p>\n<p>with pd.ExcelWriter(&#39;output_with_sheet_name.xlsx&#39;) as writer:<\/p>\n<p>    df.to_excel(writer, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5bfc\u51fa\u65f6\u6307\u5b9a\u5de5\u4f5c\u8868\u7684\u540d\u79f0\u4e3a\u201cSheet1\u201d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528OPENPYXL\u5e93\u751f\u6210Excel\u6587\u4ef6<\/strong><\/h2>\n<p><p>Openpyxl\u662f\u53e6\u4e00\u4e2a\u5904\u7406Excel\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u7684\u4f18\u52bf\u5728\u4e8e\u652f\u6301\u5bf9\u73b0\u6709Excel\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>1. \u521b\u5efa\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528openpyxl\u5e93\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u83b7\u53d6\u6d3b\u52a8\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u8bbe\u7f6e\u5de5\u4f5c\u8868\u540d\u79f0<\/strong><\/h2>\n<p>ws.title = &quot;Sample Sheet&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u5199\u5165\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5411\u5de5\u4f5c\u8868\u4e2d\u6dfb\u52a0\u884c\u6765\u5199\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6570\u636e<\/p>\n<p>ws.append([&quot;Name&quot;, &quot;Age&quot;, &quot;City&quot;])<\/p>\n<p>ws.append([&quot;John&quot;, 28, &quot;New York&quot;])<\/p>\n<p>ws.append([&quot;Anna&quot;, 24, &quot;Paris&quot;])<\/p>\n<p>ws.append([&quot;Peter&quot;, 35, &quot;Berlin&quot;])<\/p>\n<p>ws.append([&quot;Linda&quot;, 32, &quot;London&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u4fdd\u5b58Excel\u6587\u4ef6<\/h2>\n<\/p>\n<p><p>\u5199\u5165\u6570\u636e\u540e\uff0c\u9700\u8981\u4fdd\u5b58\u5de5\u4f5c\u7c3f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58Excel\u6587\u4ef6<\/p>\n<p>wb.save(&quot;openpyxl_output.xlsx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528XLSXWRITER\u5e93\u751f\u6210Excel\u6587\u4ef6<\/strong><\/h2>\n<p><p>XlsxWriter\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efaExcel\u6587\u4ef6\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u683c\u5f0f\u5316\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h2>1. \u521b\u5efa\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u7c3f\u5bf9\u8c61\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e2a\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u5e76\u6dfb\u52a0\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;xlsxwriter_output.xlsx&#39;)<\/p>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u5199\u5165\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u5411\u5de5\u4f5c\u8868\u4e2d\u5199\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u6570\u636e<\/p>\n<p>worksheet.write(&#39;A1&#39;, &#39;Name&#39;)<\/p>\n<p>worksheet.write(&#39;B1&#39;, &#39;Age&#39;)<\/p>\n<p>worksheet.write(&#39;C1&#39;, &#39;City&#39;)<\/p>\n<p>data = [<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<p>row = 1<\/p>\n<p>for name, age, city in data:<\/p>\n<p>    worksheet.write(row, 0, name)<\/p>\n<p>    worksheet.write(row, 1, age)<\/p>\n<p>    worksheet.write(row, 2, city)<\/p>\n<p>    row += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u4fdd\u5b58\u548c\u5173\u95ed\u5de5\u4f5c\u7c3f<\/h2>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4fdd\u5b58\u5e76\u5173\u95ed\u5de5\u4f5c\u7c3f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u5de5\u4f5c\u7c3f<\/p>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u6bd4\u8f83\u4e09\u79cd\u65b9\u6cd5\u7684\u4f18\u52a3<\/strong><\/h2>\n<p><p>\u5728\u9009\u62e9\u9002\u5408\u7684\u5e93\u65f6\uff0c\u9700\u8003\u8651\u4ee5\u4e0b\u56e0\u7d20\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Pandas<\/strong>\uff1a\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u5927\u6570\u636e\u96c6\uff0c\u5177\u6709\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u5bfc\u51fa\u6570\u636e\u3002<\/li>\n<li><strong>Openpyxl<\/strong>\uff1a\u9002\u5408\u5bf9\u73b0\u6709Excel\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\uff0c\u652f\u6301\u66f4\u591a\u683c\u5f0f\u5316\u9009\u9879\u3002<\/li>\n<li><strong>XlsxWriter<\/strong>\uff1a\u9002\u5408\u521b\u5efa\u65b0\u7684Excel\u6587\u4ef6\uff0c\u5e76\u63d0\u4f9b\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u9009\u9879\u3002<\/li>\n<\/ul>\n<p><p>\u603b\u7ed3\u6765\u8bf4\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u6570\u636e\u5904\u7406\u590d\u6742\u6027\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5bfc\u51fa\uff0cpandas\u662f\u4e00\u4e2a\u5feb\u901f\u800c\u9ad8\u6548\u7684\u9009\u62e9\uff1b\u5bf9\u4e8e\u9700\u8981\u590d\u6742\u683c\u5f0f\u5316\u548c\u73b0\u6709\u6587\u4ef6\u64cd\u4f5c\u7684\u573a\u666f\uff0copenpyxl\u548cxlsxwriter\u5219\u66f4\u52a0\u5408\u9002\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u5e93\u7684\u4f18\u52bf\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u8fdb\u884cExcel\u6587\u4ef6\u7684\u81ea\u52a8\u751f\u6210\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210Excel\u6587\u4ef6\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u751f\u6210Excel\u6587\u4ef6\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u5e27\u4fdd\u5b58\u4e3aExcel\u683c\u5f0f\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas openpyxl<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\u5e76\u8c03\u7528<code>to_excel<\/code>\u65b9\u6cd5\uff0c\u5c06\u5176\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\ndata = {\n    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;],\n    &#39;Age&#39;: [25, 30, 35]\n}\ndf = pd.DataFrame(data)\ndf.to_excel(&#39;output.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5728\u751f\u6210\u7684Excel\u4e2d\u5982\u4f55\u6dfb\u52a0\u683c\u5f0f\uff1f<\/strong><br \/>\u8981\u5728\u751f\u6210\u7684Excel\u6587\u4ef6\u4e2d\u6dfb\u52a0\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u3002\u8be5\u5e93\u5141\u8bb8\u5bf9Excel\u5355\u5143\u683c\u8fdb\u884c\u5404\u79cd\u683c\u5f0f\u8bbe\u7f6e\uff0c\u4f8b\u5982\u5b57\u4f53\u3001\u989c\u8272\u548c\u8fb9\u6846\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5bf9\u7279\u5b9a\u5355\u5143\u683c\u5e94\u7528\u6837\u5f0f\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u5355\u5143\u683c\u7684\u5b57\u4f53\u548c\u586b\u5145\u989c\u8272\uff1a  <\/p>\n<pre><code class=\"language-python\">from openpyxl import Workbook\nfrom openpyxl.styles import Font, PatternFill\n\nwb = Workbook()\nws = wb.active\nws[&#39;A1&#39;] = &#39;Hello&#39;\nws[&#39;A1&#39;].font = Font(bold=True, color=&quot;FF0000&quot;)\nws[&#39;A1&#39;].fill = PatternFill(start_color=&quot;FFFF00&quot;, end_color=&quot;FFFF00&quot;, fill_type=&quot;solid&quot;)\n\nwb.save(&#39;styled_output.xlsx&#39;)\n<\/code><\/pre>\n<p><strong>\u751f\u6210Excel\u65f6\u5982\u4f55\u5904\u7406\u5927\u6570\u636e\u91cf\uff1f<\/strong><br \/>\u5904\u7406\u5927\u6570\u636e\u91cf\u65f6\uff0c\u4f7f\u7528<code>pandas<\/code>\u7684<code>chunksize<\/code>\u53c2\u6570\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u6570\u636e\u5206\u5757\u5199\u5165Excel\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u907f\u514d\u4e86\u5728\u5185\u5b58\u4e2d\u52a0\u8f7d\u6574\u4e2a\u6570\u636e\u96c6\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>chunksize<\/code>\u6765\u9010\u5757\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u5c06\u5176\u5199\u5165Excel\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u5047\u8bbe\u6709\u4e00\u4e2a\u5927\u578bCSV\u6587\u4ef6\nfor chunk in pd.read_csv(&#39;large_data.csv&#39;, chunksize=10000):\n    chunk.to_excel(&#39;large_output.xlsx&#39;, mode=&#39;a&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u5728\u5904\u7406\u5927\u6570\u636e\u96c6\u65f6\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u81ea\u52a8\u751f\u6210Excel\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u548cxls [&hellip;]","protected":false},"author":3,"featured_media":990904,"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\/990899"}],"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=990899"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990899\/revisions"}],"predecessor-version":[{"id":990907,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/990899\/revisions\/990907"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/990904"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=990899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=990899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=990899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}