{"id":1046316,"date":"2024-12-31T13:32:12","date_gmt":"2024-12-31T05:32:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1046316.html"},"modified":"2024-12-31T13:32:15","modified_gmt":"2024-12-31T05:32:15","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%8d%95%e5%85%83%e6%a0%bc%e6%8f%92%e5%85%a5%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1046316.html","title":{"rendered":"python\u5982\u4f55\u5728\u5355\u5143\u683c\u63d2\u5165\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/21f6f2f8-340a-4a4a-ad95-5061eff7f512.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5728\u5355\u5143\u683c\u63d2\u5165\u6570\u636e\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u5728\u5355\u5143\u683c\u63d2\u5165\u6570\u636e<\/strong>\uff1a\u4f7f\u7528Python\u5728Excel\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5e93\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982<code>openpyxl<\/code>\u3001<code>pandas<\/code>\u3001<code>xlsxwriter<\/code>\u3001<code>xlrd<\/code>\u7b49\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528openpyxl\u5e93\u3001\u53ef\u4ee5\u7528pandas\u5e93\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u3001xlsxwriter\u5e93\u9002\u5408\u521b\u5efa\u548c\u4fee\u6539Excel\u6587\u4ef6\u3001xlrd\u5e93\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>openpyxl<\/code>\u548c<code>pandas<\/code>\u6765\u63d2\u5165\u6570\u636e\u5230Excel\u5355\u5143\u683c\u4e2d\uff0c\u5e76\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528openpyxl\u5e93\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>openpyxl<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684Python\u5e93\u3002\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728Excel\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5openpyxl\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\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. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u5e76\u5728\u7279\u5b9a\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\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>\u83b7\u53d6\u6d3b\u52a8\u7684\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5728\u5355\u5143\u683cA1\u4e2d\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>ws[&#39;A1&#39;] = &#39;Hello, World!&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb.save(&#39;example.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\uff0c\u5e76\u5728\u5355\u5143\u683cA1\u4e2d\u63d2\u5165\u4e86\u5b57\u7b26\u4e32\u201cHello, World!\u201d\u3002\u7136\u540e\u5c06\u8fd9\u4e2a\u5de5\u4f5c\u7c3f\u4fdd\u5b58\u4e3a<code>example.xlsx<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5df2\u6709\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u73b0\u6709\u7684Excel\u6587\u4ef6\u4e2d\u63d2\u5165\u6570\u636e\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u6253\u5f00\u5df2\u6709\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>wb = load_workbook(&#39;example.xlsx&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6d3b\u52a8\u7684\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u5728\u5355\u5143\u683cB1\u4e2d\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>ws[&#39;B1&#39;] = &#39;New Data&#39;<\/p>\n<h2><strong>\u4fdd\u5b58\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>wb.save(&#39;example.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86<code>example.xlsx<\/code>\u6587\u4ef6\uff0c\u5e76\u5728\u5355\u5143\u683cB1\u4e2d\u63d2\u5165\u4e86\u5b57\u7b26\u4e32\u201cNew Data\u201d\u3002\u7136\u540e\u4fdd\u5b58\u4e86\u8fd9\u4e2a\u5de5\u4f5c\u7c3f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u867d\u7136\u5b83\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5206\u6790\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u63d2\u5165\u5230Excel\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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>2. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>pandas<\/code>\u5e93\u521b\u5efaExcel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;Column1&#39;: [1, 2, 3], &#39;Column2&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]})<\/p>\n<h2><strong>\u5c06\u6570\u636e\u6846\u4fdd\u5b58\u5230Excel\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>df.to_excel(&#39;example_pandas.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u5217\u6570\u636e\u7684\u6570\u636e\u6846\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u4e3a<code>example_pandas.xlsx<\/code>\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5df2\u6709\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u73b0\u6709\u7684Excel\u6587\u4ef6\u4e2d\u63d2\u5165\u6570\u636e\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>pandas<\/code>\u5e93\u5728\u73b0\u6709\u7684Excel\u6587\u4ef6\u4e2d\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6253\u5f00\u5df2\u6709\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df_existing = pd.read_excel(&#39;example_pandas.xlsx&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df_new = pd.DataFrame({&#39;Column1&#39;: [4, 5], &#39;Column2&#39;: [&#39;D&#39;, &#39;E&#39;]})<\/p>\n<h2><strong>\u5c06\u65b0\u7684\u6570\u636e\u6846\u8ffd\u52a0\u5230\u5df2\u6709\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df_combined = pd.concat([df_existing, df_new])<\/p>\n<h2><strong>\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u6846\u4fdd\u5b58\u5230Excel\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>df_combined.to_excel(&#39;example_pandas.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86<code>example_pandas.xlsx<\/code>\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002\u7136\u540e\u5c06\u65b0\u7684\u6570\u636e\u6846\u8ffd\u52a0\u5230\u5df2\u6709\u7684\u6570\u636e\u6846\u4e2d\uff0c\u5e76\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u6846\u4fdd\u5b58\u56de<code>example_pandas.xlsx<\/code>\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528xlsxwriter\u5e93\u63d2\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>xlsxwriter<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u548c\u5199\u5165Excel\u6587\u4ef6\u7684\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u56fe\u8868\u3001\u683c\u5f0f\u5316\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5xlsxwriter\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>xlsxwriter<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xlsxwriter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>xlsxwriter<\/code>\u5e93\u521b\u5efaExcel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e\u7684\u4f8b\u5b50\uff1a<\/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<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;example_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>\u5728\u5355\u5143\u683cA1\u4e2d\u63d2\u5165\u6570\u636e<\/strong><\/h2>\n<p>worksheet.write(&#39;A1&#39;, &#39;Hello, World!&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\uff0c\u5e76\u5728\u5355\u5143\u683cA1\u4e2d\u63d2\u5165\u4e86\u5b57\u7b26\u4e32\u201cHello, World!\u201d\u3002\u7136\u540e\u5173\u95ed\u4e86\u5de5\u4f5c\u7c3f\u3002<\/p>\n<\/p>\n<p><h4>3. \u4fee\u6539\u5df2\u6709\u7684Excel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>xlsxwriter<\/code>\u5e93\u4e0d\u652f\u6301\u76f4\u63a5\u4fee\u6539\u73b0\u6709\u7684Excel\u6587\u4ef6\uff0c\u4f46\u53ef\u4ee5\u8bfb\u53d6\u73b0\u6709\u6587\u4ef6\u7684\u6570\u636e\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import xlsxwriter<\/p>\n<h2><strong>\u8bfb\u53d6\u5df2\u6709\u7684Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/strong><\/h2>\n<p>df_existing = pd.read_excel(&#39;example_pandas.xlsx&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df_new = pd.DataFrame({&#39;Column1&#39;: [4, 5], &#39;Column2&#39;: [&#39;D&#39;, &#39;E&#39;]})<\/p>\n<h2><strong>\u5c06\u65b0\u7684\u6570\u636e\u6846\u8ffd\u52a0\u5230\u5df2\u6709\u7684\u6570\u636e\u6846<\/strong><\/h2>\n<p>df_combined = pd.concat([df_existing, df_new])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>workbook = xlsxwriter.Workbook(&#39;example_xlsxwriter_combined.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\u5408\u5e76\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>for row_num, (index, row) in enumerate(df_combined.iterrows()):<\/p>\n<p>    for col_num, value in enumerate(row):<\/p>\n<p>        worksheet.write(row_num, col_num, value)<\/p>\n<h2><strong>\u5173\u95ed\u5de5\u4f5c\u7c3f<\/strong><\/h2>\n<p>workbook.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86<code>example_pandas.xlsx<\/code>\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u6846\u3002\u7136\u540e\u5c06\u65b0\u7684\u6570\u636e\u6846\u8ffd\u52a0\u5230\u5df2\u6709\u7684\u6570\u636e\u6846\u4e2d\uff0c\u5e76\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6<code>example_xlsxwriter_combined.xlsx<\/code>\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528xlrd\u5e93\u8bfb\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>xlrd<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\u7684\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5xlrd\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<code>xlrd<\/code>\u5e93\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xlrd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>xlrd<\/code>\u5e93\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u6570\u636e\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<h2><strong>\u6253\u5f00\u5df2\u6709\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = xlrd.open_workbook(&#39;example.xlsx&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>worksheet = workbook.sheet_by_index(0)<\/p>\n<h2><strong>\u8bfb\u53d6\u5355\u5143\u683cA1\u7684\u6570\u636e<\/strong><\/h2>\n<p>cell_value = worksheet.cell_value(0, 0)<\/p>\n<p>print(cell_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6253\u5f00\u4e86<code>example.xlsx<\/code>\u6587\u4ef6\uff0c\u5e76\u83b7\u53d6\u4e86\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8868\u3002\u7136\u540e\u8bfb\u53d6\u4e86\u5355\u5143\u683cA1\u4e2d\u7684\u6570\u636e\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528Python\u5728Excel\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\u7684\u51e0\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u5305\u62ec<code>openpyxl<\/code>\u3001<code>pandas<\/code>\u3001<code>xlsxwriter<\/code>\u548c<code>xlrd<\/code>\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff1a<strong><code>openpyxl<\/code>\u5e93\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u64cd\u4f5c\uff1b<code>pandas<\/code>\u5e93\u5f3a\u5927\u4e14\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\uff1b<code>xlsxwriter<\/code>\u5e93\u9002\u5408\u521b\u5efa\u548c\u4fee\u6539Excel\u6587\u4ef6\u5e76\u63d0\u4f9b\u9ad8\u7ea7\u529f\u80fd\uff1b<code>xlrd<\/code>\u5e93\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\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\u5728Python\u4e2d\u63d2\u5165\u6570\u636e\u5230Excel\u5355\u5143\u683c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u63d2\u5165\u6570\u636e\u5230Excel\u5355\u5143\u683c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install openpyxl<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6253\u5f00\u4e00\u4e2aExcel\u6587\u4ef6\u5e76\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import openpyxl\n\n# \u6253\u5f00\u5de5\u4f5c\u7c3f\nworkbook = openpyxl.load_workbook(&#39;example.xlsx&#39;)\nsheet = workbook.active\n\n# \u63d2\u5165\u6570\u636e\u5230\u6307\u5b9a\u5355\u5143\u683c\nsheet[&#39;A1&#39;] = &#39;Hello, World!&#39;\n\n# \u4fdd\u5b58\u6587\u4ef6\nworkbook.save(&#39;example.xlsx&#39;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528pandas\u5e93\u63d2\u5165\u6570\u636e\u5230Excel\u5355\u5143\u683c\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4f7f\u7528<code>pandas<\/code>\u5e93\u4e5f\u662f\u4e00\u79cd\u4fbf\u6377\u7684\u65b9\u6cd5\u6765\u5904\u7406Excel\u6587\u4ef6\u3002\u9996\u5148\u786e\u4fdd\u5b89\u88c5\u4e86<code>pandas<\/code>\u548c<code>openpyxl<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install pandas openpyxl<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u8bfb\u53d6Excel\u6587\u4ef6\ndf = pd.read_excel(&#39;example.xlsx&#39;)\n\n# \u4fee\u6539\u6570\u636e\u6846\u67b6\ndf.at[0, &#39;Column1&#39;] = &#39;Hello, World!&#39;\n\n# \u4fdd\u5b58\u56deExcel\ndf.to_excel(&#39;example.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406Excel\u6587\u4ef6\u4e2d\u7684\u591a\u4e2a\u5355\u5143\u683c\u6570\u636e\u63d2\u5165\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u9700\u8981\u5728\u591a\u4e2a\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6216\u5217\u8868\u6765\u8fdb\u884c\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u591a\u4e2a\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-python\">import openpyxl\n\n# \u6253\u5f00\u5de5\u4f5c\u7c3f\nworkbook = openpyxl.load_workbook(&#39;example.xlsx&#39;)\nsheet = workbook.active\n\n# \u63d2\u5165\u591a\u4e2a\u5355\u5143\u683c\u7684\u6570\u636e\ndata = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]\nfor index, value in enumerate(data, start=1):\n    sheet[f&#39;A{index}&#39;] = value\n\n# \u4fdd\u5b58\u6587\u4ef6\nworkbook.save(&#39;example.xlsx&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u63d2\u5165\u6570\u636e\u5230Excel\u5355\u5143\u683c\u4e2d\uff0c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u5728\u5355\u5143\u683c\u63d2\u5165\u6570\u636e\uff1a\u4f7f\u7528Python\u5728Excel\u5355\u5143\u683c\u4e2d\u63d2\u5165\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u5e93\u6765\u5b9e\u73b0\uff0c\u4f8b\u5982ope [&hellip;]","protected":false},"author":3,"featured_media":1046320,"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\/1046316"}],"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=1046316"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046316\/revisions"}],"predecessor-version":[{"id":1046322,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046316\/revisions\/1046322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1046320"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1046316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1046316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1046316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}