{"id":1164027,"date":"2025-01-15T15:01:47","date_gmt":"2025-01-15T07:01:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1164027.html"},"modified":"2025-01-15T15:01:50","modified_gmt":"2025-01-15T07:01:50","slug":"python%e5%a6%82%e4%bd%95%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9%e5%86%99%e5%85%a5excel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1164027.html","title":{"rendered":"python\u5982\u4f55\u6587\u4ef6\u5185\u5bb9\u5199\u5165excel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25204653\/cb8d3c89-bbd3-4ecf-8a67-e2e93a85b9ff.webp\" alt=\"python\u5982\u4f55\u6587\u4ef6\u5185\u5bb9\u5199\u5165excel\" \/><\/p>\n<p><p> <strong>Python\u6587\u4ef6\u5185\u5bb9\u5199\u5165Excel\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u3001\u4f7f\u7528<code>pandas<\/code>\u5e93\u3001\u4f7f\u7528<code>xlsxwriter<\/code>\u5e93\u3002<\/strong>\u5176\u4e2d\uff0c<code>pandas<\/code>\u5e93\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528<code>pandas<\/code>\u5e93\u5c06\u6587\u4ef6\u5185\u5bb9\u5199\u5165Excel\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>pandas<\/code>\u5e93\u548c<code>openpyxl<\/code>\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5185\u5bb9\u3002\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u8981\u8bfb\u53d6\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u6587\u4ef6\u5185\u5bb9\u6bcf\u884c\u662f\u4e00\u4e2a\u6570\u636e\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528Pandas\u5199\u5165Excel<\/h2>\n<\/p>\n<p><h3>1\u3001\u521b\u5efaDataFrame\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u662f\u4e00\u4e2a\u4e8c\u7ef4\u8868\u683c\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u5185\u5bb9\u7ec4\u7ec7\u6210<code>DataFrame<\/code>\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6587\u4ef6\u5185\u5bb9\u6bcf\u884c\u662f\u4e00\u4e2a\u6570\u636e\u9879\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame(lines, columns=[&#39;Data&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5<code>to_excel<\/code>\uff0c\u53ef\u4ee5\u5c06<code>DataFrame<\/code>\u5bf9\u8c61\u5199\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8bbe\u7f6e\u5199\u5165Excel\u6587\u4ef6\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p><code>to_excel<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982\u662f\u5426\u5305\u542b\u7d22\u5f15\u3001\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0\u3001\u683c\u5f0f\u5316\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165Excel\u6587\u4ef6\u5e76\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False, sheet_name=&#39;Sheet1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u66f4\u591a\u9ad8\u7ea7\u64cd\u4f5c<\/h2>\n<\/p>\n<p><h3>1\u3001\u5199\u5165\u591a\u4e2a\u5de5\u4f5c\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>pandas<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u5199\u5165\u591a\u4e2a\u5de5\u4f5c\u8868\uff0c\u53ea\u9700\u521b\u5efa\u4e00\u4e2a<code>ExcelWriter<\/code>\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with pd.ExcelWriter(&#39;output.xlsx&#39;) as writer:<\/p>\n<p>    df.to_excel(writer, sheet_name=&#39;Sheet1&#39;)<\/p>\n<p>    df.to_excel(writer, sheet_name=&#39;Sheet2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u8bbe\u7f6e\u5355\u5143\u683c\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8bbe\u7f6e\u5355\u5143\u683c\u683c\u5f0f\uff0c\u53ef\u4ee5\u7ed3\u5408<code>openpyxl<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u5148\u5199\u5165Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6\u5e76\u8bbe\u7f6e\u683c\u5f0f<\/strong><\/h2>\n<p>workbook = load_workbook(&#39;output.xlsx&#39;)<\/p>\n<p>sheet = workbook.active<\/p>\n<p>for cell in sheet[&#39;A&#39;]:<\/p>\n<p>    cell.font = Font(size=12, bold=True)<\/p>\n<p>workbook.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u5904\u7406\u590d\u6742\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u6bd4\u8f83\u590d\u6742\uff0c\u4f8b\u5982\u5305\u542b\u591a\u5217\u6570\u636e\uff0c\u53ef\u4ee5\u5148\u5c06\u6570\u636e\u89e3\u6790\u6210\u591a\u4e2a\u5217\u8868\u6216\u5b57\u5178\uff0c\u7136\u540e\u521b\u5efa<code>DataFrame<\/code>\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<h2><strong>name,age,city<\/strong><\/h2>\n<h2><strong>Alice,30,New York<\/strong><\/h2>\n<h2><strong>Bob,25,Los Angeles<\/strong><\/h2>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u89e3\u6790<\/strong><\/h2>\n<p>data = []<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        data.append(line.strip().split(&#39;,&#39;))<\/p>\n<h2><strong>\u521b\u5efaDataFrame\u5bf9\u8c61<\/strong><\/h2>\n<p>df = pd.DataFrame(data[1:], columns=data[0])<\/p>\n<h2><strong>\u5199\u5165Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u5904\u7406\u5927\u6570\u636e\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chunk<\/code>\u6765\u5206\u5757\u5904\u7406\u6570\u636e\uff0c\u907f\u514d\u5185\u5b58\u6ea2\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chunk_size = 1000<\/p>\n<p>chunks = []<\/p>\n<h2><strong>\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;large_data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    chunk = []<\/p>\n<p>    for i, line in enumerate(file):<\/p>\n<p>        chunk.append(line.strip().split(&#39;,&#39;))<\/p>\n<p>        if (i + 1) % chunk_size == 0:<\/p>\n<p>            chunks.append(pd.DataFrame(chunk))<\/p>\n<p>            chunk = []<\/p>\n<p>    if chunk:<\/p>\n<p>        chunks.append(pd.DataFrame(chunk))<\/p>\n<h2><strong>\u5199\u5165Excel\u6587\u4ef6<\/strong><\/h2>\n<p>with pd.ExcelWriter(&#39;large_output.xlsx&#39;) as writer:<\/p>\n<p>    for i, chunk in enumerate(chunks):<\/p>\n<p>        chunk.to_excel(writer, sheet_name=f&#39;Sheet{i+1}&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u5c06\u6587\u4ef6\u5185\u5bb9\u5199\u5165Excel\u6587\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u4f7f\u7528<code>pandas<\/code>\u5e93\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u6587\u4ef6\u5185\u5bb9\u7684\u8bfb\u53d6\u548c\u5199\u5165Excel\u6587\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u5199\u5165\u591a\u4e2a\u5de5\u4f5c\u8868\u3001\u8bbe\u7f6e\u5355\u5143\u683c\u683c\u5f0f\u3001\u5904\u7406\u590d\u6742\u6570\u636e\u548c\u5927\u6570\u636e\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5199\u5165\uff0c\u8fd8\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u548c\u683c\u5f0f\u5316\u9700\u6c42\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u7b49\u5e93\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u53c2\u6570\uff0c\u5c06\u4f1a\u4f7f\u4f60\u7684\u6570\u636e\u5904\u7406\u5de5\u4f5c\u66f4\u52a0\u5f97\u5fc3\u5e94\u624b\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u638c\u63e1Python\u6587\u4ef6\u5185\u5bb9\u5199\u5165Excel\u7684\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6587\u672c\u6587\u4ef6\u7684\u6570\u636e\u5199\u5165Excel\u8868\u683c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5e76\u5c06\u6570\u636e\u5199\u5165Excel\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Pandas\u548cOpenPyXL\u5e93\u3002\u901a\u8fc7<code>pd.read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528<code>pd.to_excel()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u6846\u5199\u5165Excel\u6587\u4ef6\uff0c\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u5de5\u4f5c\u8868\u540d\u79f0\u5373\u53ef\u3002<\/p>\n<p><strong>\u5728\u5199\u5165Excel\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7279\u6b8a\u5b57\u7b26\u6216\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u5904\u7406\u5305\u542b\u7279\u6b8a\u5b57\u7b26\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5efa\u8bae\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6765\u786e\u4fdd\u6587\u4ef6\u6b63\u786e\u8bfb\u53d6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>encoding=&#39;utf-8&#39;<\/code>\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u95ee\u9898\u5bfc\u81f4\u7684\u6570\u636e\u4e22\u5931\u3002\u5bf9\u4e8e\u65e5\u671f\u3001\u6570\u5b57\u7b49\u7279\u6b8a\u683c\u5f0f\uff0c\u53ef\u4ee5\u5728Pandas\u4e2d\u4f7f\u7528\u76f8\u5e94\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u8f6c\u6362\uff0c\u786e\u4fdd\u5728Excel\u4e2d\u5448\u73b0\u6b63\u786e\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6279\u91cf\u5c06\u591a\u4e2a\u6587\u672c\u6587\u4ef6\u5199\u5165\u540c\u4e00\u4e2aExcel\u6587\u4ef6\u7684\u4e0d\u540c\u5de5\u4f5c\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u591a\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u4f7f\u7528<code>pd.ExcelWriter()<\/code>\u6765\u521b\u5efa\u4e00\u4e2aExcel\u6587\u4ef6\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u5de5\u4f5c\u8868\u3002\u5728\u5faa\u73af\u4e2d\uff0c\u4f7f\u7528<code>pd.read_csv()<\/code>\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\uff0c\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u76f8\u5e94\u7684\u5de5\u4f5c\u8868\u3002\u786e\u4fdd\u5728\u5199\u5165\u65f6\u4f7f\u7528<code>sheet_name<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6587\u4ef6\u5185\u5bb9\u5199\u5165Excel\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\uff1a\u4f7f\u7528openpyxl\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528xlsx [&hellip;]","protected":false},"author":3,"featured_media":1164034,"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\/1164027"}],"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=1164027"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164027\/revisions"}],"predecessor-version":[{"id":1164035,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1164027\/revisions\/1164035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1164034"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1164027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1164027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1164027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}