{"id":8,"date":"2026-01-21T18:25:42","date_gmt":"2026-01-21T10:25:42","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=8"},"modified":"2026-01-16T05:44:43","modified_gmt":"2026-01-16T05:44:43","slug":"how-to-reference-worksheets-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-reference-worksheets-using-openpyxl.html","title":{"rendered":"How To Reference Worksheets Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sheets=wb.worksheets<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws=sheets[0]\u00a0 \u00a0 \u00a0#Using sheet index<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-size: 14.0pt;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">ws2=wb[<\/span><span style=\"font-family: Calibri;\">&#8216;<\/span><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">Sheet<\/span><span style=\"font-family: Calibri;\">&#8216;<\/span><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">]\u00a0 \u00a0 #Using sheet name<\/span><\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-size: 14.0pt;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">ws3=wb.get_sheet_by_name(<\/span><span style=\"font-family: Calibri;\">&#8216;<\/span><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">Sheet<\/span><span style=\"font-family: Calibri;\">&#8216;<\/span><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">)<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">names=wb.sheetnames<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws4=wb[names[0]]<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Accessing Worksheets by Index or Name\n\n#Import load_workbook function\nfrom openpyxl import load_workbook\n\n#Load the workbook\nwb=load_workbook(filename='wb2.xlsx')\n#Get the worksheet collection object\nsheets=wb.worksheets\n#Access the first worksheet using the index\nws=sheets&#91;0]\n#Output the title of the worksheet\nprint(ws.title)\n\n#Access the worksheet by name\nws2=wb&#91;'Sheet']\n#Output the title of worksheet ws2\nprint(ws2.title)\n\n#Use get_sheet_by_name function to access a specified worksheet\nws3=wb.get_sheet_by_name('Sheet')\n#Output the title of worksheet ws3\nprint(ws3.title)\n\n#Get all worksheet names\nnames=wb.sheetnames\n#Access the corresponding worksheet by the first name\nws4=wb&#91;names&#91;0]]\n#Output the title of worksheet ws4\nprint(ws4.title)\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\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-8","post","type-post","status-publish","format-standard","hentry","category-openpyxl-worksheet"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/8","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=8"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions"}],"predecessor-version":[{"id":207,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/8\/revisions\/207"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=8"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=8"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=8"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}