{"id":396,"date":"2021-08-28T08:00:00","date_gmt":"2021-08-28T08:00:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=396"},"modified":"2021-08-28T08:00:00","modified_gmt":"2021-08-28T08:00:00","slug":"396","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/28\/396\/","title":{"rendered":"openpyxl &#8211; \u8bfb\/\u5199 Excel 2010 xlsx\/xlsm \u6587\u4ef6\u7684 Python \u5e93"},"content":{"rendered":"<h3>openpyxl \u64cd\u4f5cExcel2010 xlsx\u5e93  <a href=\"https:\/\/openpyxl-chinese-docs.readthedocs.io\/zh_CN\/latest\/\">\u4e2d\u6587\u6587\u6863<\/a><\/h3>\n<h3>\u7b80\u6613\u6559\u7a0b<\/h3>\n<pre><code>from openpyxl import Workbook, load_workbook\n# f = load_workbook('r:\/new.xlsx')         # \u52a0\u8f7d Excel 2010\u6587\u4ef6\nwb = Workbook()           # \u65b0\u5efa\u5de5\u4f5c\u7c3f\nws = wb.active            # \u5f53\u524d\u6fc0\u6d3b\u5de5\u4f5c\u7c3f\nws.title = \"New Title\"    # \u4fee\u6539\u5de5\u4f5c\u7c3f\u540d\u79f0\nws.sheet_properties.tabColor = \"1072BA\"    # \u8bbe\u7f6e\u6807\u7b7e\u989c\u8272\n\n#  \u53ef\u4ee5\u4f7f\u7528 Workbook.create_sheet \u65b9\u6cd5\u6765\u521b\u5efa\u65b0\u7684\u5de5\u4f5c\u7c3f\nws1 = wb.create_sheet(\"Mysheet\")        #  \u5728\u6700\u540e\u63d2\u5165\uff08\u9ed8\u8ba4\uff09\nws2 = wb.create_sheet(\"Mysheet\", 0)     #   \u5728\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u63d2\u5165\nws3 = wb.create_sheet(\"Mysheet\", -1)    #  \u5728\u5012\u6570\u7b2c\u4e8c\u4e2a\u4f4d\u7f6e\u63d2\u5165\n\nprint(wb.sheetnames)     # \u67e5\u770b\u6240\u6709\u5de5\u4f5c\u7c3f\u540d\u79f0\n# #  \u904d\u5386\u5de5\u4f5c\u8868\nfor sheet in wb:\n    print(sheet.title)\n\nc = ws['A4']           # \u901a\u8fc7\u5de5\u4f5c\u8868\u7684\u952e\u6765\u8bbf\u95ee\u5355\u5143\u683c\nws['A4'] = 4           # \u76f4\u63a5\u5206\u914d\u503c\nd = ws.cell(row=4, column=2, value=10)   # \u4e5f\u53ef\u4ee5\u901a\u8fc7\u884c\u5217\u7b26\u53f7\u8bbf\u95ee\u5355\u5143\u683c\n\nwb.save('new.xlsx')    # \u4fdd\u5b58\u5de5\u4f5c\u7c3f<\/code><\/pre>\n<h3>\u5de5\u4f5c\u7c3f\u8868\u683c<\/h3>\n<p>\u5de5\u4f5c\u7c3f\u8868\u683c\u662f\u5bf9\u5355\u5143\u683c\u7ec4\u7684\u5f15\u7528\u3002\u8fd9\u4f7f\u5f97\u67d0\u4e9b\u64cd\u4f5c\uff08\u4f8b\u5982\uff0c\u5bf9\u8868\u683c\u4e2d\u7684\u5355\u5143\u683c\u8fdb\u884c\u6837\u5f0f\u8bbe\u7f6e\uff09\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<ul>\n<li>\n<p>\u521b\u5efa\u8868\u683c \u793a\u4f8b<\/p>\n<pre><code>from openpyxl import Workbook\nfrom openpyxl.worksheet.table import Table, TableStyleInfo\n\nwb = Workbook()\nws = wb.active\n\ndata = [\n  ['Apples', 10000, 5000, 8000, 6000],\n  ['Pears',   2000, 3000, 4000, 5000],\n  ['Bananas', 6000, 6000, 6500, 6000],\n  ['Oranges',  500,  300,  200,  700],\n]\n\n# \u6dfb\u52a0\u5217\u6807\u9898\u3002 \u6ce8\u610f,\u8fd9\u4e9b\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\nws.append([\"Fruit\", \"2011\", \"2012\", \"2013\", \"2014\"])\nfor row in data:\n  ws.append(row)\n\ntab = Table(displayName=\"Table1\", ref=\"A1:E5\")\n\n# \u6dfb\u52a0\u5e26\u6709\u6761\u7eb9\u884c\u548c\u6761\u7eb9\u5217\u7684\u9ed8\u8ba4\u6837\u5f0f\nstyle = TableStyleInfo(name=\"TableStyleMedium9\", showFirstColumn=False,\n                     showLastColumn=False, showRowStripes=True, showColumnStripes=True)\ntab.tableStyleInfo = style\n\n'''\n\u5fc5\u987b\u4f7f\u7528 ws.add_table() \u65b9\u6cd5\u6dfb\u52a0\u8868\u4ee5\u907f\u514d\u91cd\u590d\u540d\u79f0\u3002\n\u4f7f\u7528\u6b64\u65b9\u6cd5\u53ef\u786e\u4fdd\u8868\u540d\u5728\u5b9a\u4e49\u7684\u540d\u79f0\u548c\u6240\u6709\u5176\u4ed6\u8868\u540d\u4e2d\u90fd\u662f\u552f\u4e00\u7684\u3002\n'''\nws.add_table(tab)\nwb.save(\"table.xlsx\")<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>openpyxl \u64cd\u4f5cExcel2010 xlsx\u5e93 \u4e2d\u6587\u6587\u6863 \u7b80\u6613\u6559\u7a0b from openpyxl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-396","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=396"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/396\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}