{"id":34,"date":"2026-02-03T18:15:18","date_gmt":"2026-02-03T10:15:18","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=34"},"modified":"2026-01-16T07:06:07","modified_gmt":"2026-01-16T07:06:07","slug":"how-to-set-operations-on-cell-ranges-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-operations-on-cell-ranges-using-openpyxl.html","title":{"rendered":"How To Set Operations on Cell Ranges Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Methods of the `CellRange` object:<\/p>\n<ul style=\"direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in;\" type=\"disc\">\n<li lang=\"en-US\" style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.union(other)<\/span><\/li>\n<li lang=\"en-US\" style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.intersection(other)<\/span><\/li>\n<li lang=\"en-US\" style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.issubset(other)<\/span><\/li>\n<li lang=\"en-US\" style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.issuperset(other)<\/span><\/li>\n<li lang=\"en-US\" style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">cr.isdisjoint(other)<\/span><\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Set Operations of 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 two cell range objects\ncr1=CellRange('D5:F7')\ncr2=CellRange('E3:G5')\n\n#Compute the union of the two ranges\nprint(cr1.union(cr2))\n\n#Compute the intersection of the two ranges\nprint(cr1.intersection(cr2))\n\n#Check if one range is a subset of the other\nprint(cr1.issubset(cr2))\n\n#Check if one range is a superset of the other\nprint(cr1.issuperset(cr2))\n\n#Check if the two ranges are disconnected\nprint(cr1.isdisjoint(cr2))\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=\"273\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/17.png\" alt=\"Set Operations on Cell Ranges Using OpenPyXL\" class=\"wp-image-274\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/17.png 846w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/17-300x97.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/02\/17-768x248.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\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-34","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/34","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=34"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}