{"id":1118767,"date":"2025-01-08T18:38:24","date_gmt":"2025-01-08T10:38:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118767.html"},"modified":"2025-01-08T18:38:27","modified_gmt":"2025-01-08T10:38:27","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e5%86%99%e5%85%a5excel%e4%b8%80%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118767.html","title":{"rendered":"python\u5982\u4f55\u5c06\u5199\u5165excel\u4e00\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082129\/1840c317-5539-4020-8ccc-e0716b99b35b.webp\" alt=\"python\u5982\u4f55\u5c06\u5199\u5165excel\u4e00\u5217\" \/><\/p>\n<p><p> <strong>\u8981\u5c06\u6570\u636e\u5199\u5165Excel\u7684\u7279\u5b9a\u5217\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684pandas\u5e93\u3001openpyxl\u5e93\u6216\u8005xlsxwriter\u5e93\u3002<\/strong> \u8fd9\u51e0\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u4f46\u90fd\u80fd\u6709\u6548\u5730\u5b8c\u6210\u4efb\u52a1\u3002<strong>pandas\u6613\u4e8e\u4f7f\u7528\u3001\u529f\u80fd\u5f3a\u5927\uff0copenpyxl\u9002\u5408\u5904\u7406\u590d\u6742\u7684Excel\u64cd\u4f5c\uff0cxlsxwriter\u5219\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684Excel\u6587\u4ef6\u3002<\/strong> \u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u64cd\u4f5cExcel\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528pandas\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &quot;A&quot;: [1, 2, 3],<\/p>\n<p>    &quot;B&quot;: [4, 5, 6],<\/p>\n<p>    &quot;C&quot;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5c06\u6570\u636e\u6846\u5199\u5165Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&quot;example.xlsx&quot;, index=False)<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6\u5e76\u5728\u7279\u5b9a\u5217\u5199\u5165\u65b0\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_excel(&quot;example.xlsx&quot;)<\/p>\n<p>df[&quot;D&quot;] = [10, 11, 12]  # \u5199\u5165\u65b0\u6570\u636e\u5230\u5217D<\/p>\n<p>df.to_excel(&quot;example.xlsx&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846\u5e76\u5c06\u5176\u5199\u5165\u4e00\u4e2a\u540d\u4e3aexample.xlsx\u7684Excel\u6587\u4ef6\u3002\u7136\u540e\uff0c\u6211\u4eec\u8bfb\u53d6\u8be5\u6587\u4ef6\u5e76\u5728\u5217D\u4e2d\u5199\u5165\u65b0\u6570\u636e\uff0c\u6700\u540e\u5c06\u66f4\u65b0\u540e\u7684\u6570\u636e\u6846\u4fdd\u5b58\u56de\u540c\u4e00\u4e2aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528openpyxl\u5e93<\/h3>\n<\/p>\n<p><p>openpyxl\u5e93\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u64cd\u4f5cExcel\u6587\u4ef6\u7684\u6d41\u884c\u5de5\u5177\u3002\u5b83\u652f\u6301Excel\u6587\u4ef6\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684Excel\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528openpyxl\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook, load_workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5411\u5de5\u4f5c\u8868\u4e2d\u5199\u5165\u4e00\u4e9b\u6570\u636e<\/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<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<h2><strong>\u4fdd\u5b58\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb.save(&quot;example.xlsx&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u5de5\u4f5c\u7c3f\u5e76\u5728\u7279\u5b9a\u5217\u5199\u5165\u65b0\u6570\u636e<\/strong><\/h2>\n<p>wb = load_workbook(&quot;example.xlsx&quot;)<\/p>\n<p>ws = wb.active<\/p>\n<p>new_data = [10, 11, 12]<\/p>\n<p>for idx, value in enumerate(new_data, start=1):<\/p>\n<p>    ws.cell(row=idx, column=4, value=value)  # \u5199\u5165\u65b0\u6570\u636e\u5230\u5217D<\/p>\n<p>wb.save(&quot;example.xlsx&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\u5e76\u5411\u5de5\u4f5c\u8868\u4e2d\u5199\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u7136\u540e\u4fdd\u5b58\u8be5\u5de5\u4f5c\u7c3f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8bfb\u53d6\u8be5\u5de5\u4f5c\u7c3f\u5e76\u5728\u5217D\u4e2d\u5199\u5165\u65b0\u6570\u636e\uff0c\u6700\u540e\u5c06\u66f4\u65b0\u540e\u7684\u5de5\u4f5c\u7c3f\u4fdd\u5b58\u56de\u540c\u4e00\u4e2a\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528xlsxwriter\u5e93<\/h3>\n<\/p>\n<p><p>xlsxwriter\u5e93\u662f\u4e00\u4e2a\u4e13\u95e8\u7528\u4e8e\u521b\u5efa\u590d\u6742Excel\u6587\u4ef6\u7684\u5de5\u5177\u3002\u5b83\u652f\u6301\u591a\u79cd\u9ad8\u7ea7Excel\u529f\u80fd\uff0c\u5982\u683c\u5f0f\u5316\u3001\u56fe\u8868\u3001\u6761\u4ef6\u683c\u5f0f\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528xlsxwriter\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&quot;example.xlsx&quot;)<\/p>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u5411\u5de5\u4f5c\u8868\u4e2d\u5199\u5165\u4e00\u4e9b\u6570\u636e<\/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<p>for row_num, row_data in enumerate(data):<\/p>\n<p>    for col_num, cell_data in enumerate(row_data):<\/p>\n<p>        worksheet.write(row_num, col_num, cell_data)<\/p>\n<h2><strong>\u5199\u5165\u65b0\u6570\u636e\u5230\u7279\u5b9a\u5217<\/strong><\/h2>\n<p>new_data = [10, 11, 12]<\/p>\n<p>for row_num, cell_data in enumerate(new_data):<\/p>\n<p>    worksheet.write(row_num, 3, cell_data)  # \u5199\u5165\u65b0\u6570\u636e\u5230\u5217D<\/p>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868\uff0c\u5e76\u5411\u5de5\u4f5c\u8868\u4e2d\u5199\u5165\u4e00\u4e9b\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5728\u5217D\u4e2d\u5199\u5165\u65b0\u6570\u636e\uff0c\u6700\u540e\u5173\u95ed\u5de5\u4f5c\u7c3f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528pandas\u3001openpyxl\u548cxlsxwriter\u5e93\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\u3002<strong>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>pandas\u5e93<\/strong>\uff1a\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u5408\u5904\u7406\u548c\u5206\u6790\u5927\u89c4\u6a21\u6570\u636e\u3002<\/li>\n<li><strong>openpyxl\u5e93<\/strong>\uff1a\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u5904\u7406\u590d\u6742\u7684Excel\u64cd\u4f5c\u3002<\/li>\n<li><strong>xlsxwriter\u5e93<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684Excel\u6587\u4ef6\uff0c\u652f\u6301\u591a\u79cd\u9ad8\u7ea7\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u7279\u5b9a\u5217\u4e2d\uff0c\u5e76\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u6570\u636e\u5199\u5165Excel\u7684\u4e00\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u548c<code>openpyxl<\/code>\u5e93\u6765\u65b9\u4fbf\u5730\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u7684\u4e00\u5217\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-bash\">pip install pandas openpyxl\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u5c06\u6570\u636e\u5199\u5165Excel\u7684\u4e00\u5217\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6570\u636e\u7684\u5b57\u5178\ndata = {&#39;Column1&#39;: [&#39;\u6570\u636e1&#39;, &#39;\u6570\u636e2&#39;, &#39;\u6570\u636e3&#39;]}\n\n# \u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\ndf = pd.DataFrame(data)\n\n# \u5199\u5165Excel\u6587\u4ef6\ndf.to_excel(&#39;output.xlsx&#39;, index=False, engine=&#39;openpyxl&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3a<code>output.xlsx<\/code>\u7684Excel\u6587\u4ef6\uff0c\u6570\u636e\u88ab\u5199\u5165\u5230\u6307\u5b9a\u7684\u5217\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5199\u5165Excel\u65f6\u5982\u4f55\u786e\u4fdd\u683c\u5f0f\u6b63\u786e\uff1f<\/strong><br \/>\u5728\u5199\u5165Excel\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u683c\u5f0f\u6b63\u786e\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u65e5\u671f\u6570\u636e\u5199\u5165Excel\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u7684\u65e5\u671f\u65f6\u95f4\u683c\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>pd.to_datetime()<\/code>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u786e\u4fdd\u5728\u5199\u5165Excel\u6587\u4ef6\u4e4b\u524d\u68c0\u67e5\u6570\u636e\u7c7b\u578b\uff0c\u4ee5\u4fbf\u5728Excel\u4e2d\u5448\u73b0\u65f6\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5199\u5165Excel\u4e00\u5217\u65f6\uff0c\u5982\u4f55\u5904\u7406\u91cd\u590d\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u91cd\u590d\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>pandas<\/code>\u63d0\u4f9b\u7684\u529f\u80fd\u6765\u5b9e\u73b0\u3002\u5728\u5199\u5165\u6570\u636e\u4e4b\u524d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>DataFrame.drop_duplicates()<\/code>\u65b9\u6cd5\u53bb\u9664\u91cd\u590d\u9879\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u91cd\u590d\u6570\u636e\u7684\u5b57\u5178\ndata = {&#39;Column1&#39;: [&#39;\u6570\u636e1&#39;, &#39;\u6570\u636e2&#39;, &#39;\u6570\u636e2&#39;, &#39;\u6570\u636e3&#39;]}\n\n# \u5c06\u5b57\u5178\u8f6c\u6362\u4e3aDataFrame\ndf = pd.DataFrame(data)\n\n# \u53bb\u9664\u91cd\u590d\u6570\u636e\ndf = df.drop_duplicates()\n\n# \u5199\u5165Excel\u6587\u4ef6\ndf.to_excel(&#39;output.xlsx&#39;, index=False, engine=&#39;openpyxl&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u751f\u6210\u7684Excel\u6587\u4ef6\u4e2d\u5c31\u4e0d\u4f1a\u5305\u542b\u91cd\u590d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06\u6570\u636e\u5199\u5165Excel\u7684\u7279\u5b9a\u5217\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684pandas\u5e93\u3001openpyxl\u5e93\u6216\u8005xlsxwrit [&hellip;]","protected":false},"author":3,"featured_media":1118773,"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\/1118767"}],"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=1118767"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118767\/revisions"}],"predecessor-version":[{"id":1118776,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118767\/revisions\/1118776"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118773"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}