{"id":1007976,"date":"2024-12-27T10:58:35","date_gmt":"2024-12-27T02:58:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007976.html"},"modified":"2024-12-27T10:58:39","modified_gmt":"2024-12-27T02:58:39","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e7%bb%93%e6%9e%9cexcel","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007976.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u7ed3\u679cExcel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083450\/343c98a5-97ec-4e00-a9a2-0ef6f93fda50.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u7ed3\u679cExcel\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8f93\u51fa\u7ed3\u679c\u5230Excel\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ec<strong>pandas\u3001openpyxl\u3001xlsxwriter<\/strong>\u7b49\u3002<strong>\u5229\u7528pandas\u5e93\u7684DataFrame\u5bf9\u8c61\u3001\u4f7f\u7528openpyxl\u5e93\u6765\u64cd\u4f5c\u73b0\u6709\u7684Excel\u6587\u4ef6\u3001\u901a\u8fc7xlsxwriter\u521b\u5efa\u548c\u683c\u5f0f\u5316\u65b0\u7684Excel\u6587\u4ef6<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528PANDAS\u5e93<\/p>\n<\/p>\n<p><p><strong>pandas<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u63a5\u53e3\u6765\u5c06\u6570\u636e\u5bfc\u51fa\u4e3aExcel\u683c\u5f0f\u3002\u4e3b\u8981\u901a\u8fc7<code>pandas.DataFrame.to_excel()<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u521b\u5efaDataFrame\u5e76\u5bfc\u51fa<\/strong><\/li>\n<\/ol>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u521b\u5efaDataFrame\u5e76\u5c06\u5176\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\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<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bfc\u51fa\u4e3aExcel\u6587\u4ef6<\/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\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e09\u5217\u6570\u636e\u7684DataFrame\uff0c\u5e76\u4f7f\u7528<code>to_excel()<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u5230\u540d\u4e3a<code>output.xlsx<\/code>\u7684Excel\u6587\u4ef6\u4e2d\u3002\u53c2\u6570<code>index=False<\/code>\u8868\u793a\u4e0d\u5bfc\u51fa\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u81ea\u5b9a\u4e49Excel\u8f93\u51fa<\/strong><\/li>\n<\/ol>\n<p><p>pandas\u5e93\u8fd8\u5141\u8bb8\u6211\u4eec\u81ea\u5b9a\u4e49Excel\u8f93\u51fa\u683c\u5f0f\uff0c\u4f8b\u5982\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0\u3001\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u3001\u6dfb\u52a0\u683c\u5f0f\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5bfc\u51fa\u5230Excel\u6587\u4ef6\uff0c\u5e76\u81ea\u5b9a\u4e49\u5de5\u4f5c\u8868\u540d\u79f0<\/p>\n<p>df.to_excel(&#39;output.xlsx&#39;, sheet_name=&#39;Sheet1&#39;, index=False)<\/p>\n<h2><strong>\u6307\u5b9a\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, dtype={&#39;Age&#39;: int}, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u5de5\u4f5c\u8868\u540d\u79f0\u4e3a<code>Sheet1<\/code>\uff0c\u5e76\u660e\u786e\u6307\u5b9a\u4e86<code>Age<\/code>\u5217\u7684\u6570\u636e\u7c7b\u578b\u4e3a\u6574\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528OPENPYXL\u5e93<\/p>\n<\/p>\n<p><p><strong>openpyxl<\/strong>\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u652f\u6301Excel\u7684\u9ad8\u7ea7\u7279\u6027\uff0c\u4f8b\u5982\u516c\u5f0f\u3001\u56fe\u8868\u3001\u56fe\u50cf\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5openpyxl\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528openpyxl\u6765\u521b\u5efa\u548c\u64cd\u4f5cExcel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u6fc0\u6d3b\u9ed8\u8ba4\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>ws[&#39;A1&#39;] = &#39;Name&#39;<\/p>\n<p>ws[&#39;B1&#39;] = &#39;Age&#39;<\/p>\n<p>ws[&#39;C1&#39;] = &#39;City&#39;<\/p>\n<p>data = [<\/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<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528openpyxl\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4e86\u4e00\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u548c\u4fee\u6539\u73b0\u6709\u7684Excel\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p>openpyxl\u8fd8\u652f\u6301\u8bfb\u53d6\u548c\u4fee\u6539\u73b0\u6709\u7684Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u52a0\u8f7d\u73b0\u6709\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = load_workbook(&#39;output.xlsx&#39;)<\/p>\n<h2><strong>\u6fc0\u6d3b\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row in ws.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e<\/strong><\/h2>\n<p>ws[&#39;A2&#39;] = &#39;Updated Name&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539<\/strong><\/h2>\n<p>wb.save(&#39;output.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u52a0\u8f7d\u4e86\u4e00\u4e2a\u73b0\u6709\u7684Excel\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u4e86\u5176\u4e2d\u7684\u6570\u636e\uff0c\u7136\u540e\u4fee\u6539\u4e86\u67d0\u4e2a\u5355\u5143\u683c\u7684\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528XLSXWRITER\u5e93<\/p>\n<\/p>\n<p><p><strong>xlsxwriter<\/strong>\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efaExcel\u6587\u4ef6\u7684Python\u5e93\uff0c\u652f\u6301\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u9009\u9879\u548c\u7279\u6027\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u57fa\u672c\u4f7f\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5xlsxwriter\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install XlsxWriter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528xlsxwriter\u521b\u5efa\u548c\u683c\u5f0f\u5316\u65b0\u7684Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlsxwriter<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u548c\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;output.xlsx&#39;)<\/p>\n<p>worksheet = workbook.add_worksheet()<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\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;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<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<h2><strong>\u5173\u95ed\u5e76\u4fdd\u5b58\u6587\u4ef6<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528xlsxwriter\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u4e86\u4e00\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u683c\u5f0f\u5316\u548c\u56fe\u8868<\/strong><\/li>\n<\/ol>\n<p><p>xlsxwriter\u652f\u6301\u4e30\u5bcc\u7684\u683c\u5f0f\u5316\u9009\u9879\u548c\u56fe\u8868\u521b\u5efa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u683c\u5f0f\u5bf9\u8c61<\/p>\n<p>bold = workbook.add_format({&#39;bold&#39;: True})<\/p>\n<h2><strong>\u5e94\u7528\u683c\u5f0f<\/strong><\/h2>\n<p>worksheet.write(&#39;A1&#39;, &#39;Name&#39;, bold)<\/p>\n<p>worksheet.write(&#39;B1&#39;, &#39;Age&#39;, bold)<\/p>\n<p>worksheet.write(&#39;C1&#39;, &#39;City&#39;, bold)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>chart = workbook.add_chart({&#39;type&#39;: &#39;column&#39;})<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e\u5230\u56fe\u8868<\/strong><\/h2>\n<p>chart.add_series({<\/p>\n<p>    &#39;categories&#39;: &#39;=Sheet1!$A$2:$A$4&#39;,<\/p>\n<p>    &#39;values&#39;: &#39;=Sheet1!$B$2:$B$4&#39;,<\/p>\n<p>    &#39;name&#39;: &#39;Age&#39;<\/p>\n<p>})<\/p>\n<h2><strong>\u5c06\u56fe\u8868\u63d2\u5165\u5230\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>worksheet.insert_chart(&#39;E5&#39;, chart)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528xlsxwriter\u521b\u5efa\u4e86\u4e00\u4e2a\u67f1\u72b6\u56fe\uff0c\u5e76\u5c06\u5176\u63d2\u5165\u5230\u5de5\u4f5c\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u683c\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u8bf7\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u6b63\u786e\u3002\u4e00\u822c\u6765\u8bf4\uff0cpandas\u548copenpyxl\u9002\u7528\u4e8exlsx\u683c\u5f0f\uff0c\u800cxlsxwriter\u4e13\u7528\u4e8exlsx\u683c\u5f0f\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6027\u80fd<\/strong><\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u5927\u6570\u636e\u96c6\uff0c\u4f7f\u7528pandas\u53ef\u80fd\u662f\u6700\u4f73\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5728\u5904\u7406\u6570\u636e\u65f6\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u517c\u5bb9\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u60a8\u4f7f\u7528\u7684\u5e93\u7248\u672c\u4e0e\u60a8\u7684Python\u7248\u672c\u517c\u5bb9\uff0c\u5e76\u9605\u8bfb\u76f8\u5173\u6587\u6863\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u548c\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u4e2d\u7684\u6570\u636e\u5bfc\u51fa\u5230Excel\u6587\u4ef6\u4e2d\uff0c\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u636e\u5bfc\u51fa\u8fd8\u662f\u590d\u6742\u7684\u683c\u5f0f\u5316\u548c\u56fe\u8868\u521b\u5efa\uff0c\u8fd9\u4e9b\u5de5\u5177\u90fd\u80fd\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8f7b\u677e\u5730\u5c06\u6570\u636e\u4fdd\u5b58\u4e3aExcel\u6587\u4ef6\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas openpyxl<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>DataFrame<\/code>\u5bf9\u8c61\u5e76\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u5c06\u5176\u5bfc\u51fa\u5230Excel\u6587\u4ef6\u3002\u4f8b\u5982\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>Python\u8f93\u51faExcel\u6587\u4ef6\u65f6\u80fd\u5426\u8bbe\u7f6e\u5355\u5143\u683c\u683c\u5f0f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u53ef\u4ee5\u5bf9Excel\u6587\u4ef6\u4e2d\u7684\u5355\u5143\u683c\u8fdb\u884c\u683c\u5f0f\u8bbe\u7f6e\u3002\u5728\u521b\u5efa\u6216\u4fee\u6539Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u6765\u8c03\u6574\u5b57\u4f53\u3001\u989c\u8272\u3001\u8fb9\u6846\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from openpyxl import Workbook\nfrom openpyxl.styles import Font\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;)\nwb.save(&#39;formatted_output.xlsx&#39;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u6570\u636e\uff1f<\/strong><br \/>\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7<code>read_excel<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5c06Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u5230<code>DataFrame<\/code>\u4e2d\u3002\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86<code>openpyxl<\/code>\u5e93\u4ee5\u652f\u6301Excel\u8bfb\u53d6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf = pd.read_excel(&#39;output.xlsx&#39;)\nprint(df)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u67e5\u770bExcel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8f93\u51fa\u7ed3\u679c\u5230Excel\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecpandas\u3001openpyxl\u3001xlsxwri [&hellip;]","protected":false},"author":3,"featured_media":1007985,"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\/1007976"}],"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=1007976"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007976\/revisions"}],"predecessor-version":[{"id":1007987,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007976\/revisions\/1007987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007985"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}