{"id":7961,"date":"2024-04-20T22:01:01","date_gmt":"2024-04-20T14:01:01","guid":{"rendered":""},"modified":"2024-04-20T22:01:01","modified_gmt":"2024-04-20T14:01:01","slug":"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7961.html","title":{"rendered":"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u5728\u5b9e\u9645\u7684\u5de5\u4f5c\u4e2d\u6211\u4eec\u5e38\u5e38\u4f1a\u7528\u5230excel\u8868\u683c(\u6216\u8005CSV\u6587\u4ef6)\u6765\u4fdd\u5b58\u6570\u636e\uff0c\u8fd9\u5c31\u4f1a\u6d89\u53ca\u5230\u5bf9\u8868\u683c\u7684\u5927\u91cf\u64cd\u4f5c\uff0c\u5f53\u6709\u8bb8\u591a\u91cd\u590d\u3001\u590d\u6742\u7684\u4e8b\u60c5\u65f6\uff0c\u6211\u4eec\u5c31\u5e94\u5f53\u8003\u8651\u7f16\u5199\u7a0b\u5e8f\u6765\u81ea\u52a8\u5b8c\u6210\u3002\u8bb8\u591a\u8bed\u8a00\u90fd\u63d0\u4f9b\u6709\u8bfb\u5199excel\u6587\u4ef6\u7684API\uff0c\u6211\u5728\u8fd9\u91cc\u4f7f\u7528Python\u6765\u505a\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u5427\u3002<\/p>\n

\u5173\u4e8ePython\u8bfb\u5199excel\u7684\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u7f51\u7ad9 http:\/\/www.python-excel.org\/\u5f97\u5230\u5404\u79cd\u65b9\u6cd5\uff0c\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528xlrd\u548cxlwt\u6765\u64cd\u4f5cexcel\u3002\u4f60\u53ef\u4ee5\u4ecehttps:\/\/pypi.python.org\/pypi\u4e0b\u8f7d\u8fd9\u4e24\u4e2a\u6a21\u5757\uff0c\u6216\u8005\u76f4\u63a5\u4f7f\u7528pip\u547d\u4ee4pip install xlrd(xlwt)<\/code>\u5c06\u8fd9\u4e24\u4e2a\u6a21\u5757\u5b89\u88c5\u5230\u672c\u673a\u5373\u53ef\u3002<\/p>\n

1\u3001Python\u8bfbexcel\u2014xlrd<\/h2>\n

\u6211\u4eec\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u8fd9\u4e2a\u7f51\u7ad9\u5f97\u5230xlrd\u7684\u6700\u65b0API\uff0chttp:\/\/xlrd.readthedocs.io\/en\/latest\/api.html\uff0c\u6253\u5f00\u540e\u53ef\u4ee5\u770b\u5230\u4ee5\u4e0bAPI\u5206\u7c7b\u3002<\/p>\n

\"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

\u8bdd\u4e0d\u591a\u8bf4\uff0c\u6211\u4eec\u6765\u770b\u4e00\u770b\u6700\u57fa\u672c\u7684\u4f7f\u7528\u3002\u9996\u5148\u6211\u4eec\u9700\u8981\u5728Excel\u6216WPS\u4e2d\u521b\u5efa\u4e00\u4e2aexcel\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u5efa\u7acb\u51e0\u4e2asheet\u548c\u6dfb\u52a0\u4e00\u4e9b\u6570\u636e\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n

\"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

\"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

\u5373\uff0c\u6211\u4eec\u8981\u8bfb\u51fa\u6b64Excel\u6587\u4ef6\u4e2d\u7b2c\u4e09\u4e2a\u8868\u683c\u4e2d\u7684\u6570\u636e\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import xlrd\n\ndef read_excel():\n  #\u6253\u5f00Excel\u6587\u4ef6\n  data=xlrd.open_workbook('d:\\TestXlrd.xlsx')\n  #sheet3=data.sheets()[2]\n  #sheet3=data.sheet_by_name('thirdSheet')\n  sheet3=data.sheet_by_index(2)\n  rows=sheet3.nrows\n\n  #\u6253\u5370\u51fa\u6240\u6709sheet\u7684\u540d\u79f0\n  print (data.sheet_names(),'\\n')\n\n  #\u6253\u5370\u51fa\u7b2c\u4e09\u5f20\u8868\u7684\u540d\u79f0\u3001\u884c\u6570\u3001\u5217\u6570\n  print (sheet3.name,sheet3.nrows,sheet3.ncols,'\\n')\n \n  #\u83b7\u53d6\u67d0\u4e2a\u5355\u5143\u683c\u7684\u6570\u503c\n  print(sheet3.cell(2,2))\n  print(sheet3.cell_value(2,2))\n  print(sheet3.row(2)[2].value)\n\n  for i in range(rows):\n    print (sheet3.row_values(i))\n  print('\\n')\n\nif __name__ == '__main__':\n  read_excel()\n<\/code><\/pre>\n

\u8fd0\u884c\u6b64\u4ee3\u7801\uff0c\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n

\"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

\u4ee5\u4e0a\u4ee3\u7801\u975e\u5e38\u7b80\u5355\u4ec5\u4ec5\u7528\u5230\u4e86\u51e0\u4e2a\u57fa\u672c\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u884c\u548c\u5217\u7684\u7d22\u5f15\u90fd\u662f\u4ece0\u5f00\u59cb\u7684\u3002<\/code><\/p>\n

\u6211\u4eec\u540c\u65f6\u53ef\u4ee5\u53d1\u73b0\uff0c\u5f53\u6709\u5408\u5e76\u7684\u5355\u5143\u683c\u65f6\uff0c\u53ea\u80fd\u8bfb\u51fa\u7d22\u5f15\u6700\u5c0f\u7684\u90a3\u4e00\u4e2a\uff0c\u5c06\u53e6\u5916\u7684\u8bfb\u4e3a\u7a7a\u503c\uff0c\u8fd9\u5c31\u4ea7\u751f\u4e86\u4e24\u4e2a\u95ee\u9898\uff1a<\/p>\n

    \n
  1. \u600e\u4e48\u83b7\u53d6\u5408\u5e76\u7684\u5355\u5143\u683c\uff1f<\/li>\n
  2. \u591a\u6570\u60c5\u51b5\u4e0b\u5408\u5e76\u7684\u5355\u5143\u683c\u9700\u8981\u7684\u662f\u540c\u4e00\u4e2a\u503c\uff0c\u6211\u4eec\u600e\u4e48\u53bb\u5904\u7406\u5b83\uff1f<\/li>\n<\/ol>\n

    \u8bf7\u63a5\u7740\u5f80\u4e0b\u770b\u3002<\/p>\n

    \u5408\u5e76\u5355\u5143\u683c\u5206\u6790<\/strong><\/h3>\n

    \u6211\u4eec\u67e5\u8be2xlrd\u7684API\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u5173\u952e\u65b9\u6cd5\uff1a<\/p>\n

    \"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3
    \u53ef\u4ee5\u6e05\u695a\u5730\u770b\u89c1\uff0c\u6b64\u65b9\u6cd5\u662f\u4ee54\u4e2a\u6570\u5b57\u7684\u5f62\u5f0f\u8fd4\u56de\u7684\u662f\u5408\u5e76\u7684\u5355\u5143\u683c\u4fe1\u606f\u3002\u6211\u4eec\u4f9d\u7136\u7528\u4e00\u5f00\u59cb\u751f\u6210\u7684Excel\u6587\u4ef6\u6765\u63a9\u9970\uff0c\u8bf7\u770b\u4ee5\u4e0b\u793a\u4f8b\uff1a<\/p>\n

    import xlrd\n\ndef read_excel():\n  #\u6253\u5f00\u6587\u4ef6\n  data=xlrd.open_workbook('d:\\TestXlrd.xlsx')\n  sheet3=data.sheet_by_index(2)\n  #\u8f93\u51fa\u2018\u4e00\u5757\u2019\u5408\u5e76\u5355\u5143\u683c\u7684\u4fe1\u606f\n  print(sheet3.merged_cells,'\\n')\n  \n  #\u5206\u89e3\u5408\u5e76\u7684\u5355\u5143\u683c\n  for crange in sheet3.merged_cells:\n    rlo, rhi, clo, chi = crange\n    for rowx in range(rlo, rhi):\n        for colx in range(clo, chi):\n          print(rowx,colx)\n          \nif __name__ == '__main__':\n  read_excel()\n<\/code><\/pre>\n

    \u8fd0\u884c\u5b83\uff0c\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n

    \"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

    \n

    \u8fd9\u91cc\u7528cell\u6765\u4ee3\u8868\u539f\u59cb\u7684\u4e00\u4e2a\u5355\u5143\u683c\uff0c\u7528\"\u4e00\u5757\"\u6765\u4ee3\u8868\u5408\u5e76\u540e\u7684\u5355\u5143\u683c\u3002<\/p>\n<\/blockquote>\n

    \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u201c\u4e00\u5757\u201d\u5408\u5e76\u7684\u5355\u5143\u683c\u8f93\u51fa\u4e3a\u4e86\u4e00\u4e2a\u5305\u542b4\u4f4d\u6570\u5b57\u7684list\uff0c\u5b83\u7684\u610f\u601d\u5c31\u662f[row,row_ range, col, col_range]\uff0c\u53ef\u4ee5\u8bb0\u4e3a[rlow,rhigh)\u5373\uff1a\u884c\u7684\u4f4e\u4f4d\u7d22\u5f15\u4e3arlow\uff0c\u9ad8\u4f4d\u7d22\u5f15\u4e0d\u5305\u542brhigh\uff08\u4e3arhigh-1\uff09\uff1b\u5217\u7684\u60c5\u51b5\u540c\u7406\u3002<\/p>\n

    \u5f97\u5230\u5408\u5e76\u7684\u5355\u5143\u683c\u540e\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u5206\u89e3\u6210\u4e00\u4e2a\u4e00\u4e2a\u7684cell\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8f93\u51fa\u90a3\"\u4e00\u5757\"\u6700\u4f4e\u4f4d\u7684\u884c\u5217\u503c\uff0c\u5219\u6b64\"\u4e00\u5757\"\u5355\u5143\u683c\u4e2d\u6240\u6709cell\u7684\u503c\u90fd\u4e3a\u6b64\uff0c\u4ee3\u7801\u7247\u6bb5\u5982\u4e0b\uff1a<\/p>\n

    for crange in sheet3.merged_cells:\n    rlo, rhi, clo, chi = crange\n    for rowx in range(rlo, rhi):\n        for colx in range(clo, chi):\n          print(rowx,colx)\n    print (\"\u4e0a\u9762cell\u7684\u503c\u4e3a\uff1a\"+sheet3.cell_value(rlo,clo)+'\\n')\n<\/code><\/pre>\n

    \u540c\u65f6\u53ef\u4ee5\u8bb0\u5f55\u7d22\u5f15\u6700\u4f4e\u7684\u90a3\u4e2acell\uff0c\u5e76\u5c06\u5176\u8d4b\u503c\u7ed9\u540c\"\u4e00\u5757\"\u5408\u5e76\u7684\u5355\u5143\u683c\u4e2d\u7684\u5176\u4ed6cell\uff0c\u8bb0\u5f55\u5728\u6211\u4eec\u6784\u9020\u7684dict\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53d8\u76f8\u89e3\u51b3xlrd\u6a21\u5757\u8bfb\u53d6\u5408\u5e76\u5355\u5143\u683c\u4e2d\u8bfb\u51fa\u7a7a\u503c\u7684\u95ee\u9898\u4e86\u3002xlrd\u4e2d\u8fd8\u6709\u5f88\u591a\u5176\u5b83\u7684API\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5f88\u591a\u6709\u7528\u7684\u529f\u80fd\u3002<\/p>\n

    2\u3001Python\u5199excel\u2014xlwt<\/h2>\n

    \u5199excel\u7684\u96be\u70b9\u5728\u4e8e\u586b\u5145\u7684\u6570\u636e\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u56fa\u5b9a\u7684\u6570\u636e\u7b80\u5316\u4e00\u4e0b\u3002\u5728\u5199excel\u7684\u64cd\u4f5c\u4e2d\u8fd8\u6709\u51e0\u4e2a\u68d8\u624b\u7684\u5730\u65b9\uff0c\u6bd4\u5982\u5199\u5165\u5408\u5e76\u7684\u5355\u5143\u683c\u3001\u5199\u5165\u4e0d\u540c\u7684\u6837\u5f0f\u7b49\u7b49\uff0c\u6709\u4e9b\u8981\u770b\u6e90\u7801\u624d\u80fd\u7814\u7a76\u7684\u900f\u3002\u76f8\u5bf9\u4e8exlrd\u63d0\u4f9b\u7684\u5f88\u591aAPI\u6765\u8bf4\uff0cxlwt\u63d0\u4f9b\u7684\u5c31\u8981\u5c11\u4e00\u4e9b\u4e86\u3002<\/p>\n

    \u5728Excel\u4e2d\u6dfb\u52a0\u5982\u4e0bsheet\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4e00\u4e9b\u6570\u636e\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n

    \"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

    \u8fd9\u5f20\u8868\u91cc\u9762\u53ea\u5b58\u4e86\u4e00\u4e9b\u57fa\u672c\u7684\u6570\u636e\uff0c\u53e6\u5916\u6709\u51e0\u4e2a\u5408\u5e76\u7684\u5355\u5143\u683c\uff0c\u6211\u4eec\u5e94\u5f53\u600e\u4e48\u751f\u6210\u8fd9\u6837\u4e00\u5f20\u8868\u5462\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    import xlwt\n\ndef write_excel():\n  wb = xlwt.Workbook() \n \n  #\u521b\u5efasheet\n  sheet1 = wb.add_sheet('sheet1',cell_overwrite_ok=True) \n\n  row0 = ['\u59d3\u540d', '\u7edf\u8ba1','\u6570\u503c'\t,'\u6392\u540d']\n  column0 = ['G','D','P']\n  item = ['\u5f97\u5206','\u7bee\u677f','\u52a9\u653b']\n\n  #\u751f\u6210\u7b2c\u4e00\u884c\n  for i in range(0,len(row0)):\n    sheet1.write(0,i,row0[i])\n\n  #\u751f\u6210\u7b2c\u4e00\u5217\u548c\u6700\u540e\u4e00\u5217(\u5408\u5e764\u884c)\n  i, j = 1, 0\n  while i < 3*len(column0) and j < len(column0):\n    sheet1.write_merge(i,i+2,0,0,column0[j]) #\u7b2c\u4e00\u5217\n    sheet1.write_merge(i,i+2,3,3) #\u6700\u540e\u4e00\u5217\"\u6392\u540d\"\n    i += 3\n    j += 1\n\n #\u751f\u6210\u7b2c\u4e8c\u5217\n  i = 0\n  while i < 3*len(column0):\n    for j in range(0,len(item)):\n      sheet1.write(j+i+1,1,item[j])\n    i += 3\n    \n #\u4fdd\u5b58\u6587\u4ef6\n  wb.save('demo2.xlsx') \n\nif __name__ == '__main__':\n    write_excel()   \n<\/code><\/pre>\n

    \u8fd0\u884c\u4e4b\u540e\uff0c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n

    \"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

    \"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3<\/p>\n

    \u53ef\u4ee5\u770b\u5230\uff0c\u5df2\u7ecf\u5b9e\u73b0\u4e86\u6211\u4eec\u60f3\u8981\u7684\u6548\u679c\u3002\u53ea\u662f\u8fd9\u91cc\u6211\u4eec\u6ca1\u6709\u6dfb\u52a0\u6837\u5f0f\uff0c\u6bd4\u5982\u6846\u7ebf\u3001\u5c45\u4e2d\u7b49\u3002\u5176\u5b9e\u8fd9\u91cc\u9762\u4e3b\u8981\u7528\u5230\u4e86write<\/code>\u548cwrite_merge<\/code>\u6765\u5f80cell\u91cc\u9762\u5199\u6570\u636e\uff0c\u5373\u6309\u7167cell\u6216\"\u5408\u5e76\u5355\u5143\u683c\"\u7684\u5f62\u5f0f\u5f80\u91cc\u9762\u5199\u6570\u636e\uff0c\u521d\u59cb\u6570\u636e\u90fd\u662f\u6211\u4eec\u7ed9\u5b9a\u7684\uff0c\u56e0\u6b64\u5e76\u4e0d\u590d\u6742\u3002<\/p>\n

    \u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3acell\u5b9a\u5236\u5404\u79cdFormatting\u548cStyle\uff0c\u4e0d\u8fc7\u8fd9\u597d\u50cf\u5e76\u4e0d\u662f\u5f88\u91cd\u8981\uff0c\u5982\u679c\u4e0d\u662f\u975e\u5e38\u5fc5\u8981\uff0c\u8c01\u4f1a\u5728\u7a0b\u5e8f\u91cc\u82b1\u5927\u91cf\u7684\u65f6\u95f4\u6765\u505a\u8fd9\u4e9b\u5462~~[\u624b\u52a8\u518d\u89c1]<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u5199excel\u8868\u683c\u8be6\u89e3\u8bb8\u591a\u8bed\u8a00\u90fd\u63d0\u4f9b\u6709\u8bfb\u5199excel\u6587\u4ef6\u7684API\uff0c\u6211\u5728\u8fd9\u91cc\u4f7f\u7528Python\u6765\u505a\u4e00\u4e2a\u57fa\u672c\u7684\u4e86\u89e3\u5427","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7961"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7961"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7961\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}