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