{"id":31,"date":"2026-02-02T11:20:38","date_gmt":"2026-02-02T03:20:38","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=31"},"modified":"2026-01-16T06:59:09","modified_gmt":"2026-01-16T06:59:09","slug":"how-to-copy-cell-range-styles-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-copy-cell-range-styles-using-openpyxl.html","title":{"rendered":"How To Copy Cell Range Styles Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p>Copying styles:<\/p>\n<p>from copy import copy<\/p>\n<p>cr=CellRange(&#8216;D5:F7&#8217;)<\/p>\n<p>cr2=CellRange(&#8216;D12:F14&#8217;)<\/p>\n<p>r=cr2.max_row-cr.max_row<\/p>\n<p>c=cr2.max_col-cr.max_col<\/p>\n<p>for cl in cr.cells:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 cl2=ws.cell(row=cl[0],column=cl[1])<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 ws.cell(row=cl[0]+r,column=cl[1]+c,value=cl2.value)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0 if cl2.has_style:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc=ws.cell(row=cl[0]+r,column=cl[1]+c)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc.<b>font<\/b>=copy(cl2.font)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc.<b>border<\/b>=copy(cl2.border)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc.<b>fill<\/b>=copy(cl2.fill)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc.<b>number_format<\/b>=copy(cl2.number_format)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 nc.<b>alignment<\/b>=copy(cl2.alignment)<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Copy the Style of a 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#Import CellRange class and copy function\nfrom openpyxl.worksheet.cell_range import CellRange\nfrom copy import copy\n\n#Create two cell ranges\ncr=CellRange('D5:F7')\ncr2=CellRange('D12:F14')\n\n#Copy the style\nr=cr2.max_row-cr.max_row\nc=cr2.max_col-cr.max_col\nfor cl in cr.cells:\n      cl2=ws.cell(row=cl&#91;0],column=cl&#91;1])\n      ws.cell(row=cl&#91;0]+r,column=cl&#91;1]+c,value=cl2.value)\n      #If there are styles, use the `copy` function to copy them\n      if cl2.has_style:\n            nc=ws.cell(row=cl&#91;0]+r,column=cl&#91;1]+c)\n            nc.font=copy(cl2.font)\n            nc.border=copy(cl2.border)\n            nc.fill=copy(cl2.fill)\n            nc.number_format=copy(cl2.number_format)\n            nc.alignment=copy(cl2.alignment)\n\nwb.save('test.xlsx')\nwb.close()\n<\/code><\/pre>\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-31","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/31","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=31"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}