{"id":314,"date":"2018-11-08T11:10:37","date_gmt":"2018-11-08T03:10:37","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=314"},"modified":"2022-05-17T09:45:11","modified_gmt":"2022-05-17T01:45:11","slug":"python-5","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=314","title":{"rendered":"python\u6839\u636eexcel\u7684\u4e00\u5217\u6570\u636e\u4ea7\u751f\u52a0\u6743\u968f\u673a\u6570"},"content":{"rendered":"<h1>\u9700\u6c42<\/h1>\n<p>\u6700\u8fd1\u9047\u5230\u4e00\u4e2a\u5947\u8469\u7684\u4e8b\uff0c\u884c\u653f\u90a3\u8fb9\u8bf4\uff0c\u8ba9\u4f30\u7b97\u4e00\u4e0b\u660e\u5e74\u7684\u8fd9\u4e9b\u6742\u4e03\u6742\u516b\u8d39\uff0c\u7136\u540e\u7ed9\u4e86\u6211\u4e00\u4e2aexcel,\u91cc\u9762\u6709200\u591a\u4e2a\u8fd9\u6837\u7684\u8d39\u7528\u3002\u6211\u6ca1\u505a\u8fc7\u884c\u653f\uff0c\u4e5f\u641e\u4e0d\u6e05\u8fd9\u4e2a\u5230\u5e95\u548b\u6765\uff0c\u4e3a\u4ec0\u4e48\u8981\u5f04\u8fd9\u73a9\u610f\u3002\u4e00\u756a\u4ea4\u6d41\uff0c\u539f\u6765\u662f\u4e0a\u5934\u8981\u7684\uff0c\u4e0d\u4e00\u5b9a\u770b\uff0c\u4f46\u662f\u4e1c\u897f\u5f97\u6709\uff0c\u8ba9\u6211\u5f04\u4e2a\u6570\u5b57\u548c\u53bb\u5e74\u5dee\u4e0d\u591a\u7684\u5c31\u884c\u3002\u4e8e\u662f\u53d8\u6709\u4e86\u4e0b\u9762\u7684\u6545\u4e8b<\/p>\n<h1>\u8bbe\u8ba1<\/h1>\n<p>1.\u9700\u8981\u8bfb\u53d6excel. \u7528\u7684\u5e93\u662fxlwing\u3002\u53ef\u4ee5\u6839\u636esheet\u540d\u79f0\u5b9a\u4f4dsheet,\u7136\u540e\u7528range()\u65b9\u6cd5\u5b9a\u4f4d\u4e00\u884c\uff0c\u4e00\u5217\uff0c\u6216\u8005\u6307\u5b9a\u51e0\u4e2a\u5355\u5143\u683c\u3002<br \/>\u4f8b\u5982\uff1a\u5b9a\u4f4d A1\u5230A3 \uff0c<code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">range1=sheet.range(\"A1:A3\")<\/code><br \/>\u5177\u4f53\u7528\u6cd5\u5f88\u591a\uff0c\u5728xlwing\u7684\u5b98\u7f51\u4e0a\u6709\u4ecb\u7ecd\u3002http:\/\/docs.xlwings.org\/en\/stable\/api.html<br \/>2.\u9700\u8981\u4ea7\u751f\u968f\u673a\u6570\uff0c\u7528\u7684\u5e93\u662fnumpy,\u4ea7\u751f\u4e00\u4e2a\u52a0\u6743\u7684\u968f\u673a\u6570\uff0c\u7136\u540e\u6211\u7528\u7684\u52a0\u6743\u968f\u673a\u6570\u89c4\u5219\u4e3a\uff1a<br \/>\u3010-10%\uff0c\u6982\u738710%\u3011\u3010\u4e0d\u53d8\uff0c\u6982\u738730%\u3011\u3010+2%\uff0c\u6982\u738730%\u3011\u3010+5%\uff0c\u6982\u738720%\u3011\u3010+10%\uff0c\u6982\u738710%\u3011<\/p>\n<h1>\u4ee3\u7801\u5b9e\u73b0<\/h1>\n<p>\u4ee3\u7801\u4e2d\u5206\u4e3a\u4e24\u4e2afunction\uff0c\u4e00\u4e2a\u662f\u8bfb\u53d6excel\uff0c\u4e00\u4e2a\u662f\u6839\u636e\u4e00\u4e2a\u503c\uff0c\u4ea7\u751f\u4e00\u4e2a\u52a0\u6743\u968f\u673a\u6570\u3002\u89c2\u4f17\u8001\u7237\u53ef\u4ee5\u53c2\u8003\uff0c\u5bf9\u6709\u7591\u95ee\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u518d\u6765\u4e0e\u6211\u6c9f\u901aO(\u2229_\u2229)O<\/p>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># -*- coding: UTF-8 -*-\nimport xlwings\nimport numpy as np\ndef import_excel(filename, sheet_name, source,target):\n    '''\n    \u5bfc\u5165\u4e00\u4e2aexcel,\u6839\u636e\u67d0\u4e00\u5217\u7684\u503c\uff0c\u4ea7\u751f\u4e00\u4e2a\u52a0\u6743\u968f\u673a\u6570\uff0c\u8986\u76d6\u6389\u53e6\u4e00\u5217\u7684\u503c\n    :param filename: excel\u8def\u5f84\n    :param sheet_name: sheet\u7684\u540d\u5b57\n    :param source: \u6e90\u5217\u7684\u7b2c\u4e00\u4e2a\u503c \u5982\uff1aA1,C1\n    :param target: \u76ee\u6807\u5217\u7684\u7b2c\u4e00\u4e2a\u503c\uff0c\u5982B1,D2\n    :return:\n    '''\n    app = xlwings.App(visible=False, add_book=True)\n    app.display_alerts = False\n    app.screen_updating = False\n    wb = app.books.open(filename)\n    sht = wb.sheets[sheet_name]\n    rng1 = sht.range(source)\n    rng2 = sht.range(source).end('down')\n    rng3 = sht.range(rng1, rng2)\n    all_rng = rng3.value\n    list_dest = []\n    for item in all_rng:\n        dest_item = arithmetic(item)\n        # \u8fd9\u91cc\u8981\u628a\u7ed3\u679c\u53d8\u6210[[1],[2]]\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u624d\u662f\u4e00\u5217\u7684\u6570\u636e\n        # \u5982\u679c\u662f[1,2]\u8fd9\u6837\uff0c\u662f\u4e00\u884c\u7684\u6570\u636e\n        list_temp = []\n        list_temp.append(dest_item)\n        list_dest.append(list_temp)\n    sht.range(target).value = list_dest\n    wb.save(filename)\n    wb.close()\n    app.quit()\ndef arithmetic(source):\n    '''\n    \u52a0\u6743\u968f\u673a\u7b97\u6cd5\n    :param source: \u8f93\u5165\u4e00\u4e2a\u539f\u503c\n    :return: \u8fd4\u56de\u6d6e\u52a8\u540e\u7684\u503c\n    '''\n    power = np.array([0.1, 0.3, 0.3, 0.2, 0.1])\n    index = np.random.choice([-0.1, 0, 2, 5, 10], p=power.ravel())\n    rand = source * index \/ 100\n    dest = int(source + rand)\n    return dest\nif __name__ == '__main__':\n    import_excel(\"Book1.xlsx\", 'Sheet1','C2','D2')<\/code><\/pre>\n<h1>\u8fd0\u884c\u7ed3\u679c<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-318 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/\u8fd0\u884c\u7ed3\u679c.png\" alt=\"\" width=\"710\" height=\"164\" \/><\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u6700\u8fd1\u9047\u5230\u4e00\u4e2a\u5947\u8469\u7684\u4e8b\uff0c\u884c\u653f\u90a3\u8fb9\u8bf4\uff0c\u8ba9\u4f30\u7b97\u4e00\u4e0b\u660e\u5e74\u7684\u8fd9\u4e9b\u6742\u4e03\u6742\u516b\u8d39\uff0c\u7136\u540e\u7ed9\u4e86\u6211\u4e00\u4e2aexcel,\u91cc\u9762\u6709200 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":316,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=314"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/314\/revisions\/941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/media\/316"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}