{"id":1170127,"date":"2025-01-15T16:18:44","date_gmt":"2025-01-15T08:18:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170127.html"},"modified":"2025-01-15T16:18:46","modified_gmt":"2025-01-15T08:18:46","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5excel%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170127.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165excel\u4ee3\u7801\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070923\/482bcb9e-470c-4f7f-9f6f-a755041906d9.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165excel\u4ee3\u7801\u5b9e\u73b0\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5bfc\u5165Excel\u6587\u4ef6\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlrd\u5e93\u3002<\/strong>\u5176\u4e2d\uff0cpandas\u5e93\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528pandas\u5e93\u5bfc\u5165Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528pandas\u5e93\u5bfc\u5165Excel\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u51e0\u884c\u4ee3\u7801\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/strong><\/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\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u524d\u4e94\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165\u4e86pandas\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>pd.read_excel<\/code>\u51fd\u6570\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u6587\u4ef6\u8def\u5f84\u9700\u8981\u66ff\u6362\u4e3a\u5b9e\u9645\u6587\u4ef6\u7684\u8def\u5f84\u3002\u8bfb\u53d6\u7684Excel\u6587\u4ef6\u4f1a\u5b58\u50a8\u5728\u4e00\u4e2aDataFrame\u5bf9\u8c61\u4e2d\uff0c\u4f7f\u7528<code>df.head()<\/code>\u53ef\u4ee5\u67e5\u770b\u5bfc\u5165\u7684\u6570\u636e\u524d\u4e94\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pandas\u5e93\u5bfc\u5165Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>pandas\u5e93\u4e0d\u4ec5\u652f\u6301\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u6307\u5b9a\u5de5\u4f5c\u8868<\/h4>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>pd.read_excel<\/code>\u51fd\u6570\u4f1a\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5de5\u4f5c\u8868\u3002\u5982\u679c\u9700\u8981\u8bfb\u53d6\u5176\u4ed6\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u5de5\u4f5c\u8868\u540d\u79f0\u6216\u7d22\u5f15\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;)<\/p>\n<h2><strong>\u6216\u8005\u4f7f\u7528\u5de5\u4f5c\u8868\u7d22\u5f15\uff08\u4ece0\u5f00\u59cb\u8ba1\u6570\uff09<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u591a\u5f20\u5de5\u4f5c\u8868<\/h4>\n<\/p>\n<p><p>\u5982\u679cExcel\u6587\u4ef6\u4e2d\u5305\u542b\u591a\u5f20\u5de5\u4f5c\u8868\uff0c\u5e76\u4e14\u9700\u8981\u4e00\u6b21\u6027\u8bfb\u53d6\u6240\u6709\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u5c06<code>sheet_name<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>None<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dfs = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, sheet_name=None)<\/p>\n<h2><strong>dfs\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u662f\u5de5\u4f5c\u8868\u540d\u79f0\uff0c\u503c\u662f\u5bf9\u5e94\u7684DataFrame<\/strong><\/h2>\n<p>for sheet_name, df in dfs.items():<\/p>\n<p>    print(f&#39;\u5de5\u4f5c\u8868: {sheet_name}&#39;)<\/p>\n<p>    print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6307\u5b9a\u6570\u636e\u8303\u56f4<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u53ea\u9700\u8981\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u90e8\u5206\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>usecols<\/code>\u548c<code>skiprows<\/code>\u53c2\u6570\u6307\u5b9a\u5217\u548c\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6307\u5b9a\u5217<\/p>\n<p>df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, usecols=&#39;A:C&#39;)<\/p>\n<h2><strong>\u8df3\u8fc7\u524d\u4e24\u884c\u6570\u636e<\/strong><\/h2>\n<p>df = pd.read_excel(&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;, skiprows=2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528openpyxl\u5e93\u5bfc\u5165Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>openpyxl\u5e93\u662f\u4e13\u95e8\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\u7684\u5e93\uff0c\u652f\u6301\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86openpyxl\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>\u4ee5\u4e0b\u662f\u4f7f\u7528openpyxl\u5e93\u5bfc\u5165Excel\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import load_workbook<\/p>\n<h2><strong>\u52a0\u8f7dExcel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = load_workbook(filename=&#39;\u6587\u4ef6\u8def\u5f84.xlsx&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5de5\u4f5c\u8868\u540d\u79f0<\/strong><\/h2>\n<p>sheet_names = workbook.sheetnames<\/p>\n<p>print(sheet_names)<\/p>\n<h2><strong>\u9009\u62e9\u4e00\u4e2a\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>sheet = workbook[sheet_names[0]]<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row in sheet.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u5355\u5143\u683c\u6570\u636e<\/h4>\n<\/p>\n<p><p>openpyxl\u5e93\u5141\u8bb8\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u8bfb\u53d6\u5355\u5143\u683c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6A1\u5355\u5143\u683c\u6570\u636e<\/p>\n<p>cell_value = sheet[&#39;A1&#39;].value<\/p>\n<p>print(cell_value)<\/p>\n<h2><strong>\u8bfb\u53d6\u7b2c2\u884c\u7b2c3\u5217\u6570\u636e<\/strong><\/h2>\n<p>cell_value = sheet.cell(row=2, column=3).value<\/p>\n<p>print(cell_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6307\u5b9a\u8303\u56f4\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>iter_rows<\/code>\u65b9\u6cd5\u6307\u5b9a\u8bfb\u53d6\u7684\u6570\u636e\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6A1\u5230C3\u8303\u56f4\u7684\u6570\u636e<\/p>\n<p>for row in sheet.iter_rows(min_row=1, max_row=3, min_col=1, max_col=3, values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528xlrd\u5e93\u5bfc\u5165Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>xlrd\u5e93\u4e3b\u8981\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u7279\u522b\u662fxls\u683c\u5f0f\u7684\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86xlrd\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install xlrd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528xlrd\u5e93\u5bfc\u5165Excel\u6587\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<h2><strong>\u6253\u5f00Excel\u6587\u4ef6<\/strong><\/h2>\n<p>workbook = xlrd.open_workbook(&#39;\u6587\u4ef6\u8def\u5f84.xls&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>sheet = workbook.sheet_by_index(0)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row_idx in range(sheet.nrows):<\/p>\n<p>    row = sheet.row(row_idx)<\/p>\n<p>    print([cell.value for cell in row])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u5355\u5143\u683c\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u884c\u5217\u7d22\u5f15\u8bfb\u53d6\u5355\u5143\u683c\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7b2c1\u884c\u7b2c1\u5217\u6570\u636e<\/p>\n<p>cell_value = sheet.cell_value(rowx=0, colx=0)<\/p>\n<p>print(cell_value)<\/p>\n<h2><strong>\u8bfb\u53d6\u7b2c2\u884c\u7b2c3\u5217\u6570\u636e<\/strong><\/h2>\n<p>cell_value = sheet.cell(1, 2).value<\/p>\n<p>print(cell_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bfb\u53d6\u6307\u5b9a\u8303\u56f4\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u8bfb\u53d6\u6307\u5b9a\u8303\u56f4\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u7b2c2\u5230\u7b2c4\u884c\uff0c\u7b2c1\u5230\u7b2c3\u5217\u6570\u636e<\/p>\n<p>for row_idx in range(1, 4):<\/p>\n<p>    row = sheet.row_slice(rowx=row_idx, start_colx=0, end_colx=3)<\/p>\n<p>    print([cell.value for cell in row])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001pandas\u5e93\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u8bfb\u53d6\u64cd\u4f5c\uff0cpandas\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5904\u7406Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bfb\u53d6\u591a\u4e2aExcel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u8bfb\u53d6\u591a\u4e2aExcel\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u5408\u5e76\u5230\u4e00\u4e2aDataFrame\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;\u6587\u4ef6\u5939\u8def\u5f84&#39;<\/p>\n<p>all_data = pd.DataFrame()<\/p>\n<p>for file in os.listdir(file_path):<\/p>\n<p>    if file.endswith(&#39;.xlsx&#39;):<\/p>\n<p>        df = pd.read_excel(os.path.join(file_path, file))<\/p>\n<p>        all_data = all_data.append(df, ignore_index=True)<\/p>\n<p>print(all_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165Excel\u6587\u4ef6\u540e\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7f3a\u5931\u503c\u3002pandas\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u7f3a\u5931\u503c\u60c5\u51b5<\/p>\n<p>print(df.isnull().sum())<\/p>\n<h2><strong>\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>df.dropna(inplace=True)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>df.fillna(value=0, inplace=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u7b5b\u9009\u4e0e\u6392\u5e8f<\/h4>\n<\/p>\n<p><p>\u5bfc\u5165Excel\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u5e76\u8fdb\u884c\u6392\u5e8f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7b5b\u9009\u6570\u636e<\/p>\n<p>filtered_df = df[df[&#39;\u5217\u540d&#39;] &gt; \u67d0\u4e2a\u503c]<\/p>\n<h2><strong>\u6392\u5e8f\u6570\u636e<\/strong><\/h2>\n<p>sorted_df = df.sort_values(by=&#39;\u5217\u540d&#39;, ascending=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001Excel\u6587\u4ef6\u7684\u5199\u5165\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8bfb\u53d6Excel\u6587\u4ef6\uff0cpandas\u5e93\u8fd8\u652f\u6301\u5c06\u6570\u636e\u5199\u5165Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5199\u5165\u5355\u4e2a\u5de5\u4f5c\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u5c06DataFrame\u5199\u5165Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5199\u5165\u5355\u4e2a\u5de5\u4f5c\u8868<\/p>\n<p>df.to_excel(&#39;\u8f93\u51fa\u6587\u4ef6\u8def\u5f84.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5199\u5165\u591a\u4e2a\u5de5\u4f5c\u8868<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>ExcelWriter<\/code>\u5bf9\u8c61\u5c06\u591a\u4e2aDataFrame\u5199\u5165\u540c\u4e00\u4e2aExcel\u6587\u4ef6\u4e2d\u7684\u4e0d\u540c\u5de5\u4f5c\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with pd.ExcelWriter(&#39;\u8f93\u51fa\u6587\u4ef6\u8def\u5f84.xlsx&#39;) as writer:<\/p>\n<p>    df1.to_excel(writer, sheet_name=&#39;\u5de5\u4f5c\u88681&#39;, index=False)<\/p>\n<p>    df2.to_excel(writer, sheet_name=&#39;\u5de5\u4f5c\u88682&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86Python\u4e2d\u5982\u4f55\u5bfc\u5165Excel\u6587\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\u3002<strong>pandas\u5e93\u662f\u6700\u5e38\u7528\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u652f\u6301\u8bfb\u53d6\u3001\u5199\u5165\u3001\u6570\u636e\u6e05\u6d17\u548c\u5206\u6790\u7b49\u64cd\u4f5c<\/strong>\u3002\u9664\u4e86pandas\u5e93\uff0copenpyxl\u548cxlrd\u5e93\u4e5f\u63d0\u4f9b\u4e86\u7075\u6d3b\u7684Excel\u6587\u4ef6\u5904\u7406\u529f\u80fd\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548cExcel\u6587\u4ef6\u7684\u683c\u5f0f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5e93\uff0c\u5145\u5206\u53d1\u6325\u5b83\u4eec\u7684\u4f18\u52bf\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5bfc\u5165Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5bfc\u5165Excel\u6587\u4ef6\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8f7b\u677e\u8bfb\u53d6Excel\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u8bfb\u53d6Excel\u6587\u4ef6\ndf = pd.read_excel(&#39;your_file.xlsx&#39;, sheet_name=&#39;Sheet1&#39;)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u5728\u8fd0\u884c\u6b64\u4ee3\u7801\u4e4b\u524d\u5b89\u88c5\u4e86<code>pandas<\/code>\u548c<code>openpyxl<\/code>\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pandas openpyxl<\/code>\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5bfc\u5165Excel\u6570\u636e\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528<code>pandas<\/code>\u5bfc\u5165Excel\u6570\u636e\u540e\uff0c\u53ef\u4ee5\u5229\u7528<code>dropna()<\/code>\u6216<code>fillna()<\/code>\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5220\u9664\u7f3a\u5931\u503c\ndf_cleaned = df.dropna()\n\n# \u7528\u7279\u5b9a\u503c\u586b\u5145\u7f3a\u5931\u503c\ndf_filled = df.fillna(0)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u60a8\u7684\u6570\u636e\u5206\u6790\u5728\u6570\u636e\u5b8c\u6574\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u3002<\/p>\n<p><strong>Python\u5bfc\u5165Excel\u540e\uff0c\u5982\u4f55\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff1f<\/strong><br \/>\u4e00\u65e6\u60a8\u6210\u529f\u5bfc\u5165Excel\u6570\u636e\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u5176\u8f6c\u6362\u4e3a\u5176\u4ed6\u683c\u5f0f\uff0c\u4f8b\u5982CSV\u6216JSON\u3002\u4ee5\u4e0b\u662f\u8f6c\u6362\u4e3aCSV\u683c\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">df.to_csv(&#39;output_file.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u540c\u6837\uff0c\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">df.to_json(&#39;output_file.json&#39;, orient=&#39;records&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u548c\u4f7f\u7528\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5bfc\u5165Excel\u6587\u4ef6\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u6709\u4f7f\u7528pandas\u5e93\u3001openpyxl\u5e93\u3001xlrd\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1170137,"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\/1170127"}],"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=1170127"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170127\/revisions"}],"predecessor-version":[{"id":1170139,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170127\/revisions\/1170139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170137"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}