{"id":5450,"date":"2024-07-07T18:04:35","date_gmt":"2024-07-07T10:04:35","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=5450"},"modified":"2024-07-07T18:04:36","modified_gmt":"2024-07-07T10:04:36","slug":"python%e6%89%93%e5%bc%80excel%e6%96%87%e6%a1%a3%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=5450","title":{"rendered":"Python\u6253\u5f00Excel\u6587\u6863\u5e76\u8bfb\u53d6\u6570\u636e"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Python \u7248\u672c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u76ee\u524d Python 3 \u7248\u672c\u4e3a\u4e3b\u6d41\u7248\u672c\uff0c\u8fd9\u91cc\u6d4b\u8bd5\u7684\u7248\u672c\u662f\uff1aPython 3.10.5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u7528\u5e93\u8bf4\u660e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python \u64cd\u4f5c Excel \u7684\u5e38\u7528\u5e93\u6709\uff1axlrd\u3001xlwt\u3001xlutils\u3001openpyxl\u3001pandas\u3002\u8fd9\u91cc\u4e3b\u8981\u8bf4\u660e\u4e0b Excel \u6587\u6863 <strong>.xls<\/strong> \u683c\u5f0f\u548c <strong>.xlsx<\/strong> \u683c\u5f0f\u7684\u6587\u6863\u6253\u5f00\u548c\u8bfb\u53d6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python \u64cd\u4f5c .xls \u683c\u5f0f\u7684 Excel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u8003\u7f51\u5740\uff1a<a href=\"https:\/\/xlrd.readthedocs.io\/en\/latest\/\" target=\"_blank\" rel=\"noreferrer noopener\">xlrd \u2014 xlrd 2.0.1 documentationhttps:\/\/xlrd.readthedocs.io\/en\/latest\/<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlrd\n\n# \u5b9a\u4e49\u6587\u4ef6\u8def\u5f84\u5e76\u6253\u5f00\u6587\u4ef6\nfilePath = r'C:\\Users\\mengll\\Desktop\\\u6d4b\u8bd5\u7684EXCEL\u6587\u6863.xls'\nfile = xlrd.open_workbook(filePath)\n\n# Excel Sheet \u9875\u6570\u91cf\uff1afile.nsheets\nprint(\"\u8fd9\u4e2a\u8868\u683c\u4e00\u5171\u6709\u7684sheet\u6570\u91cf\u662f\uff1a {0}\".format(file.nsheets))\n# Excel Sheet \u9875\u540d\u5217\u8868\uff1afile.sheet_names()\nprint(\"\u8fd9\u4e2a\u8868\u683csheet\u540d\u662f\uff1a{0}\".format(file.sheet_names()))\n\n# Excel \u7b2c\u4e00\u4e2a Sheet \u9875\uff1afile.sheet_by_index(0)\nsheet0 = file.sheet_by_index(0)\n# Sheet \u9875\u7684\u540d\u79f0\u3001\u884c\u6570\u3001\u5217\u6570\u662f\uff1asheet.name|sheet.nrows|sheet.ncols\nprint(\"{0} {1} {2}\".format(sheet0.name, sheet0.nrows, sheet0.ncols))\n# Sheet \u9875\u6307\u5b9a\u5355\u5143\u683c\u7684\u503c\u662f\uff1asheet.cell_value(rowx=1, colx=1)\nprint(\"\u5355\u5143\u683c A1 \u5185\u5bb9\u4e3a\uff1a{0}\".format(sheet0.cell_value(rowx=0, colx=0)))\n\n# \u5faa\u73af\u6253\u5370 Sheet \u7684\u6240\u6709\u884c\u6570\u636e\nfor rx in range(sheet0.nrows):\n    print(sheet0.row(rx))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Python \u64cd\u4f5c .xlsx \u683c\u5f0f\u7684 Excel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u53c2\u8003\u7f51\u5740\uff1a<a href=\"https:\/\/openpyxl.readthedocs.io\/en\/stable\/\">openpyxl &#8211; A Python library to read\/write Excel 2010 xlsx\/xlsm files \u2014 openpyxl 3.1.3 documentationhttps:\/\/openpyxl.readthedocs.io\/en\/stable\/<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import openpyxl\n\n# \u5b9a\u4e49\u6587\u4ef6\u8def\u5f84\u5e76\u6253\u5f00\u6587\u4ef6\nfilePath = r'C:\\Users\\mengll\\Desktop\\\u6d4b\u8bd5\u7684EXCEL\u6587\u6863.xlsx'\nfile = openpyxl.load_workbook(filePath)\n\n# Excel Sheet \u9875\u540d\u5217\u8868\uff1afile.sheetnames\nprint(\"\u8fd9\u4e2a\u8868\u683csheet\u540d\u662f\uff1a{0}\".format(file.sheetnames))\nprint(\"\u8fd9\u4e2a\u8868\u683csheet\u540d\u662f\uff1a{0}\".format(file.worksheets))\n\n# Excel \u7b2c\u4e00\u4e2a Sheet \u9875\uff1afile&#91;'sheet0']\nsheet = file&#91;'sheet0']\n# Sheet \u9875\u7684\u540d\u79f0\u3001\u5de6\u4e0a\u53f3\u4e0b\u5355\u5143\u683c\u3001\u884c\u6570\u3001\u5217\u6570\u662f\uff1asheet.title|sheet.dimensions|sheet.max_row|sheet.min_row|sheet.max_column|sheet.min_column\nprint(\"{0} {1} {2} {3}\".format(sheet.title, sheet.dimensions, sheet.max_row, sheet.max_column))\n# Sheet \u9875\u6307\u5b9a\u5355\u5143\u683c\u7684\u503c\u662f\uff1asheet.cell(row=1, column=1).value\nprint(\"\u5355\u5143\u683c A1 \u5185\u5bb9\u4e3a\uff1a{0}\".format(sheet.cell(row=1, column=1).value))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5de5\u5177\u4e2d\u5176\u5b83\u5f88\u591a\u5c5e\u6027\u548c\u7528\u6cd5\uff0c\u53ef\u4ee5\u53c2\u8003\u6587\u6863\u81ea\u884c\u5c1d\u8bd5\uff0cGood Luck~ \u200b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/menglanglang.blog.csdn.net\/article\/details\/140249393\">https:\/\/menglanglang.blog.csdn.net\/article\/details\/140249393<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7248\u672c \u76ee\u524d Python 3 \u7248\u672c\u4e3a\u4e3b\u6d41\u7248\u672c\uff0c\u8fd9\u91cc\u6d4b\u8bd5\u7684\u7248\u672c\u662f\uff1aP &hellip; <a href=\"http:\/\/www.menglanglang.cn\/?p=5450\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","wpai_meta_description":"","footnotes":""},"categories":[812],"tags":[700,934,813,933,935,936,110],"class_list":["post-5450","post","type-post","status-publish","format-standard","hentry","category-python","tag-excel","tag-openpyxl","tag-python","tag-xlrd","tag-xls","tag-xlsx","tag-110"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5450"}],"version-history":[{"count":1,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5450\/revisions"}],"predecessor-version":[{"id":5451,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/5450\/revisions\/5451"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5450"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}