{"id":989259,"date":"2024-12-27T08:10:01","date_gmt":"2024-12-27T00:10:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/989259.html"},"modified":"2024-12-27T08:10:06","modified_gmt":"2024-12-27T00:10:06","slug":"python%e5%86%99%e5%85%a5excel%e5%a6%82%e4%bd%95%e6%b8%85%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/989259.html","title":{"rendered":"python\u5199\u5165Excel\u5982\u4f55\u6e05\u7a7a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064535\/ad20f2b2-f774-41a4-98d2-e702a3ebf94b.webp\" alt=\"python\u5199\u5165Excel\u5982\u4f55\u6e05\u7a7a\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5199\u5165Excel\u5e76\u6e05\u7a7a\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528openpyxl\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528openpyxl\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u76f4\u63a5\u64cd\u4f5cExcel\u7684\u5de5\u4f5c\u8868\u548c\u5355\u5143\u683c\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6e05\u9664\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u6570\u636e<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u6e05\u7a7aExcel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528OPENPYXL\u5e93<\/p>\n<\/p>\n<p><p>openpyxl\u662f\u4e00\u4e2a\u7528\u4e8e\u8bfb\u5199Excel 2010 xlsx\/xlsm\/xltx\/xltm\u6587\u4ef6\u7684Python\u5e93\u3002\u4f7f\u7528openpyxl\u53ef\u4ee5\u8f7b\u677e\u5730\u6e05\u7a7aExcel\u5de5\u4f5c\u8868\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u5bfc\u5165openpyxl<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528openpyxl\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165openpyxl\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u52a0\u8f7d\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528openpyxl\u52a0\u8f7d\u73b0\u6709\u7684Excel\u5de5\u4f5c\u7c3f\uff0c\u5e76\u9009\u62e9\u8981\u6e05\u7a7a\u7684\u5de5\u4f5c\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u52a0\u8f7dExcel\u5de5\u4f5c\u7c3f<\/p>\n<p>workbook = openpyxl.load_workbook(&#39;example.xlsx&#39;)<\/p>\n<h2><strong>\u9009\u62e9\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>worksheet = workbook[&#39;Sheet1&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u6e05\u7a7a\u5de5\u4f5c\u8868\u5185\u5bb9<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u904d\u5386\u5de5\u4f5c\u8868\u7684\u6240\u6709\u884c\u548c\u5217\uff0c\u9010\u4e2a\u6e05\u7a7a\u5355\u5143\u683c\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u6240\u6709\u884c\u548c\u5217\uff0c\u6e05\u7a7a\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u5185\u5bb9<\/p>\n<p>for row in worksheet.iter_rows():<\/p>\n<p>    for cell in row:<\/p>\n<p>        cell.value = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u4fdd\u5b58\u66f4\u6539<\/li>\n<\/ol>\n<p><p>\u6700\u540e\uff0c\u5c06\u66f4\u6539\u4fdd\u5b58\u5230\u539f\u6587\u4ef6\u6216\u53e6\u5b58\u4e3a\u65b0\u7684Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u66f4\u6539<\/p>\n<p>workbook.save(&#39;example.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528PANDAS\u5e93<\/p>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8eExcel\u6587\u4ef6\u7684\u64cd\u4f5c\u3002\u4f7f\u7528pandas\u53ef\u4ee5\u5feb\u901f\u6e05\u7a7aExcel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u548c\u5bfc\u5165pandas<\/li>\n<\/ol>\n<p><p>\u9996\u5148\u5b89\u88c5pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165pandas\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6e05\u7a7aExcel\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528pandas\u7684ExcelWriter\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u7a7a\u7684\u6570\u636e\u6846\u5199\u5165Excel\u6587\u4ef6\uff0c\u4ece\u800c\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6570\u636e\u6846<\/p>\n<p>df_empty = pd.DataFrame()<\/p>\n<h2><strong>\u4f7f\u7528ExcelWriter\u6e05\u7a7aExcel\u6587\u4ef6<\/strong><\/h2>\n<p>with pd.ExcelWriter(&#39;example.xlsx&#39;) as writer:<\/p>\n<p>    df_empty.to_excel(writer, sheet_name=&#39;Sheet1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u9700\u8981\u4fdd\u7559\u539f\u6709\u7684Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7a7a\u767dExcel\u6587\u4ef6\u8986\u76d6\u539f\u6587\u4ef6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528openpyxl\u521b\u5efa\u65b0\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f<\/p>\n<p>new_workbook = openpyxl.Workbook()<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3a\u65b0\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>new_workbook.save(&#39;example.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528pandas\u521b\u5efa\u65b0\u6587\u4ef6<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u6570\u636e\u6846<\/p>\n<p>df_empty = pd.DataFrame()<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3a\u7a7a\u767d\u7684Excel\u6587\u4ef6<\/strong><\/h2>\n<p>df_empty.to_excel(&#39;example.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u6e05\u7a7aExcel\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u5bf9Excel\u6587\u4ef6\u7684\u683c\u5f0f\u6216\u5185\u5bb9\u6709\u7279\u5b9a\u8981\u6c42\uff0c\u4f7f\u7528openpyxl\u53ef\u80fd\u4f1a\u66f4\u52a0\u7075\u6d3b\uff0c\u800cpandas\u5219\u9002\u5408\u4e8e\u5feb\u901f\u64cd\u4f5c\u5927\u6570\u636e\u96c6\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u6e05\u7a7aExcel\u6587\u4ef6\u524d\u90fd\u5e94\u786e\u4fdd\u5907\u4efd\u6570\u636e\uff0c\u4ee5\u514d\u8bef\u64cd\u4f5c\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6e05\u7a7aExcel\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u5355\u5143\u683c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u5e93\u6765\u6e05\u7a7a\u7279\u5b9a\u5355\u5143\u683c\u7684\u5185\u5bb9\u3002\u9996\u5148\uff0c\u6253\u5f00Excel\u6587\u4ef6\uff0c\u9009\u62e9\u5de5\u4f5c\u8868\uff0c\u7136\u540e\u5c06\u60f3\u8981\u6e05\u7a7a\u7684\u5355\u5143\u683c\u7684\u503c\u8bbe\u7f6e\u4e3a<code>None<\/code>\u3002\u6700\u540e\uff0c\u4fdd\u5b58\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from openpyxl import load_workbook\n\nworkbook = load_workbook(&#39;example.xlsx&#39;)\nsheet = workbook.active\nsheet[&#39;A1&#39;] = None  # \u6e05\u7a7aA1\u5355\u5143\u683c\nworkbook.save(&#39;example.xlsx&#39;)\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Pandas\u5e93\u5982\u4f55\u6e05\u7a7a\u6574\u4e2aExcel\u8868\u683c\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u5e0c\u671b\u6e05\u7a7a\u6574\u4e2aExcel\u8868\u683c\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u3002\u8bfb\u53d6Excel\u6587\u4ef6\u540e\uff0c\u5c06DataFrame\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u7a7a\u7684DataFrame\uff0c\u7136\u540e\u5c06\u5176\u5199\u56de\u5230Excel\u6587\u4ef6\u4e2d\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\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# \u6e05\u7a7a\u6570\u636e\ndf = pd.DataFrame()\n# \u5c06\u7a7a\u7684DataFrame\u5199\u56deExcel\ndf.to_excel(&#39;example.xlsx&#39;, index=False)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u6e05\u7a7aExcel\u6587\u4ef6\u540e\uff0c\u5982\u4f55\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u4e0d\u53d7\u5f71\u54cd\uff1f<\/strong><br \/>\u6e05\u7a7aExcel\u6587\u4ef6\u5185\u5bb9\u65f6\uff0c\u786e\u4fdd\u6587\u4ef6\u683c\u5f0f\u4e0d\u53d7\u5f71\u54cd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>openpyxl<\/code>\u6216<code>xlsxwriter<\/code>\u7b49\u5e93\u6765\u5904\u7406\u3002\u8fd9\u4e9b\u5e93\u80fd\u591f\u4fdd\u7559Excel\u6587\u4ef6\u7684\u683c\u5f0f\u548c\u6837\u5f0f\u3002\u5728\u6e05\u7a7a\u6570\u636e\u7684\u540c\u65f6\uff0c\u4e0d\u8981\u4f7f\u7528<code>to_excel<\/code>\u65b9\u6cd5\u8986\u76d6\u5df2\u6709\u7684\u683c\u5f0f\uff0c\u6216\u8005\u5728\u521b\u5efa\u65b0\u6587\u4ef6\u65f6\u5e94\u7528\u76f8\u540c\u7684\u683c\u5f0f\u8bbe\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5199\u5165Excel\u5e76\u6e05\u7a7a\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528openpyxl\u5e93\u3001\u4f7f\u7528pandas\u5e93\u3001\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 [&hellip;]","protected":false},"author":3,"featured_media":989268,"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\/989259"}],"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=989259"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989259\/revisions"}],"predecessor-version":[{"id":989271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/989259\/revisions\/989271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/989268"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=989259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=989259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=989259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}