{"id":33,"date":"2026-02-03T11:03:28","date_gmt":"2026-02-03T03:03:28","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=33"},"modified":"2026-01-16T07:03:40","modified_gmt":"2026-01-16T07:03:40","slug":"how-to-expande-and-shrink-specified-regions-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-expande-and-shrink-specified-regions-using-openpyxl.html","title":{"rendered":"How To Expande and Shrink Specified Regions Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<ul style=\"direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in;\" type=\"disc\">\n<li style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Expand using the `expand` method of the `CellRange` object<\/span><\/li>\n<\/ul>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.expand(right=0,down=0,left=0,up=0)<\/p>\n<ul style=\"direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in;\" type=\"disc\">\n<li style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">using the `shrink` method of the `CellRange` object<\/span><\/li>\n<\/ul>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.shrink(right=0,bottom=0,left=0,top=0)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Use the `size` attribute to view the changes before and after.<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Expanding and Shinking Cell Ranges\n\n#Import load_workbook function\nfrom openpyxl import Workbook\n\n#Create a new workbook\nwb=Workbook()\n#Get the active worksheet\nws=wb.active\n\n#Import CellRange function\nfrom openpyxl.worksheet.cell_range import CellRange\n\n#Create a cell range object\ncr=CellRange('D5:F7')\n#Output the size of the cell range\nprint(cr.size)\n\n#Expanding\ncr.expand(right=1,down=2,left=0,up=0)\nprint(cr.size)\n#Shinking\n#cr.shrink(right=1,bottom=2,left=0,top=0)\n#print(cr.size)\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"323\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/16.png\" alt=\"Expande and Shrink Specified Regions Using OpenPyXL\" class=\"wp-image-271\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/16.png 846w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/16-300x115.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/16-768x293.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\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":[3],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/33","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=33"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":272,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/33\/revisions\/272"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}