{"id":32,"date":"2026-02-02T17:45:42","date_gmt":"2026-02-02T09:45:42","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=32"},"modified":"2026-01-16T07:01:17","modified_gmt":"2026-01-16T07:01:17","slug":"how-to-move-cell-ranges-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-move-cell-ranges-using-openpyxl.html","title":{"rendered":"How To Move Cell Ranges 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;\">Move a range using the `move_range` method of the worksheet object:<\/p>\n<p lang=\"en-US\" style=\"margin-left: .375in; margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.move_range(cr,rows,cols)<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">The first parameter of this method specifies the range to be moved, which must be a CellRange object; the rows parameter defines the vertical movement. If the value is greater than 0, it moves down; if the value is less than 0, it moves up. The cols parameter defines the horizontal movement. If the value is greater than 0, it moves to the right; if the value is less than 0, it moves to the left.<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Move the Cell Range\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#Move the cell range\n#Row: >0 moves down; &lt;0 moves up\n#Column: >0 moves right; &lt;0 moves left\n\nfrom openpyxl.styles import PatternFill\nws&#91;'D4'].fill=PatternFill(fill_type='solid', start_color='00FF00')\nws&#91;'F10'].fill=PatternFill(fill_type='solid', start_color='00FF00')\nws.move_range('D4:F10', rows=-1, cols=2)\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=\"787\" height=\"507\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/15.png\" alt=\"Move Cell Ranges Using OpenPyXL\" class=\"wp-image-268\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/15.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/15-300x193.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/15-768x495.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/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-32","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/32","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=32"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}