{"id":3369,"date":"2023-05-11T12:08:15","date_gmt":"2023-05-11T04:08:15","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3369"},"modified":"2023-05-11T12:08:18","modified_gmt":"2023-05-11T04:08:18","slug":"python%e4%bb%a3%e7%a0%81%e7%94%a8openpyxl%e5%ba%93%e8%af%bb%e5%86%99excel%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=3369","title":{"rendered":"python\u4ee3\u7801\u7528openpyxl\u5e93\u8bfb\u5199excel\u6587\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#To create a workbook, you can import the Workbook class and start work: \r\nfrom openpyxl import Workbook; \r\nwb = Workbook()\r\n#To grab the active worksheet, you can use \r\nws = wb.active\r\n#To assign data directly to cells, you can use \r\nws&#91;'A1'] = 42.\r\n#To append rows, you can use \r\nws.append(&#91;1, 2, 3])\r\nws.append(&#91;6, 7, 8])\r\nws.append(&#91;1*2, 2*2, 3*2])\r\nws.append(&#91;6*3, 7*3, 8*4])\r\n\r\n#To add formulas using the openpyxl module just like you add values to a cell, you can use \r\nws&#91;'A9'] = '=SUM(A1:A8)'\r\nwb.save(r\"d:\\myexcel.xlsx\")\r\n\r\n#Read .xlsx wenjan \r\n# import openpyxl \r\n# worksheet = openpyxl.load_workbook(r\"d:\\myexcel.xlsx\") \r\n# sheet = worksheet.active \r\n# for row in sheet.iter_rows(min_row=1, min_col=1, max_row=6, max_col=2): \r\n#     for cell in row: \r\n#         print(cell.value, end=\" \") \r\n#     print()\r\n\r\n#To read excel.xlsx File\r\nimport openpyxl\r\n\r\n# Load the workbook\r\nworkbook = openpyxl.load_workbook(r\"d:\\myexcel.xlsx\")\r\n\r\n# Select the worksheet\r\nworksheet = workbook.active\r\n\r\n# # Access a cell value\r\n# cell = worksheet.cell(row=1, column=1)\r\n# print(cell.value)\r\n\r\n# Iterate through rows and columns\r\nfor row in worksheet.iter_rows(min_row=1, max_col=3):\r\n    for cell in row:\r\n        print(cell.value,end= \" \")\r\n    print()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3369","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3369"}],"version-history":[{"count":1,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3369\/revisions"}],"predecessor-version":[{"id":3370,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3369\/revisions\/3370"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3369"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}