{"id":6388,"date":"2020-09-14T21:30:40","date_gmt":"2020-09-14T13:30:40","guid":{"rendered":"http:\/\/dachengge.com\/?p=6388"},"modified":"2020-09-14T21:30:40","modified_gmt":"2020-09-14T13:30:40","slug":"python%e6%93%8d%e4%bd%9cexcel%e7%b1%bb%e5%ba%93openpyxl%e4%b9%8b%e5%88%9d%e4%bd%93%e9%aa%8c","status":"publish","type":"post","link":"http:\/\/dachengge.com\/?p=6388","title":{"rendered":"Python\u64cd\u4f5cExcel\u7c7b\u5e93Openpyxl\u4e4b\u521d\u4f53\u9a8c"},"content":{"rendered":"<p>\u4ee5\u524d\u5c1d\u8bd5\u8fc7\u901a\u8fc7win32com\u65b9\u5f0f\u8bfb\u5199excel\uff0cWin32com\u8c03\u7528Excel\u662f\u6700\u597d\u7684\u4e00\u4e2a\u65b9\u5f0f\uff0c\u56e0\u4e3a\u76f4\u63a5\u8c03\u7528\u7684\u662fwindows\u5e73\u53f0\u7684\u8f6f\u4ef6\uff0c\u53ef\u9760\u6027\u8981\u9ad8\u4e00\u4e9b\uff0c\u53e6\u5916excel\u51fa\u9519\u7684\u53ef\u80fd\u6027\u4e5f\u5c0f\u4e00\u4e9b\u3002\u7531\u4e8ewin32com\u5305\u600e\u4e48\u4e5f\u627e\u4e0d\u5230\uff0c\u4e8e\u662f\u8fd9\u6b21\u5c1d\u8bd5\u4f7f\u7528openpyxl\u7c7b\u5e93\u8bfb\u5199excel\uff0c\u9009\u62e9openpyxl\u7684\u539f\u56e0\u5728\u4e8e\u5b83\u80fd\u591f\u8bfb\u53d6\u548c\u5199\u5165Excel\u6587\u4ef6\uff0c\u76f8\u5bf9\u4e8exlrd\u3001xlwt\u65b9\u4fbf\u4e00\u4e9b\u3002<\/p>\n<p><strong>1 \u5b89\u88c5openpyxl<\/strong><\/p>\n<p>\u5b89\u88c5\u65b9\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u76f4\u63a5\u4f7f\u7528pip\u5373\u53ef\uff1a<br \/>\npip install openpyxl<br \/>\n\u5b83\u80fd\u81ea\u52a8\u4e0b\u8f7d\u548c\u5b89\u88c5openpyxl\u6240\u4f9d\u8d56\u7684\u5305\uff0c\u7136\u540e\u518d\u5b89\u88c5openpyxl\u3002<br \/>\n\u4f46\u662f\u672c\u4eba\u4f7f\u7528\u7684\u673a\u5668\u662f\u79bb\u7ebf\u7684\uff0c\u56e0\u6b64\u53ea\u80fd\u91c7\u7528\u79bb\u7ebf\u5b89\u88c5\u7684\u65b9\u5f0f\uff0c\u7ecf\u8fc7\u5c1d\u8bd5\uff0c\u9700\u8981\u4e0b\u8f7d\u4ee5\u4e0b\u5b89\u88c5\u5305\uff1a<br \/>\net_xmlfile<br \/>\njdcal<br \/>\nopenpyxl<br \/>\n\u4e0a\u9762\u4e09\u4e2a\u5b89\u88c5\u4f9d\u6b21\u4e0b\u88c5\u5e76\u5b89\u88c5\uff0c\u6ce8\u610f\u7248\u672c\uff0c\u6bd4\u5982Openpyxl\u6700\u65b0\u7248\u672c\u53f7\u662f3.0.5\uff0c\u90a3\u4e48et_xmlfile\u548cjdcal\u4e0b\u8f7d\u65f6\u5e94\u6ce8\u610f\u7248\u672c\u53f7\uff0c\u5426\u5219\u4f1a\u51fa\u9519\u3002\u4e0d\u8fc7\u51fa\u9519\u4e5f\u6ca1\u5173\u7cfb\uff0c\u56e0\u4e3a\u51fa\u9519\u4fe1\u606f\u4e2d\u4f1a\u63d0\u793a\u6240\u4f9d\u8d56\u5305\u7684\u7248\u672c\u53f7\uff0c\u91cd\u65b0\u4e0b\u8f7d\u4e00\u904d\u4e5f\u6ca1\u5173\u7cfb\u3002<\/p>\n<p><strong>2 \u64cd\u4f5cExcel<\/strong><br \/>\n2.1 \u5bfc\u5165openpyxl<br \/>\n<!--more-->\u548c\u6240\u6709\u7684python\u5305\u4e00\u6837\uff0c\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u5bfc\u5165\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u9519\u3002<br \/>\n#\u5bfc\u5165workbook\u6216\u8005load_workbook\uff0c\u80fd\u591f\u8bfb\u5199excel<br \/>\nfrom openpyxl import Workbook<br \/>\nfrom openpyxl import load_workbook<br \/>\n#\u5bfc\u5165\u6837\u5f0f\uff0c\u80fd\u591f\u4fee\u6539\u5355\u5143\u683c\u683c\u5f0f<br \/>\n#PatternFill\u4e3a\u586b\u5145\u6837\u5f0f<br \/>\n#Border\u4e3a\u5355\u5143\u683c\u8fb9\u754c<br \/>\n#Alignmeng\u4e3a\u5bf9\u9f50\u65b9\u5f0f<br \/>\n#Font\u4e3a\u5b57\u4f53\u6837\u5f0f<br \/>\n#\u53e6\u5916\uff0c\u8fd8\u6709\u503c\u683c\u5f0f.number_format<br \/>\nfrom openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font<br \/>\n2.2 \u8bfb\u5199excel<br \/>\n#\u6253\u5f00excel\u6587\u4ef6<br \/>\nfrom openpyxl import load_workbook<br \/>\nwb=load_workbook(&#8216;test.xlsx&#8217;)<br \/>\nws1=wb[&#8216;Sheet1&#8217;]<br \/>\n#\u65b0\u5efaexcel\u6587\u4ef6<br \/>\nfrom openpyxl import Workbook<br \/>\nwb=Workbook()<br \/>\nws1=wb.active\u00a0 \u6216\u8005<br \/>\nws2=wb.create_sheet(&#8216;Sheet2&#8217;,1) 1\u662f\u4f4d\u7f6e<br \/>\n#\u5199\u5165\u6570\u636e \u65b9\u5f0f1\uff0c\u76f4\u63a5\u5728\u540e\u9762\u8ffd\u52a0\u6570\u636e\uff0c\u4f7f\u7528append\u65b9\u6cd5\uff1a<br \/>\nlist=[&#8216;\u5f20\u4e09&#8217;,&#8217;\u7537&#8217;,&#8217;23&#8217;,&#8217;\u5b66\u751f&#8217;]<br \/>\nws1.append(list)<br \/>\n#\u5199\u5165\u6570\u636e \u65b9\u5f0f2\uff0c\u6839\u636e\u884c\u3001\u5217\u5199\u5165\uff1a<br \/>\nws1[&#8216;C3&#8217;]=123 \u6216\u8005<br \/>\nws1.cell(row=3,column=3,value=&#8221;234&#8243;)<br \/>\n2.3 \u4fdd\u5b58\u548c\u5173\u95edexcel<br \/>\nfileName=&#8221;d:\\\\data.xlsx&#8221;<br \/>\nwb.save(fileName)<\/p>\n<p><strong>3 \u8868\u683c\u6837\u5f0f<\/strong><br \/>\n\u76f4\u63a5\u5199\u5165\u6570\u636e\u7684Excel\u8868\u6ca1\u6709\u6837\u5f0f\uff0c\u4e0d\u592a\u597d\u770b\uff0c\u56e0\u6b64\u9700\u8981\u8bbe\u7f6e\u6837\u5f0f\uff0c\u5728openpyxl\u4e2d\uff0c\u6837\u5f0f\u6709\u5355\u72ec\u7684\u7c7b\u8868\u793a\u3002\u53ef\u4ee5\u5bf9\u5355\u5143\u683c\u8c03\u7f6e\uff0c\u4e5f\u80fd\u5bf9\u884c\u6216\u5217\u8c03\u7f6e\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\uff0c\u524d\u9762\u5df2\u7ecf\u8bb2\u8fc7\u4e86\u8981\u5bfc\u5165\u7684\u5305\u3002<br \/>\n#\u8bbe\u7f6e\u5b57\u4f53\uff0c\u5148\u5b9a\u4e49\u5b57\u4f53\u7c7b\uff0c\u518d\u8d4b\u503c\u7ed9\u5355\u5143\u683c<br \/>\nMyfont = Font(name=&#8217;Calibri&#8217;,<br \/>\nsize=11,<br \/>\nbold=False,<br \/>\nitalic=False,<br \/>\nvertAlign=None,<br \/>\nunderline=&#8217;none&#8217;,<br \/>\nstrike=False,<br \/>\ncolor=&#8217;FF000000&#8242;)<br \/>\nws1.cell(row=1,column=2).font=Myfont<br \/>\n#\u8bbe\u7f6e\u5bf9\u9f50\u65b9\u5f0f\uff0c\u5148\u5b9a\u4e49\u5bf9\u9f50\u683c\u5f0f\uff0c\u518d\u8d4b\u503c\u5355\u5143\u683c<br \/>\nMyalignment=Alignment(horizontal=&#8217;general&#8217;,<br \/>\nvertical=&#8217;bottom&#8217;,<br \/>\ntext_rotation=0,<br \/>\nwrap_text=False,<br \/>\nshrink_to_fit=False,<br \/>\nindent=0)<br \/>\nws1.cell(row=1,column=2).alignment=Myalignment<br \/>\n#\u8bbe\u7f6e\u8fb9\u754c<br \/>\nMyborder = Border(left=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\nright=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\ntop=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\nbottom=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\ndiagonal=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\ndiagonal_direction=0,<br \/>\noutline=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\nvertical=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;),<br \/>\nhorizontal=Side(border_style=None,<br \/>\ncolor=&#8217;FF000000&#8242;)<br \/>\n)<br \/>\nws1.cell(row=1,column=2).border=Myborder<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u524d\u5c1d\u8bd5\u8fc7\u901a\u8fc7win32com\u65b9\u5f0f\u8bfb\u5199excel\uff0cWin32com\u8c03\u7528Excel\u662f\u6700\u597d\u7684\u4e00\u4e2a\u65b9\u5f0f\uff0c\u56e0\u4e3a\u76f4\u63a5\u8c03\u7528 &hellip; <a href=\"http:\/\/dachengge.com\/?p=6388\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Python\u64cd\u4f5cExcel\u7c7b\u5e93Openpyxl\u4e4b\u521d\u4f53\u9a8c<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,11],"tags":[61,85,94],"views":3164,"_links":{"self":[{"href":"http:\/\/dachengge.com\/index.php?rest_route=\/wp\/v2\/posts\/6388"}],"collection":[{"href":"http:\/\/dachengge.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dachengge.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dachengge.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dachengge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6388"}],"version-history":[{"count":0,"href":"http:\/\/dachengge.com\/index.php?rest_route=\/wp\/v2\/posts\/6388\/revisions"}],"wp:attachment":[{"href":"http:\/\/dachengge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dachengge.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6388"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dachengge.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}