[{"data":1,"prerenderedAt":1446},["ShallowReactive",2],{"doc:\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Fset-column-width-and-row-height-in-openpyxl":3,"surround:\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Fset-column-width-and-row-height-in-openpyxl":1438},{"id":4,"title":5,"body":6,"dateModified":1415,"datePublished":1415,"description":1416,"extension":1417,"faq":1418,"meta":1429,"navigation":204,"path":1430,"seo":1431,"slug":1434,"stem":1435,"type":1436,"__hash__":1437},"docs\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Fset-column-width-and-row-height-in-openpyxl\u002Findex.md","Set Column Width and Row Height in openpyxl",{"type":7,"value":8,"toc":1402},"minimark",[9,19,146,151,176,412,416,431,577,596,600,611,717,721,731,930,937,941,948,1035,1041,1045,1052,1142,1146,1282,1286,1289,1293,1303,1309,1321,1333,1346,1350,1371,1375,1398],[10,11,12,13,18],"p",{},"openpyxl gives you direct control over how wide each column is and how tall each row is, but the units are not pixels and there is no real auto-fit — you compute it yourself. This guide, part of ",[14,15,17],"a",{"href":16},"\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002F","Styling Excel Cells with openpyxl",", shows the exact calls, an auto-fit pass you can drop into any generator, plus hiding and defaults. Every block runs in order against a sample workbook built first.",[20,21,29,30,29,34,29,38,29,48,29,52,29,56,29,59,29,66,29,70,29,73,29,76,29,81,29,85,29,88,29,93,29,98,29,101,29,104,29,107,29,113,29,117,29,124,29,130,29,134,29,137,29,142],"svg",{"viewBox":22,"role":23,"ariaLabelledBy":24,"xmlns":27,"style":28},"0 0 760 250","img",[25,26],"dim-t","dim-d","http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg","width:100%;max-width:760px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif","\n  ",[31,32,33],"title",{"id":25},"Column width in character units and row height in points",[35,36,37],"desc",{"id":26},"A small grid shows column width measured in character units along the top and row height measured in points down the side, set through column_dimensions and row_dimensions.",[39,40],"rect",{"x":41,"y":42,"width":43,"height":44,"rx":45,"fill":46,"stroke":47},"180","70","320","150","4","var(--surface-muted,#eef2ff)","var(--line,#cdd5e6)",[49,50],"line",{"x1":43,"y1":42,"x2":43,"y2":51,"stroke":47},"220",[49,53],{"x1":41,"y1":54,"x2":55,"y2":54,"stroke":47},"120","500",[49,57],{"x1":41,"y1":58,"x2":55,"y2":58,"stroke":47},"170",[60,61,65],"text",{"x":62,"y":63,"style":64},"250","100","font-size:12px;fill:var(--text,#172033);text-anchor:middle","A1",[60,67,69],{"x":68,"y":63,"style":64},"410","B1",[60,71,72],{"x":62,"y":44,"style":64},"A2",[60,74,75],{"x":68,"y":44,"style":64},"B2",[49,77],{"x1":41,"y1":78,"x2":43,"y2":78,"stroke":79,"style":80},"48","var(--brand,#5b5cf0)","stroke-width:2px",[82,83],"polygon",{"points":84,"fill":79},"180,48 190,43 190,53",[82,86],{"points":87,"fill":79},"320,48 310,43 310,53",[60,89,92],{"x":62,"y":90,"style":91},"38","font-size:13px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","width = 18",[60,94,97],{"x":62,"y":95,"style":96},"64","font-size:11px;fill:var(--muted,#5b6780);text-anchor:middle","character units",[49,99],{"x1":44,"y1":42,"x2":44,"y2":54,"stroke":100,"style":80},"var(--accent,#f43f8f)",[82,102],{"points":103,"fill":100},"150,70 145,80 155,80",[82,105],{"points":106,"fill":100},"150,120 145,110 155,110",[60,108,112],{"x":109,"y":110,"style":111},"60","92","font-size:13px;font-weight:700;fill:var(--accent,#f43f8f);text-anchor:middle","height",[60,114,116],{"x":109,"y":115,"style":111},"108","= 30 pt",[39,118],{"x":119,"y":120,"width":121,"height":109,"rx":122,"fill":123,"stroke":79},"540","78","200","10","var(--brand-soft,rgba(91,92,240,0.12))",[60,125,129],{"x":126,"y":127,"style":128},"640","104","font-size:12px;font-weight:600;fill:var(--brand-strong,#4338ca);text-anchor:middle","column_dimensions",[60,131,133],{"x":126,"y":132,"style":96},"124","[\"A\"].width",[39,135],{"x":119,"y":44,"width":121,"height":109,"rx":122,"fill":136,"stroke":100},"rgba(244,63,143,0.12)",[60,138,141],{"x":126,"y":139,"style":140},"176","font-size:12px;font-weight:600;fill:var(--accent,#f43f8f);text-anchor:middle","row_dimensions",[60,143,145],{"x":126,"y":144,"style":96},"196","[1].height",[147,148,150],"h2",{"id":149},"install-and-create-a-sample-workbook","Install and create a sample workbook",[152,153,158],"pre",{"className":154,"code":155,"language":156,"meta":157,"style":157},"language-bash shiki shiki-themes github-light github-dark","pip install openpyxl\n","bash","",[159,160,161],"code",{"__ignoreMap":157},[162,163,165,169,173],"span",{"class":49,"line":164},1,[162,166,168],{"class":167},"sScJk","pip",[162,170,172],{"class":171},"sZZnC"," install",[162,174,175],{"class":171}," openpyxl\n",[152,177,181],{"className":178,"code":179,"language":180,"meta":157,"style":157},"language-python shiki shiki-themes github-light github-dark","from openpyxl import Workbook\n\nwb = Workbook()\nws = wb.active\nws.title = \"Inventory\"\n\nws.append([\"SKU\", \"Product Name\", \"Warehouse Location\", \"Qty\"])\nfor row in [\n    [\"A-1001\", \"Stainless Steel Water Bottle\", \"Aisle 12, Bin 4\", 340],\n    [\"A-1002\", \"Bamboo Cutting Board\",         \"Aisle 3, Bin 19\", 88],\n    [\"A-1003\", \"Ceramic Pour-Over Dripper\",    \"Aisle 7, Bin 2\",  152],\n]:\n    ws.append(row)\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Sample workbook created\")\n","python",[159,182,183,199,206,218,229,240,245,273,288,316,342,369,375,381,386,398],{"__ignoreMap":157},[162,184,185,189,193,196],{"class":49,"line":164},[162,186,188],{"class":187},"szBVR","from",[162,190,192],{"class":191},"sVt8B"," openpyxl ",[162,194,195],{"class":187},"import",[162,197,198],{"class":191}," Workbook\n",[162,200,202],{"class":49,"line":201},2,[162,203,205],{"emptyLinePlaceholder":204},true,"\n",[162,207,209,212,215],{"class":49,"line":208},3,[162,210,211],{"class":191},"wb ",[162,213,214],{"class":187},"=",[162,216,217],{"class":191}," Workbook()\n",[162,219,221,224,226],{"class":49,"line":220},4,[162,222,223],{"class":191},"ws ",[162,225,214],{"class":187},[162,227,228],{"class":191}," wb.active\n",[162,230,232,235,237],{"class":49,"line":231},5,[162,233,234],{"class":191},"ws.title ",[162,236,214],{"class":187},[162,238,239],{"class":171}," \"Inventory\"\n",[162,241,243],{"class":49,"line":242},6,[162,244,205],{"emptyLinePlaceholder":204},[162,246,248,251,254,257,260,262,265,267,270],{"class":49,"line":247},7,[162,249,250],{"class":191},"ws.append([",[162,252,253],{"class":171},"\"SKU\"",[162,255,256],{"class":191},", ",[162,258,259],{"class":171},"\"Product Name\"",[162,261,256],{"class":191},[162,263,264],{"class":171},"\"Warehouse Location\"",[162,266,256],{"class":191},[162,268,269],{"class":171},"\"Qty\"",[162,271,272],{"class":191},"])\n",[162,274,276,279,282,285],{"class":49,"line":275},8,[162,277,278],{"class":187},"for",[162,280,281],{"class":191}," row ",[162,283,284],{"class":187},"in",[162,286,287],{"class":191}," [\n",[162,289,291,294,297,299,302,304,307,309,313],{"class":49,"line":290},9,[162,292,293],{"class":191},"    [",[162,295,296],{"class":171},"\"A-1001\"",[162,298,256],{"class":191},[162,300,301],{"class":171},"\"Stainless Steel Water Bottle\"",[162,303,256],{"class":191},[162,305,306],{"class":171},"\"Aisle 12, Bin 4\"",[162,308,256],{"class":191},[162,310,312],{"class":311},"sj4cs","340",[162,314,315],{"class":191},"],\n",[162,317,319,321,324,326,329,332,335,337,340],{"class":49,"line":318},10,[162,320,293],{"class":191},[162,322,323],{"class":171},"\"A-1002\"",[162,325,256],{"class":191},[162,327,328],{"class":171},"\"Bamboo Cutting Board\"",[162,330,331],{"class":191},",         ",[162,333,334],{"class":171},"\"Aisle 3, Bin 19\"",[162,336,256],{"class":191},[162,338,339],{"class":311},"88",[162,341,315],{"class":191},[162,343,345,347,350,352,355,358,361,364,367],{"class":49,"line":344},11,[162,346,293],{"class":191},[162,348,349],{"class":171},"\"A-1003\"",[162,351,256],{"class":191},[162,353,354],{"class":171},"\"Ceramic Pour-Over Dripper\"",[162,356,357],{"class":191},",    ",[162,359,360],{"class":171},"\"Aisle 7, Bin 2\"",[162,362,363],{"class":191},",  ",[162,365,366],{"class":311},"152",[162,368,315],{"class":191},[162,370,372],{"class":49,"line":371},12,[162,373,374],{"class":191},"]:\n",[162,376,378],{"class":49,"line":377},13,[162,379,380],{"class":191},"    ws.append(row)\n",[162,382,384],{"class":49,"line":383},14,[162,385,205],{"emptyLinePlaceholder":204},[162,387,389,392,395],{"class":49,"line":388},15,[162,390,391],{"class":191},"wb.save(",[162,393,394],{"class":171},"\"sized_report.xlsx\"",[162,396,397],{"class":191},")\n",[162,399,401,404,407,410],{"class":49,"line":400},16,[162,402,403],{"class":311},"print",[162,405,406],{"class":191},"(",[162,408,409],{"class":171},"\"Sample workbook created\"",[162,411,397],{"class":191},[147,413,415],{"id":414},"set-column-width","Set column width",[10,417,418,419,422,423,427,428,430],{},"Set a column's width through ",[159,420,421],{},"ws.column_dimensions[letter].width",". The unit is approximately the number of characters of the default font that fit in the column — ",[424,425,426],"strong",{},"not"," pixels. A width of ",[159,429,122],{}," shows roughly 10 characters:",[152,432,434],{"className":178,"code":433,"language":180,"meta":157,"style":157},"from openpyxl import load_workbook\n\nwb = load_workbook(\"sized_report.xlsx\")\nws = wb[\"Inventory\"]\n\nws.column_dimensions[\"A\"].width = 12   # SKU\nws.column_dimensions[\"B\"].width = 32   # Product Name (longest text)\nws.column_dimensions[\"C\"].width = 22   # Warehouse Location\nws.column_dimensions[\"D\"].width = 8    # Qty\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Column widths set\")\n",[159,435,436,447,451,464,479,483,503,520,537,554,558,566],{"__ignoreMap":157},[162,437,438,440,442,444],{"class":49,"line":164},[162,439,188],{"class":187},[162,441,192],{"class":191},[162,443,195],{"class":187},[162,445,446],{"class":191}," load_workbook\n",[162,448,449],{"class":49,"line":201},[162,450,205],{"emptyLinePlaceholder":204},[162,452,453,455,457,460,462],{"class":49,"line":208},[162,454,211],{"class":191},[162,456,214],{"class":187},[162,458,459],{"class":191}," load_workbook(",[162,461,394],{"class":171},[162,463,397],{"class":191},[162,465,466,468,470,473,476],{"class":49,"line":220},[162,467,223],{"class":191},[162,469,214],{"class":187},[162,471,472],{"class":191}," wb[",[162,474,475],{"class":171},"\"Inventory\"",[162,477,478],{"class":191},"]\n",[162,480,481],{"class":49,"line":231},[162,482,205],{"emptyLinePlaceholder":204},[162,484,485,488,491,494,496,499],{"class":49,"line":242},[162,486,487],{"class":191},"ws.column_dimensions[",[162,489,490],{"class":171},"\"A\"",[162,492,493],{"class":191},"].width ",[162,495,214],{"class":187},[162,497,498],{"class":311}," 12",[162,500,502],{"class":501},"sJ8bj","   # SKU\n",[162,504,505,507,510,512,514,517],{"class":49,"line":247},[162,506,487],{"class":191},[162,508,509],{"class":171},"\"B\"",[162,511,493],{"class":191},[162,513,214],{"class":187},[162,515,516],{"class":311}," 32",[162,518,519],{"class":501},"   # Product Name (longest text)\n",[162,521,522,524,527,529,531,534],{"class":49,"line":275},[162,523,487],{"class":191},[162,525,526],{"class":171},"\"C\"",[162,528,493],{"class":191},[162,530,214],{"class":187},[162,532,533],{"class":311}," 22",[162,535,536],{"class":501},"   # Warehouse Location\n",[162,538,539,541,544,546,548,551],{"class":49,"line":290},[162,540,487],{"class":191},[162,542,543],{"class":171},"\"D\"",[162,545,493],{"class":191},[162,547,214],{"class":187},[162,549,550],{"class":311}," 8",[162,552,553],{"class":501},"    # Qty\n",[162,555,556],{"class":49,"line":318},[162,557,205],{"emptyLinePlaceholder":204},[162,559,560,562,564],{"class":49,"line":344},[162,561,391],{"class":191},[162,563,394],{"class":171},[162,565,397],{"class":191},[162,567,568,570,572,575],{"class":49,"line":371},[162,569,403],{"class":311},[162,571,406],{"class":191},[162,573,574],{"class":171},"\"Column widths set\"",[162,576,397],{"class":191},[10,578,579,580,582,583,586,587,256,589,591,592,595],{},"You always index ",[159,581,129],{}," by the ",[424,584,585],{},"column letter"," (",[159,588,490],{},[159,590,509],{},", ...), never by a number. If you have a 1-based index, convert it with ",[159,593,594],{},"get_column_letter",".",[147,597,599],{"id":598},"set-row-height","Set row height",[10,601,602,603,606,607,610],{},"Set a row's height through ",[159,604,605],{},"ws.row_dimensions[index].height",". Here the unit is ",[424,608,609],{},"points"," (1\u002F72 inch), the same unit as font size. Give the header room and bump a data row:",[152,612,614],{"className":178,"code":613,"language":180,"meta":157,"style":157},"from openpyxl import load_workbook\n\nwb = load_workbook(\"sized_report.xlsx\")\nws = wb[\"Inventory\"]\n\nws.row_dimensions[1].height = 24   # taller header\nws.row_dimensions[2].height = 18   # one roomier data row\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Row heights set\")\n",[159,615,616,626,630,642,654,658,677,694,698,706],{"__ignoreMap":157},[162,617,618,620,622,624],{"class":49,"line":164},[162,619,188],{"class":187},[162,621,192],{"class":191},[162,623,195],{"class":187},[162,625,446],{"class":191},[162,627,628],{"class":49,"line":201},[162,629,205],{"emptyLinePlaceholder":204},[162,631,632,634,636,638,640],{"class":49,"line":208},[162,633,211],{"class":191},[162,635,214],{"class":187},[162,637,459],{"class":191},[162,639,394],{"class":171},[162,641,397],{"class":191},[162,643,644,646,648,650,652],{"class":49,"line":220},[162,645,223],{"class":191},[162,647,214],{"class":187},[162,649,472],{"class":191},[162,651,475],{"class":171},[162,653,478],{"class":191},[162,655,656],{"class":49,"line":231},[162,657,205],{"emptyLinePlaceholder":204},[162,659,660,663,666,669,671,674],{"class":49,"line":242},[162,661,662],{"class":191},"ws.row_dimensions[",[162,664,665],{"class":311},"1",[162,667,668],{"class":191},"].height ",[162,670,214],{"class":187},[162,672,673],{"class":311}," 24",[162,675,676],{"class":501},"   # taller header\n",[162,678,679,681,684,686,688,691],{"class":49,"line":247},[162,680,662],{"class":191},[162,682,683],{"class":311},"2",[162,685,668],{"class":191},[162,687,214],{"class":187},[162,689,690],{"class":311}," 18",[162,692,693],{"class":501},"   # one roomier data row\n",[162,695,696],{"class":49,"line":275},[162,697,205],{"emptyLinePlaceholder":204},[162,699,700,702,704],{"class":49,"line":290},[162,701,391],{"class":191},[162,703,394],{"class":171},[162,705,397],{"class":191},[162,707,708,710,712,715],{"class":49,"line":318},[162,709,403],{"class":311},[162,711,406],{"class":191},[162,713,714],{"class":171},"\"Row heights set\"",[162,716,397],{"class":191},[147,718,720],{"id":719},"auto-fit-columns-by-measuring-content","Auto-fit columns by measuring content",[10,722,723,724,727,728,730],{},"openpyxl has no true auto-fit, so you compute a width from the longest value in each column. Walk every cell, take ",[159,725,726],{},"len(str(value))",", track the per-column maximum, add a small padding, then assign it. Convert the column index to a letter with ",[159,729,594],{},":",[152,732,734],{"className":178,"code":733,"language":180,"meta":157,"style":157},"from openpyxl import load_workbook\nfrom openpyxl.utils import get_column_letter\n\nwb = load_workbook(\"sized_report.xlsx\")\nws = wb[\"Inventory\"]\n\nfor col_cells in ws.columns:\n    longest = 0\n    letter = get_column_letter(col_cells[0].column)   # .column is 1-based int\n    for cell in col_cells:\n        if cell.value is not None:\n            longest = max(longest, len(str(cell.value)))\n    ws.column_dimensions[letter].width = longest + 2   # padding\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Columns auto-fitted\")\n",[159,735,736,746,758,762,774,786,790,802,812,831,844,864,888,907,911,919],{"__ignoreMap":157},[162,737,738,740,742,744],{"class":49,"line":164},[162,739,188],{"class":187},[162,741,192],{"class":191},[162,743,195],{"class":187},[162,745,446],{"class":191},[162,747,748,750,753,755],{"class":49,"line":201},[162,749,188],{"class":187},[162,751,752],{"class":191}," openpyxl.utils ",[162,754,195],{"class":187},[162,756,757],{"class":191}," get_column_letter\n",[162,759,760],{"class":49,"line":208},[162,761,205],{"emptyLinePlaceholder":204},[162,763,764,766,768,770,772],{"class":49,"line":220},[162,765,211],{"class":191},[162,767,214],{"class":187},[162,769,459],{"class":191},[162,771,394],{"class":171},[162,773,397],{"class":191},[162,775,776,778,780,782,784],{"class":49,"line":231},[162,777,223],{"class":191},[162,779,214],{"class":187},[162,781,472],{"class":191},[162,783,475],{"class":171},[162,785,478],{"class":191},[162,787,788],{"class":49,"line":242},[162,789,205],{"emptyLinePlaceholder":204},[162,791,792,794,797,799],{"class":49,"line":247},[162,793,278],{"class":187},[162,795,796],{"class":191}," col_cells ",[162,798,284],{"class":187},[162,800,801],{"class":191}," ws.columns:\n",[162,803,804,807,809],{"class":49,"line":275},[162,805,806],{"class":191},"    longest ",[162,808,214],{"class":187},[162,810,811],{"class":311}," 0\n",[162,813,814,817,819,822,825,828],{"class":49,"line":290},[162,815,816],{"class":191},"    letter ",[162,818,214],{"class":187},[162,820,821],{"class":191}," get_column_letter(col_cells[",[162,823,824],{"class":311},"0",[162,826,827],{"class":191},"].column)   ",[162,829,830],{"class":501},"# .column is 1-based int\n",[162,832,833,836,839,841],{"class":49,"line":318},[162,834,835],{"class":187},"    for",[162,837,838],{"class":191}," cell ",[162,840,284],{"class":187},[162,842,843],{"class":191}," col_cells:\n",[162,845,846,849,852,855,858,861],{"class":49,"line":344},[162,847,848],{"class":187},"        if",[162,850,851],{"class":191}," cell.value ",[162,853,854],{"class":187},"is",[162,856,857],{"class":187}," not",[162,859,860],{"class":311}," None",[162,862,863],{"class":191},":\n",[162,865,866,869,871,874,877,880,882,885],{"class":49,"line":371},[162,867,868],{"class":191},"            longest ",[162,870,214],{"class":187},[162,872,873],{"class":311}," max",[162,875,876],{"class":191},"(longest, ",[162,878,879],{"class":311},"len",[162,881,406],{"class":191},[162,883,884],{"class":311},"str",[162,886,887],{"class":191},"(cell.value)))\n",[162,889,890,893,895,898,901,904],{"class":49,"line":377},[162,891,892],{"class":191},"    ws.column_dimensions[letter].width ",[162,894,214],{"class":187},[162,896,897],{"class":191}," longest ",[162,899,900],{"class":187},"+",[162,902,903],{"class":311}," 2",[162,905,906],{"class":501},"   # padding\n",[162,908,909],{"class":49,"line":383},[162,910,205],{"emptyLinePlaceholder":204},[162,912,913,915,917],{"class":49,"line":388},[162,914,391],{"class":191},[162,916,394],{"class":171},[162,918,397],{"class":191},[162,920,921,923,925,928],{"class":49,"line":400},[162,922,403],{"class":311},[162,924,406],{"class":191},[162,926,927],{"class":171},"\"Columns auto-fitted\"",[162,929,397],{"class":191},[10,931,932,933,936],{},"This is an approximation: character count ignores font, bold weight, and proportional glyph widths, so wide content in a bold or large font may still clip slightly. Clamp the result with ",[159,934,935],{},"min(longest + 2, 60)"," if a single long value would otherwise blow out the layout.",[147,938,940],{"id":939},"hide-a-column","Hide a column",[10,942,943,944,947],{},"Hide a column without deleting its data by setting ",[159,945,946],{},"hidden=True"," on its dimension. The data stays in the file and reappears if the user unhides it:",[152,949,951],{"className":178,"code":950,"language":180,"meta":157,"style":157},"from openpyxl import load_workbook\n\nwb = load_workbook(\"sized_report.xlsx\")\nws = wb[\"Inventory\"]\n\nws.column_dimensions[\"C\"].hidden = True   # hide Warehouse Location\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Column C hidden\")\n",[159,952,953,963,967,979,991,995,1012,1016,1024],{"__ignoreMap":157},[162,954,955,957,959,961],{"class":49,"line":164},[162,956,188],{"class":187},[162,958,192],{"class":191},[162,960,195],{"class":187},[162,962,446],{"class":191},[162,964,965],{"class":49,"line":201},[162,966,205],{"emptyLinePlaceholder":204},[162,968,969,971,973,975,977],{"class":49,"line":208},[162,970,211],{"class":191},[162,972,214],{"class":187},[162,974,459],{"class":191},[162,976,394],{"class":171},[162,978,397],{"class":191},[162,980,981,983,985,987,989],{"class":49,"line":220},[162,982,223],{"class":191},[162,984,214],{"class":187},[162,986,472],{"class":191},[162,988,475],{"class":171},[162,990,478],{"class":191},[162,992,993],{"class":49,"line":231},[162,994,205],{"emptyLinePlaceholder":204},[162,996,997,999,1001,1004,1006,1009],{"class":49,"line":242},[162,998,487],{"class":191},[162,1000,526],{"class":171},[162,1002,1003],{"class":191},"].hidden ",[162,1005,214],{"class":187},[162,1007,1008],{"class":311}," True",[162,1010,1011],{"class":501},"   # hide Warehouse Location\n",[162,1013,1014],{"class":49,"line":247},[162,1015,205],{"emptyLinePlaceholder":204},[162,1017,1018,1020,1022],{"class":49,"line":275},[162,1019,391],{"class":191},[162,1021,394],{"class":171},[162,1023,397],{"class":191},[162,1025,1026,1028,1030,1033],{"class":49,"line":290},[162,1027,403],{"class":311},[162,1029,406],{"class":191},[162,1031,1032],{"class":171},"\"Column C hidden\"",[162,1034,397],{"class":191},[10,1036,1037,1038,595],{},"The same attribute exists for rows: ",[159,1039,1040],{},"ws.row_dimensions[2].hidden = True",[147,1042,1044],{"id":1043},"set-a-default-width-and-height","Set a default width and height",[10,1046,1047,1048,1051],{},"Apply a baseline size to every column or row through the sheet's ",[159,1049,1050],{},"sheet_format",". Per-column and per-row settings still override the default where you set them:",[152,1053,1055],{"className":178,"code":1054,"language":180,"meta":157,"style":157},"from openpyxl import load_workbook\n\nwb = load_workbook(\"sized_report.xlsx\")\nws = wb[\"Inventory\"]\n\nws.sheet_format.defaultColWidth = 15\nws.sheet_format.defaultRowHeight = 16\n\nwb.save(\"sized_report.xlsx\")\nprint(\"Defaults set\")\n",[159,1056,1057,1067,1071,1083,1095,1099,1109,1119,1123,1131],{"__ignoreMap":157},[162,1058,1059,1061,1063,1065],{"class":49,"line":164},[162,1060,188],{"class":187},[162,1062,192],{"class":191},[162,1064,195],{"class":187},[162,1066,446],{"class":191},[162,1068,1069],{"class":49,"line":201},[162,1070,205],{"emptyLinePlaceholder":204},[162,1072,1073,1075,1077,1079,1081],{"class":49,"line":208},[162,1074,211],{"class":191},[162,1076,214],{"class":187},[162,1078,459],{"class":191},[162,1080,394],{"class":171},[162,1082,397],{"class":191},[162,1084,1085,1087,1089,1091,1093],{"class":49,"line":220},[162,1086,223],{"class":191},[162,1088,214],{"class":187},[162,1090,472],{"class":191},[162,1092,475],{"class":171},[162,1094,478],{"class":191},[162,1096,1097],{"class":49,"line":231},[162,1098,205],{"emptyLinePlaceholder":204},[162,1100,1101,1104,1106],{"class":49,"line":242},[162,1102,1103],{"class":191},"ws.sheet_format.defaultColWidth ",[162,1105,214],{"class":187},[162,1107,1108],{"class":311}," 15\n",[162,1110,1111,1114,1116],{"class":49,"line":247},[162,1112,1113],{"class":191},"ws.sheet_format.defaultRowHeight ",[162,1115,214],{"class":187},[162,1117,1118],{"class":311}," 16\n",[162,1120,1121],{"class":49,"line":275},[162,1122,205],{"emptyLinePlaceholder":204},[162,1124,1125,1127,1129],{"class":49,"line":290},[162,1126,391],{"class":191},[162,1128,394],{"class":171},[162,1130,397],{"class":191},[162,1132,1133,1135,1137,1140],{"class":49,"line":318},[162,1134,403],{"class":311},[162,1136,406],{"class":191},[162,1138,1139],{"class":171},"\"Defaults set\"",[162,1141,397],{"class":191},[147,1143,1145],{"id":1144},"common-pitfalls","Common pitfalls",[1147,1148,1149,1165],"table",{},[1150,1151,1152],"thead",{},[1153,1154,1155,1159,1162],"tr",{},[1156,1157,1158],"th",{},"Symptom",[1156,1160,1161],{},"Cause",[1156,1163,1164],{},"Fix",[1166,1167,1168,1190,1214,1231,1251,1262],"tbody",{},[1153,1169,1170,1174,1181],{},[1171,1172,1173],"td",{},"Columns far too narrow or wide",[1171,1175,1176,1177,1180],{},"Treating ",[159,1178,1179],{},"width"," as pixels",[1171,1182,1183,1185,1186,1189],{},[159,1184,1179],{}," ≈ character count of the default font; a value of ",[159,1187,1188],{},"30"," is ~30 characters",[1153,1191,1192,1198,1204],{},[1171,1193,1194,1197],{},[159,1195,1196],{},"KeyError"," or no effect when sizing a column",[1171,1199,1200,1201,1203],{},"Indexing ",[159,1202,129],{}," by a number",[1171,1205,1206,1207,1210,1211],{},"Use the letter: ",[159,1208,1209],{},"column_dimensions[\"B\"]",", or convert with ",[159,1212,1213],{},"get_column_letter(idx)",[1153,1215,1216,1219,1222],{},[1171,1217,1218],{},"Row height ignored",[1171,1220,1221],{},"Confusing height units with width units",[1171,1223,1224,1225,1227,1228,1230],{},"Row ",[159,1226,112],{}," is in ",[424,1229,609],{}," (like font size), not characters",[1153,1232,1233,1236,1242],{},[1171,1234,1235],{},"\"Auto-fit\" call not found",[1171,1237,1238,1239],{},"openpyxl has no ",[159,1240,1241],{},"autofit()",[1171,1243,1244,1245,1247,1248,1250],{},"Measure ",[159,1246,726],{}," per column and set ",[159,1249,1179],{}," yourself",[1153,1252,1253,1256,1259],{},[1171,1254,1255],{},"Wide font still clips",[1171,1257,1258],{},"Character count ignores bold\u002Flarge fonts",[1171,1260,1261],{},"Add extra padding or measure against the actual font if exactness matters",[1153,1263,1264,1267,1273],{},[1171,1265,1266],{},"Hidden column lost its data",[1171,1268,1269,1270,1272],{},"Confusing ",[159,1271,946],{}," with deletion",[1171,1274,1275,1277,1278,1281],{},[159,1276,946],{}," keeps the data; only ",[159,1279,1280],{},"ws.delete_cols()"," removes it",[147,1283,1285],{"id":1284},"a-note-on-scale","A note on scale",[10,1287,1288],{},"The auto-fit pass iterates every cell, so for sheets with hundreds of thousands of rows it gets slow and memory-heavy. Sample the first few hundred rows, or cap the measured length, instead of scanning the whole column — the header plus a representative slice usually gives a good enough width.",[147,1290,1292],{"id":1291},"frequently-asked-questions","Frequently asked questions",[10,1294,1295,1298,1299,1302],{},[424,1296,1297],{},"What unit is column width in openpyxl?","\nRoughly the number of characters of the workbook's default font that fit in the column. It is not pixels and not points. A width of ",[159,1300,1301],{},"20"," shows about 20 characters.",[10,1304,1305,1308],{},[424,1306,1307],{},"What unit is row height?","\nPoints — 1\u002F72 of an inch, the same unit used for font size. A 14-point font fits comfortably in a row about 18 points tall.",[10,1310,1311,1314,1315,1317,1318,595],{},[424,1312,1313],{},"How do I auto-fit columns?","\nopenpyxl cannot measure rendered text, so there is no built-in auto-fit. Loop the cells, take the longest ",[159,1316,726],{}," per column, add padding, and assign it to ",[159,1319,1320],{},"column_dimensions[letter].width",[10,1322,1323,1326,1327,1329,1330,595],{},[424,1324,1325],{},"Why do I get a KeyError when setting a width?","\nYou indexed ",[159,1328,129],{}," with a number. It is keyed by column letter. Convert an index with ",[159,1331,1332],{},"from openpyxl.utils import get_column_letter",[10,1334,1335,1338,1339,1342,1343,1345],{},[424,1336,1337],{},"Does hiding a column delete its data?","\nNo. ",[159,1340,1341],{},"ws.column_dimensions[\"C\"].hidden = True"," only hides it; the values remain in the file and reappear when unhidden. Use ",[159,1344,1280],{}," to actually remove a column.",[147,1347,1349],{"id":1348},"conclusion","Conclusion",[10,1351,1352,1353,1355,1356,1358,1359,1361,1362,1364,1365,1367,1368,1370],{},"Column width is set in approximate character units through ",[159,1354,421],{},", and row height in points through ",[159,1357,605],{},". There is no native auto-fit, so measure ",[159,1360,726],{}," per column and pad the result — clamping it for safety. Index columns by letter, convert from numbers with ",[159,1363,594],{},", and use ",[159,1366,946],{}," plus ",[159,1369,1050],{}," defaults to round out the layout.",[147,1372,1374],{"id":1373},"where-to-go-next","Where to go next",[1376,1377,1378,1384,1391],"ul",{},[1379,1380,1381,1383],"li",{},[14,1382,17],{"href":16}," — the parent cluster covering fonts, fills, borders, and alignment.",[1379,1385,1386,1390],{},[14,1387,1389],{"href":1388},"\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Ffreeze-header-row-in-excel-with-openpyxl\u002F","Freeze the Header Row in Excel with openpyxl"," — keep the header visible once the sheet is wide.",[1379,1392,1393,1397],{},[14,1394,1396],{"href":1395},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002F","Using openpyxl for Excel File Manipulation"," — workbook fundamentals behind these sizing calls.",[1399,1400,1401],"style",{},"html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":157,"searchDepth":201,"depth":201,"links":1403},[1404,1405,1406,1407,1408,1409,1410,1411,1412,1413,1414],{"id":149,"depth":201,"text":150},{"id":414,"depth":201,"text":415},{"id":598,"depth":201,"text":599},{"id":719,"depth":201,"text":720},{"id":939,"depth":201,"text":940},{"id":1043,"depth":201,"text":1044},{"id":1144,"depth":201,"text":1145},{"id":1284,"depth":201,"text":1285},{"id":1291,"depth":201,"text":1292},{"id":1348,"depth":201,"text":1349},{"id":1373,"depth":201,"text":1374},"2026-06-18","Set Excel column widths and row heights from Python with openpyxl, build an auto-fit pass by measuring content, hide columns, and set sensible defaults — runnable.","md",[1419,1421,1423,1425,1427],{"q":1297,"a":1420},"Roughly the number of characters of the workbook's default font that fit in the column. It is not pixels and not points. A width of 20 shows about 20 characters.",{"q":1307,"a":1422},"Points — 1\u002F72 of an inch, the same unit used for font size. A 14-point font fits comfortably in a row about 18 points tall.",{"q":1313,"a":1424},"openpyxl cannot measure rendered text, so there is no built-in auto-fit. Loop the cells, take the longest len(str(value)) per column, add padding, and assign it to column_dimensions[letter].width.",{"q":1325,"a":1426},"You indexed column_dimensions with a number. It is keyed by column letter. Convert an index with from openpyxl.utils import get_column_letter.",{"q":1337,"a":1428},"No. ws.column_dimensions[\"C\"].hidden = True only hides it; the values remain in the file and reappear when unhidden. Use ws.delete_cols() to actually remove a column.",{},"\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Fset-column-width-and-row-height-in-openpyxl",{"title":1432,"description":1433},"Column Width & Row Height in openpyxl","Control Excel column width and row height in Python with openpyxl: unit gotchas, an auto-fit pattern that measures content, hidden columns, and default sizes.","set-column-width-and-row-height-in-openpyxl","formatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Fset-column-width-and-row-height-in-openpyxl\u002Findex","long_tail","nNQLh8xeELraWLRL672U9yBmIBvIX_0fS0fhX6tG5Ns",[1439,1442],{"title":1389,"path":1440,"stem":1441,"children":-1},"\u002Fformatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Ffreeze-header-row-in-excel-with-openpyxl","formatting-and-charting-excel-reports-with-python\u002Fstyling-excel-cells-with-openpyxl\u002Ffreeze-header-row-in-excel-with-openpyxl\u002Findex",{"title":1443,"path":1444,"stem":1445,"children":-1},"Getting Started with Python Excel Automation","\u002Fgetting-started-with-python-excel-automation","getting-started-with-python-excel-automation\u002Findex",1781795518476]