{"id":7,"date":"2026-01-21T11:07:34","date_gmt":"2026-01-21T03:07:34","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=7"},"modified":"2026-01-16T05:42:26","modified_gmt":"2026-01-16T05:42:26","slug":"how-to-store-and-manage-worksheets-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-store-and-manage-worksheets-using-openpyxl.html","title":{"rendered":"How To Store and Manage Worksheets Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Newly created worksheet objects are automatically added to the `worksheets` collection. You can extract objects from this collection using indexing or iteration, and also remove objects from it.<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">To access the `worksheets` collection of a workbook object:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; sheets=wb.worksheets<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; sheets[0].title<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&#8216;Sheet&#8217;<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; sheets[1].title<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&#8216;MySheet&#8217;<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; line-height: 10pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt; color: black;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Accessing the active worksheet:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; ws=wb.active<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">If you don\u2019t know the worksheet name but know the index, use the `sheetnames` property to get all sheet names and reference the sheet by index:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; names = wb.sheetnames<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; ws4 = wb[names[0]]<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; line-height: 15pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">To remove a worksheet from the collection:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; wb.remove(ws)<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Check how many objects are in the collection:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; sheets=wb.worksheets<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; len(sheets)<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Managing and Storing Worksheets\n\n#Import load_workbook function\nfrom openpyxl import load_workbook\n\n#Load the workbook\nwb=load_workbook(filename='wb2.xlsx')\n#Create a new worksheet\nws1=wb.create_sheet()\n#Get the worksheet collection object\nsheets=wb.worksheets\n#Output the title of the first worksheet\nprint(sheets&#91;0].title)\n#Output the title of the second worksheet\nprint(sheets&#91;1].title)\n\n#Output the worksheet collection\nprint(sheets)\n#Output the number of worksheets in the collection\nprint(len(sheets))\n\n#Iterate through the workbook and\n#output the titles of each worksheet\nfor sheet in wb:\n    print(sheet.title)\n\n#Delete the newly created worksheets\nwb.remove(ws1)\n#Re-fetch the number of worksheets\n#in the collection and output it\nsheets=wb.worksheets\nprint(len(sheets))\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenPyXL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-openpyxl-worksheet"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/205"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}