{"id":17,"date":"2026-01-26T11:48:22","date_gmt":"2026-01-26T03:48:22","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=17"},"modified":"2026-01-16T06:19:07","modified_gmt":"2026-01-16T06:19:07","slug":"how-to-reference-a-cell-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-reference-a-cell-using-openpyxl.html","title":{"rendered":"How To Reference a Cell Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">A cell reference refers to finding the cell you want to operate on in a worksheet.<\/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;\">Use square brackets<\/span><\/li>\n<\/ul>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws[&#8216;A1&#8217;]=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;\">Use the `cell` method of the worksheet object to reference and assign values<\/span><\/li>\n<li style=\"margin-top: 0; margin-bottom: 0; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Use the `Cell` class<\/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;\"><span style=\"background: white;\">from openpyxl.cell.cell import Cell, MergedCell, WriteOnlyCell<\/span><\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"background: white;\">from openpyxl.cell.read_only import ReadOnlyCell<\/span><\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Common Attributes<\/p>\n<ul>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">alignment<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">Border<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">col_idx<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">column<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">column_letter<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">comment<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">coordinate<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">data_type<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">encoding<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">fill<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">font<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">has_style<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">hyperlink<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">is_date<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">number_format<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">parent<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">row<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">value<\/span><\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Referencing Cells\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#Assign a value to cell A1\nws&#91;'A1']=123\n#Assign a value to cell B2\nws&#91;'B2']='\u4f60\u597d'\n#Get the value from cell A1\ncl=ws&#91;'A1']\n\n#Return the Cell object\ncl=ws.cell(row=4, column=2, value=10)\n\n#Import the cell class\nfrom openpyxl.cell import cell\n#Use the Cell method to create a cell\ncl=cell.Cell(worksheet=ws, row=4, column=2, value=10)\n\ncl=ws&#91;'C3']    #Referencing a cell\nprint(cl.row)    #Row number of the cell\nprint(cl.column)    #Column number of the cell\nprint(cl.value)    #Value of the cell\nprint(cl.coordinate)    #Coordinates of the cell\nprint(cl.data_type)    #Data type of the cell value\ncl.hyperlink='https:\\\\www.baidu.com'    #Link in the cell\nprint(cl.hyperlink)\n#Offset to a cell at a specified position\n#(e.g., 1 row down, 2 columns right)\nh=cl.offset(row=1, column=2)\nprint(h.value)\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":[3],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/17","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=17"}],"version-history":[{"count":1,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":227,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/227"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}