[{"data":1,"prerenderedAt":1978},["ShallowReactive",2],{"doc:\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl":3,"surround:\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl":1943,"ancestors:\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl":1951,"children:\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl":1956,"related:\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl":1957},{"id":4,"title":5,"body":6,"dateModified":1907,"datePublished":1907,"description":1908,"extension":1909,"faq":1910,"keyword":1920,"kind":1921,"meta":1922,"navigation":278,"path":1931,"seo":1932,"slug":1935,"stem":1936,"topics":1937,"type":1941,"__hash__":1942},"docs\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl\u002Findex.md","Set the Active Sheet, Selected Cell and Tab Colours with openpyxl",{"type":7,"value":8,"toc":1892},"minimark",[9,19,201,206,234,240,244,250,332,349,353,367,410,417,542,552,690,694,705,889,904,908,915,1036,1046,1050,1053,1135,1147,1297,1301,1307,1578,1586,1590,1597,1724,1739,1743,1784,1788,1791,1795,1824,1828,1837,1843,1849,1855,1859,1888],[10,11,12,13,18],"p",{},"The first second after a reader opens a report decides a lot. A workbook that opens on the raw data tab, scrolled to row 4,000 because that is where the script last wrote, feels like a dump; one that opens on the summary, top-left, with the key tabs coloured, feels designed. openpyxl controls all of that, but one of the settings has a trap that leaves sheets grouped together — and edits made to grouped sheets apply to all of them. This page, part of ",[14,15,17],"a",{"href":16},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002F","Using openpyxl for Excel File Manipulation",", covers the active sheet, the selected cell, the scroll position and tab colours.",[20,21,29,30,29,34,29,38,29,46,29,53,29,63,29,68,29,79,29,85,29,91,29,94,29,100,29,104,29,108,29,112,29,117,29,121,29,125,29,127,29,131,29,134,29,138,29,140,29,144,29,146,29,149,29,152,29,156,29,158,29,161,29,163,29,166,29,171,29,178,29,184,29,188,29,193,29,197],"svg",{"viewBox":22,"role":23,"ariaLabelledBy":24,"xmlns":27,"style":28},"256 0 252 226","img",[25,26],"act-open-t","act-open-d","http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg","width:100%;max-width:252px;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},"A workbook set up to open on its summary",[35,36,37],"desc",{"id":26},"The Summary sheet is in front with cell A1 selected, the view scrolled to the top, and three tabs along the bottom. Summary is the active tab; Data and Notes follow. The Summary tab is coloured to stand out.",[39,40],"rect",{"x":41,"y":42,"width":43,"height":44,"fill":45},"256","0","252","226","#ffffff",[47,48,52],"text",{"x":49,"y":50,"style":51},"380.0","31.0","font-size:14px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","What the reader sees on open",[39,54],{"x":55,"y":56,"width":57,"height":58,"rx":59,"fill":60,"stroke":61,"style":62},"291.6","48.0","176.9","30.0","8","#e7ebef","var(--line,#cdd5e6)","stroke-width:1.5px",[39,64],{"x":55,"y":65,"width":57,"height":66,"rx":42,"fill":60,"stroke":61,"style":67},"70.0","8.0","stroke-width:0px",[69,70,72],"g",{"transform":71},"translate(301.6,55.0) scale(0.667)",[73,74],"path",{"d":75,"fill":76,"stroke":77,"style":78},"M6 3h9l4 4v14H6z M15 3v4h4 M9.5 11.5l4.5 6 M14 11.5l-4.5 6","none","var(--teal-ink,#0b6157)","stroke-width:2.70px;stroke-linecap:round;stroke-linejoin:round",[47,80,84],{"x":81,"y":82,"style":83},"323.6","68.0","font-size:12px;font-weight:700;fill:var(--text,#172033);text-anchor:start","report.xlsx",[39,86],{"x":55,"y":87,"width":88,"height":89,"rx":42,"fill":60,"stroke":61,"style":90},"78.0","32.0","24.0","stroke-width:1px",[39,92],{"x":81,"y":87,"width":93,"height":89,"rx":42,"fill":60,"stroke":61,"style":90},"78.1",[47,95,99],{"x":96,"y":97,"style":98},"362.6","95.0","font-size:11px;font-weight:600;fill:var(--muted,#5b6780);text-anchor:middle","A",[39,101],{"x":102,"y":87,"width":103,"height":89,"rx":42,"fill":60,"stroke":61,"style":90},"401.6","66.8",[47,105,107],{"x":106,"y":97,"style":98},"435.0","B",[39,109],{"x":55,"y":110,"width":88,"height":111,"rx":42,"fill":60,"stroke":61,"style":90},"102.0","28.0",[47,113,116],{"x":114,"y":115,"style":98},"307.6","121.0","1",[39,118],{"x":81,"y":110,"width":93,"height":111,"rx":42,"fill":119,"stroke":120,"style":62},"#d9f4f1","var(--teal,#0f9488)",[47,122,124],{"x":96,"y":115,"style":123},"font-size:12px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","Metric",[39,126],{"x":102,"y":110,"width":103,"height":111,"rx":42,"fill":45,"stroke":61,"style":90},[47,128,130],{"x":106,"y":115,"style":129},"font-size:12px;font-weight:700;fill:var(--text,#172033);text-anchor:middle","Value",[39,132],{"x":55,"y":133,"width":88,"height":111,"rx":42,"fill":60,"stroke":61,"style":90},"130.0",[47,135,137],{"x":114,"y":136,"style":98},"149.0","2",[39,139],{"x":81,"y":133,"width":93,"height":111,"rx":42,"fill":45,"stroke":61,"style":90},[47,141,143],{"x":96,"y":136,"style":142},"font-size:12px;font-weight:400;fill:var(--text,#172033);text-anchor:middle","Revenue",[39,145],{"x":102,"y":133,"width":103,"height":111,"rx":42,"fill":45,"stroke":61,"style":90},[47,147,148],{"x":106,"y":136,"style":142},"48,210",[39,150],{"x":55,"y":151,"width":88,"height":111,"rx":42,"fill":60,"stroke":61,"style":90},"158.0",[47,153,155],{"x":114,"y":154,"style":98},"177.0","3",[39,157],{"x":81,"y":151,"width":93,"height":111,"rx":42,"fill":45,"stroke":61,"style":90},[47,159,160],{"x":96,"y":154,"style":142},"Orders",[39,162],{"x":102,"y":151,"width":103,"height":111,"rx":42,"fill":45,"stroke":61,"style":90},[47,164,165],{"x":106,"y":154,"style":142},"1,310",[39,167],{"x":168,"y":169,"width":170,"height":89,"rx":42,"fill":45,"stroke":61,"style":90},"299.6","186.0","80.7",[172,173],"line",{"x1":174,"y1":175,"x2":176,"y2":175,"stroke":120,"style":177},"301.6","209.0","378.3","stroke-width:3px",[47,179,183],{"x":180,"y":181,"style":182},"339.9","202.5","font-size:11.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","Summary",[39,185],{"x":186,"y":169,"width":187,"height":89,"rx":42,"fill":60,"stroke":61,"style":90},"382.3","50.2",[47,189,192],{"x":190,"y":181,"style":191},"407.4","font-size:11.5px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:middle","Data",[39,194],{"x":195,"y":169,"width":196,"height":89,"rx":42,"fill":60,"stroke":61,"style":90},"434.5","57.7",[47,198,200],{"x":199,"y":181,"style":191},"463.3","Notes",[202,203,205],"h2",{"id":204},"prerequisites","Prerequisites",[207,208,213],"pre",{"className":209,"code":210,"language":211,"meta":212,"style":212},"language-bash shiki shiki-themes github-light github-dark-high-contrast","pip install openpyxl xlsxwriter\n","bash","",[214,215,216],"code",{"__ignoreMap":212},[217,218,220,224,228,231],"span",{"class":172,"line":219},1,[217,221,223],{"class":222},"sMTad","pip",[217,225,227],{"class":226},"srMev"," install",[217,229,230],{"class":226}," openpyxl",[217,232,233],{"class":226}," xlsxwriter\n",[10,235,236,237,239],{},"The examples use ",[214,238,84],{},", which has three sheets — Data, Summary and Notes — and was last \"saved\" with the Data tab selected, the way a file saved by Excel would be.",[202,241,243],{"id":242},"step-1-put-a-sheet-in-front-with-wbactive","Step 1 — Put a sheet in front with wb.active",[10,245,246,249],{},[214,247,248],{},"wb.active"," accepts a sheet's index or the worksheet object itself. Using the object is clearer and does not break when sheets are reordered:",[207,251,255],{"className":252,"code":253,"language":254,"meta":212,"style":212},"language-python shiki shiki-themes github-light github-dark-high-contrast","from openpyxl import load_workbook\n\nwb = load_workbook(\"report.xlsx\")\nwb.active = wb[\"Summary\"]\nprint(wb.active.title)\n# Summary\n","python",[214,256,257,273,280,298,315,325],{"__ignoreMap":212},[217,258,259,263,267,270],{"class":172,"line":219},[217,260,262],{"class":261},"s-kum","from",[217,264,266],{"class":265},"skGVy"," openpyxl ",[217,268,269],{"class":261},"import",[217,271,272],{"class":265}," load_workbook\n",[217,274,276],{"class":172,"line":275},2,[217,277,279],{"emptyLinePlaceholder":278},true,"\n",[217,281,283,286,289,292,295],{"class":172,"line":282},3,[217,284,285],{"class":265},"wb ",[217,287,288],{"class":261},"=",[217,290,291],{"class":265}," load_workbook(",[217,293,294],{"class":226},"\"report.xlsx\"",[217,296,297],{"class":265},")\n",[217,299,301,304,306,309,312],{"class":172,"line":300},4,[217,302,303],{"class":265},"wb.active ",[217,305,288],{"class":261},[217,307,308],{"class":265}," wb[",[217,310,311],{"class":226},"\"Summary\"",[217,313,314],{"class":265},"]\n",[217,316,318,322],{"class":172,"line":317},5,[217,319,321],{"class":320},"sP0c6","print",[217,323,324],{"class":265},"(wb.active.title)\n",[217,326,328],{"class":172,"line":327},6,[217,329,331],{"class":330},"s-wDw","# Summary\n",[10,333,334,335,339,340,344,345,348],{},"Reordering the tabs is separate from choosing the active one. To make Summary the ",[336,337,338],"em",{},"first"," tab as well, move it — ",[14,341,343],{"href":342},"\u002Fgetting-started-with-python-excel-automation\u002Fworking-with-multiple-excel-sheets-in-python\u002Frename-reorder-and-delete-excel-sheets-with-openpyxl\u002F","Rename, Reorder and Delete Excel Sheets with openpyxl"," shows ",[214,346,347],{},"wb.move_sheet",".",[202,350,352],{"id":351},"step-2-clear-tabselected-so-sheets-are-not-grouped","Step 2 — Clear tabSelected so sheets are not grouped",[10,354,355,356,359,360,362,363,366],{},"Each worksheet also has a ",[214,357,358],{},"tabSelected"," flag, which Excel writes for every sheet that was selected when the file was saved. openpyxl does not touch it when you change ",[214,361,248],{},", so a file last saved on the Data tab keeps Data selected ",[336,364,365],{},"as well as"," the new active sheet:",[207,368,370],{"className":252,"code":369,"language":254,"meta":212,"style":212},"wb.save(\"grouped.xlsx\")\nprint([(ws.title, ws.sheet_view.tabSelected) for ws in load_workbook(\"grouped.xlsx\").worksheets])\n# [('Data', True), ('Summary', None), ('Notes', None)]\n",[214,371,372,382,405],{"__ignoreMap":212},[217,373,374,377,380],{"class":172,"line":219},[217,375,376],{"class":265},"wb.save(",[217,378,379],{"class":226},"\"grouped.xlsx\"",[217,381,297],{"class":265},[217,383,384,386,389,392,395,398,400,402],{"class":172,"line":275},[217,385,321],{"class":320},[217,387,388],{"class":265},"([(ws.title, ws.sheet_view.tabSelected) ",[217,390,391],{"class":261},"for",[217,393,394],{"class":265}," ws ",[217,396,397],{"class":261},"in",[217,399,291],{"class":265},[217,401,379],{"class":226},[217,403,404],{"class":265},").worksheets])\n",[217,406,407],{"class":172,"line":282},[217,408,409],{"class":330},"# [('Data', True), ('Summary', None), ('Notes', None)]\n",[10,411,412,413,416],{},"Opening such a file, Excel treats the selected sheets as a group — the title bar shows \"",[217,414,415],{},"Group","\", and typing into one sheet types into all of them. LibreOffice reads it the same way: converting this file and inspecting the result shows both Data and the active sheet marked as selected. Clear the flag on every sheet except the one in front:",[207,418,420],{"className":252,"code":419,"language":254,"meta":212,"style":212},"def activate(wb, name):\n    target = wb[name]\n    for ws in wb.worksheets:\n        ws.sheet_view.tabSelected = ws is target\n    wb.active = target\n\nwb = load_workbook(\"report.xlsx\")\nactivate(wb, \"Summary\")\nwb.save(\"summary_first.xlsx\")\nprint([(ws.title, ws.sheet_view.tabSelected) for ws in load_workbook(\"summary_first.xlsx\").worksheets])\n# [('Data', False), ('Summary', True), ('Notes', False)]\n",[214,421,422,434,444,456,471,480,484,497,507,517,536],{"__ignoreMap":212},[217,423,424,427,431],{"class":172,"line":219},[217,425,426],{"class":261},"def",[217,428,430],{"class":429},"s_Opv"," activate",[217,432,433],{"class":265},"(wb, name):\n",[217,435,436,439,441],{"class":172,"line":275},[217,437,438],{"class":265},"    target ",[217,440,288],{"class":261},[217,442,443],{"class":265}," wb[name]\n",[217,445,446,449,451,453],{"class":172,"line":282},[217,447,448],{"class":261},"    for",[217,450,394],{"class":265},[217,452,397],{"class":261},[217,454,455],{"class":265}," wb.worksheets:\n",[217,457,458,461,463,465,468],{"class":172,"line":300},[217,459,460],{"class":265},"        ws.sheet_view.tabSelected ",[217,462,288],{"class":261},[217,464,394],{"class":265},[217,466,467],{"class":261},"is",[217,469,470],{"class":265}," target\n",[217,472,473,476,478],{"class":172,"line":317},[217,474,475],{"class":265},"    wb.active ",[217,477,288],{"class":261},[217,479,470],{"class":265},[217,481,482],{"class":172,"line":327},[217,483,279],{"emptyLinePlaceholder":278},[217,485,487,489,491,493,495],{"class":172,"line":486},7,[217,488,285],{"class":265},[217,490,288],{"class":261},[217,492,291],{"class":265},[217,494,294],{"class":226},[217,496,297],{"class":265},[217,498,500,503,505],{"class":172,"line":499},8,[217,501,502],{"class":265},"activate(wb, ",[217,504,311],{"class":226},[217,506,297],{"class":265},[217,508,510,512,515],{"class":172,"line":509},9,[217,511,376],{"class":265},[217,513,514],{"class":226},"\"summary_first.xlsx\"",[217,516,297],{"class":265},[217,518,520,522,524,526,528,530,532,534],{"class":172,"line":519},10,[217,521,321],{"class":320},[217,523,388],{"class":265},[217,525,391],{"class":261},[217,527,394],{"class":265},[217,529,397],{"class":261},[217,531,291],{"class":265},[217,533,514],{"class":226},[217,535,404],{"class":265},[217,537,539],{"class":172,"line":538},11,[217,540,541],{"class":330},"# [('Data', False), ('Summary', True), ('Notes', False)]\n",[10,543,544,545,548,549,551],{},"Use a helper like ",[214,546,547],{},"activate"," everywhere instead of assigning ",[214,550,248],{}," directly; the problem only shows up with files that went through Excel at some point, which makes it easy to miss in testing with freshly generated workbooks.",[20,553,29,559,29,562,29,565,29,570,29,580,29,588,29,593,29,598,29,604,29,611,29,615,29,620,29,624,29,629,29,633,29,639,29,642,29,648,29,653,29,657,29,661,29,666,29,669,29,674,29,677,29,682,29,685],{"viewBox":554,"role":23,"ariaLabelledBy":555,"xmlns":27,"style":558},"4 0 752 233",[556,557],"act-group-t","act-group-d","width:100%;max-width:752px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif",[31,560,561],{"id":556},"Setting wb.active alone versus clearing tabSelected",[35,563,564],{"id":557},"Two panels. Setting wb.active on a file last saved on the Data tab leaves Data selected too, so the file opens with Data and Summary grouped and edits go to both. The activate helper sets tabSelected to true only on the target sheet, so the file opens with Summary alone selected.",[39,566],{"x":567,"y":42,"width":568,"height":569,"fill":45},"4","752","233",[39,571],{"x":572,"y":573,"width":574,"height":575,"rx":576,"fill":577,"stroke":578,"style":579},"20.0","16.0","351.0","201.0","14","#fee8f2","var(--accent,#f43f8f)","stroke-width:2px",[69,581,583],{"transform":582},"translate(182.5,29.0) scale(1.083)",[73,584],{"d":585,"fill":76,"stroke":586,"style":587},"M12 3.5l9.5 17h-19z M12 10v5 M12 17.8v.4","var(--accent-ink,#be185d)","stroke-width:1.66px;stroke-linecap:round;stroke-linejoin:round",[47,589,592],{"x":590,"y":87,"style":591},"195.5","font-size:14px;font-weight:700;fill:var(--accent-ink,#be185d);text-anchor:middle","wb.active = wb[\"Summary\"] only",[172,594],{"x1":595,"y1":596,"x2":597,"y2":596,"stroke":578,"style":90},"36.0","91.0","355.0",[47,599,603],{"x":600,"y":601,"style":602},"60.0","111.0","font-size:12px;font-weight:400;fill:var(--text,#172033);text-anchor:start","Data keeps tabSelected",[69,605,607],{"transform":606},"translate(38.5,99.5) scale(0.625)",[73,608],{"d":609,"fill":76,"stroke":578,"style":610},"M6 6l12 12 M18 6L6 18","stroke-width:2.88px;stroke-linecap:round;stroke-linejoin:round",[47,612,614],{"x":600,"y":613,"style":602},"137.0","two sheets selected on open",[69,616,618],{"transform":617},"translate(38.5,125.5) scale(0.625)",[73,619],{"d":609,"fill":76,"stroke":578,"style":610},[47,621,623],{"x":600,"y":622,"style":602},"163.0","\"[Group]\": typing edits both",[69,625,627],{"transform":626},"translate(38.5,151.5) scale(0.625)",[73,628],{"d":609,"fill":76,"stroke":578,"style":610},[47,630,632],{"x":600,"y":631,"style":602},"189.0","invisible with fresh files",[69,634,636],{"transform":635},"translate(38.5,177.5) scale(0.625)",[73,637],{"d":585,"fill":76,"stroke":638,"style":610},"var(--gold,#b4740a)",[39,640],{"x":641,"y":573,"width":574,"height":575,"rx":576,"fill":119,"stroke":120,"style":579},"389.0",[69,643,645],{"transform":644},"translate(551.5,29.0) scale(1.083)",[73,646],{"d":647,"fill":76,"stroke":77,"style":587},"M5 12.5l4.5 4.5L19 7",[47,649,652],{"x":650,"y":87,"style":651},"564.5","font-size:14px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","activate(wb, \"Summary\")",[172,654],{"x1":655,"y1":596,"x2":656,"y2":596,"stroke":120,"style":90},"405.0","724.0",[47,658,660],{"x":659,"y":601,"style":602},"429.0","tabSelected only on Summary",[69,662,664],{"transform":663},"translate(407.5,99.5) scale(0.625)",[73,665],{"d":647,"fill":76,"stroke":120,"style":610},[47,667,668],{"x":659,"y":613,"style":602},"one sheet selected on open",[69,670,672],{"transform":671},"translate(407.5,125.5) scale(0.625)",[73,673],{"d":647,"fill":76,"stroke":120,"style":610},[47,675,676],{"x":659,"y":622,"style":602},"safe with files from Excel",[69,678,680],{"transform":679},"translate(407.5,151.5) scale(0.625)",[73,681],{"d":647,"fill":76,"stroke":120,"style":610},[47,683,684],{"x":659,"y":631,"style":602},"one helper for every job",[69,686,688],{"transform":687},"translate(407.5,177.5) scale(0.625)",[73,689],{"d":647,"fill":76,"stroke":120,"style":610},[202,691,693],{"id":692},"step-3-choose-the-selected-cell-and-the-scroll-position","Step 3 — Choose the selected cell and the scroll position",[10,695,696,697,700,701,704],{},"Each sheet remembers its own selection and scroll position in ",[214,698,699],{},"sheet_view",". Set the active cell and its selection range, and ",[214,702,703],{},"topLeftCell"," for the first visible cell:",[207,706,708],{"className":252,"code":707,"language":254,"meta":212,"style":212},"wb = load_workbook(\"summary_first.xlsx\")\ndata = wb[\"Data\"]\nsel = data.sheet_view.selection[0]\nsel.activeCell = \"A1\"\nsel.sqref = \"A1\"\ndata.sheet_view.topLeftCell = \"A1\"\n\nsummary = wb[\"Summary\"]\nsummary[\"A1\"], summary[\"B1\"] = \"Metric\", \"Value\"\nsummary.sheet_view.selection[0].activeCell = \"B1\"\nsummary.sheet_view.selection[0].sqref = \"B1\"\nwb.save(\"summary_first.xlsx\")\nprint(load_workbook(\"summary_first.xlsx\")[\"Summary\"].sheet_view.selection[0].activeCell)\n# B1\n",[214,709,710,722,736,750,760,769,778,782,795,823,838,851,860,883],{"__ignoreMap":212},[217,711,712,714,716,718,720],{"class":172,"line":219},[217,713,285],{"class":265},[217,715,288],{"class":261},[217,717,291],{"class":265},[217,719,514],{"class":226},[217,721,297],{"class":265},[217,723,724,727,729,731,734],{"class":172,"line":275},[217,725,726],{"class":265},"data ",[217,728,288],{"class":261},[217,730,308],{"class":265},[217,732,733],{"class":226},"\"Data\"",[217,735,314],{"class":265},[217,737,738,741,743,746,748],{"class":172,"line":282},[217,739,740],{"class":265},"sel ",[217,742,288],{"class":261},[217,744,745],{"class":265}," data.sheet_view.selection[",[217,747,42],{"class":320},[217,749,314],{"class":265},[217,751,752,755,757],{"class":172,"line":300},[217,753,754],{"class":265},"sel.activeCell ",[217,756,288],{"class":261},[217,758,759],{"class":226}," \"A1\"\n",[217,761,762,765,767],{"class":172,"line":317},[217,763,764],{"class":265},"sel.sqref ",[217,766,288],{"class":261},[217,768,759],{"class":226},[217,770,771,774,776],{"class":172,"line":327},[217,772,773],{"class":265},"data.sheet_view.topLeftCell ",[217,775,288],{"class":261},[217,777,759],{"class":226},[217,779,780],{"class":172,"line":486},[217,781,279],{"emptyLinePlaceholder":278},[217,783,784,787,789,791,793],{"class":172,"line":499},[217,785,786],{"class":265},"summary ",[217,788,288],{"class":261},[217,790,308],{"class":265},[217,792,311],{"class":226},[217,794,314],{"class":265},[217,796,797,800,803,806,809,812,814,817,820],{"class":172,"line":509},[217,798,799],{"class":265},"summary[",[217,801,802],{"class":226},"\"A1\"",[217,804,805],{"class":265},"], summary[",[217,807,808],{"class":226},"\"B1\"",[217,810,811],{"class":265},"] ",[217,813,288],{"class":261},[217,815,816],{"class":226}," \"Metric\"",[217,818,819],{"class":265},", ",[217,821,822],{"class":226},"\"Value\"\n",[217,824,825,828,830,833,835],{"class":172,"line":519},[217,826,827],{"class":265},"summary.sheet_view.selection[",[217,829,42],{"class":320},[217,831,832],{"class":265},"].activeCell ",[217,834,288],{"class":261},[217,836,837],{"class":226}," \"B1\"\n",[217,839,840,842,844,847,849],{"class":172,"line":538},[217,841,827],{"class":265},[217,843,42],{"class":320},[217,845,846],{"class":265},"].sqref ",[217,848,288],{"class":261},[217,850,837],{"class":226},[217,852,854,856,858],{"class":172,"line":853},12,[217,855,376],{"class":265},[217,857,514],{"class":226},[217,859,297],{"class":265},[217,861,863,865,868,870,873,875,878,880],{"class":172,"line":862},13,[217,864,321],{"class":320},[217,866,867],{"class":265},"(load_workbook(",[217,869,514],{"class":226},[217,871,872],{"class":265},")[",[217,874,311],{"class":226},[217,876,877],{"class":265},"].sheet_view.selection[",[217,879,42],{"class":320},[217,881,882],{"class":265},"].activeCell)\n",[217,884,886],{"class":172,"line":885},14,[217,887,888],{"class":330},"# B1\n",[10,890,891,892,894,895,898,899,903],{},"Scripts that append to a sheet often leave the view far down the data, especially when the file was last saved by someone scrolled to the bottom. Resetting ",[214,893,703],{}," to A1 on every sheet is a cheap habit. If the sheet has frozen panes, the selection lives in the pane below and to the right of the freeze; the first entry of ",[214,896,897],{},"selection"," is still a reasonable place to set it, and ",[14,900,902],{"href":901},"\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"," explains how panes and selections interact.",[202,905,907],{"id":906},"step-4-colour-the-tabs","Step 4 — Colour the tabs",[10,909,910,911,914],{},"Tab colours help readers find their way in workbooks with many sheets — summary tabs in one colour, detail tabs in another, notes greyed out. Set ",[214,912,913],{},"tabColor"," on each sheet's properties with an RGB hex string:",[207,916,918],{"className":252,"code":917,"language":254,"meta":212,"style":212},"TAB_COLOURS = {\"Summary\": \"0F9488\", \"Data\": \"5B5CF0\", \"Notes\": \"9AA3B5\"}\n\nwb = load_workbook(\"summary_first.xlsx\")\nfor name, colour in TAB_COLOURS.items():\n    wb[name].sheet_properties.tabColor = colour\nwb.save(\"coloured.xlsx\")\nprint({ws.title: ws.sheet_properties.tabColor.rgb for ws in load_workbook(\"coloured.xlsx\").worksheets})\n# {'Data': '005B5CF0', 'Summary': '000F9488', 'Notes': '009AA3B5'}\n",[214,919,920,961,965,977,992,1002,1011,1031],{"__ignoreMap":212},[217,921,922,925,928,931,933,936,939,941,943,945,948,950,953,955,958],{"class":172,"line":219},[217,923,924],{"class":320},"TAB_COLOURS",[217,926,927],{"class":261}," =",[217,929,930],{"class":265}," {",[217,932,311],{"class":226},[217,934,935],{"class":265},": ",[217,937,938],{"class":226},"\"0F9488\"",[217,940,819],{"class":265},[217,942,733],{"class":226},[217,944,935],{"class":265},[217,946,947],{"class":226},"\"5B5CF0\"",[217,949,819],{"class":265},[217,951,952],{"class":226},"\"Notes\"",[217,954,935],{"class":265},[217,956,957],{"class":226},"\"9AA3B5\"",[217,959,960],{"class":265},"}\n",[217,962,963],{"class":172,"line":275},[217,964,279],{"emptyLinePlaceholder":278},[217,966,967,969,971,973,975],{"class":172,"line":282},[217,968,285],{"class":265},[217,970,288],{"class":261},[217,972,291],{"class":265},[217,974,514],{"class":226},[217,976,297],{"class":265},[217,978,979,981,984,986,989],{"class":172,"line":300},[217,980,391],{"class":261},[217,982,983],{"class":265}," name, colour ",[217,985,397],{"class":261},[217,987,988],{"class":320}," TAB_COLOURS",[217,990,991],{"class":265},".items():\n",[217,993,994,997,999],{"class":172,"line":317},[217,995,996],{"class":265},"    wb[name].sheet_properties.tabColor ",[217,998,288],{"class":261},[217,1000,1001],{"class":265}," colour\n",[217,1003,1004,1006,1009],{"class":172,"line":327},[217,1005,376],{"class":265},[217,1007,1008],{"class":226},"\"coloured.xlsx\"",[217,1010,297],{"class":265},[217,1012,1013,1015,1018,1020,1022,1024,1026,1028],{"class":172,"line":486},[217,1014,321],{"class":320},[217,1016,1017],{"class":265},"({ws.title: ws.sheet_properties.tabColor.rgb ",[217,1019,391],{"class":261},[217,1021,394],{"class":265},[217,1023,397],{"class":261},[217,1025,291],{"class":265},[217,1027,1008],{"class":226},[217,1029,1030],{"class":265},").worksheets})\n",[217,1032,1033],{"class":172,"line":499},[217,1034,1035],{"class":330},"# {'Data': '005B5CF0', 'Summary': '000F9488', 'Notes': '009AA3B5'}\n",[10,1037,1038,1039,1041,1042,1045],{},"openpyxl stores the colour with an alpha prefix, which is why ",[214,1040,938],{}," reads back as ",[214,1043,1044],{},"\"000F9488\"",". Keeping the colours in one dictionary, keyed by sheet name, makes the scheme easy to change and to apply consistently across every workbook a job produces. For sheets created per group (per region, per customer), colour by category rather than individually — three or four colours in a workbook are useful; twenty are noise.",[202,1047,1049],{"id":1048},"step-5-hidden-sheets-and-the-active-tab","Step 5 — Hidden sheets and the active tab",[10,1051,1052],{},"A hidden sheet cannot be the one in front. If a job hides the sheet that is currently active, openpyxl moves the active tab to another visible sheet when it saves, which may not be the one you intended:",[207,1054,1056],{"className":252,"code":1055,"language":254,"meta":212,"style":212},"wb = load_workbook(\"coloured.xlsx\")\nwb[\"Notes\"].sheet_state = \"hidden\"\nactivate(wb, \"Summary\")          # set the active sheet after hiding\nwb.save(\"final.xlsx\")\nfinal = load_workbook(\"final.xlsx\")\nprint(final.active.title, [(ws.title, ws.sheet_state) for ws in final.worksheets])\n",[214,1057,1058,1070,1085,1097,1106,1119],{"__ignoreMap":212},[217,1059,1060,1062,1064,1066,1068],{"class":172,"line":219},[217,1061,285],{"class":265},[217,1063,288],{"class":261},[217,1065,291],{"class":265},[217,1067,1008],{"class":226},[217,1069,297],{"class":265},[217,1071,1072,1075,1077,1080,1082],{"class":172,"line":275},[217,1073,1074],{"class":265},"wb[",[217,1076,952],{"class":226},[217,1078,1079],{"class":265},"].sheet_state ",[217,1081,288],{"class":261},[217,1083,1084],{"class":226}," \"hidden\"\n",[217,1086,1087,1089,1091,1094],{"class":172,"line":282},[217,1088,502],{"class":265},[217,1090,311],{"class":226},[217,1092,1093],{"class":265},")          ",[217,1095,1096],{"class":330},"# set the active sheet after hiding\n",[217,1098,1099,1101,1104],{"class":172,"line":300},[217,1100,376],{"class":265},[217,1102,1103],{"class":226},"\"final.xlsx\"",[217,1105,297],{"class":265},[217,1107,1108,1111,1113,1115,1117],{"class":172,"line":317},[217,1109,1110],{"class":265},"final ",[217,1112,288],{"class":261},[217,1114,291],{"class":265},[217,1116,1103],{"class":226},[217,1118,297],{"class":265},[217,1120,1121,1123,1126,1128,1130,1132],{"class":172,"line":327},[217,1122,321],{"class":320},[217,1124,1125],{"class":265},"(final.active.title, [(ws.title, ws.sheet_state) ",[217,1127,391],{"class":261},[217,1129,394],{"class":265},[217,1131,397],{"class":261},[217,1133,1134],{"class":265}," final.worksheets])\n",[10,1136,1137,1138,1141,1142,1146],{},"Hide first, then activate, then check ",[214,1139,1140],{},"wb.active.title"," before saving. ",[14,1143,1145],{"href":1144},"\u002Fformatting-and-charting-excel-reports-with-python\u002Fprotecting-and-sharing-excel-workbooks\u002Fhide-sheets-rows-and-columns-with-openpyxl\u002F","Hide Sheets, Rows and Columns with openpyxl"," covers the hidden and very-hidden states.",[20,1148,29,1153,29,1156,29,1159,29,1162,29,1167,29,1175,29,1181,29,1185,29,1191,29,1196,29,1199,29,1205,29,1209,29,1212,29,1216,29,1219,29,1222,29,1227,29,1232,29,1235,29,1237,29,1240,29,1244,29,1247,29,1251,29,1258,29,1264,29,1268,29,1271,29,1275,29,1278,29,1282,29,1289,29,1294],{"viewBox":1149,"role":23,"ariaLabelledBy":1150,"xmlns":27,"style":558},"4 0 752 160",[1151,1152],"act-order-t","act-order-d",[31,1154,1155],{"id":1151},"Finishing a workbook before it is saved",[35,1157,1158],{"id":1152},"Five finishing steps in order: reorder tabs, hide sheets that readers should not see, activate the front sheet with tabSelected cleared elsewhere, reset each sheet's selection and scroll to A1, then colour the tabs by category.",[39,1160],{"x":567,"y":42,"width":568,"height":1161,"fill":45},"160",[39,1163],{"x":572,"y":573,"width":1164,"height":1165,"rx":1166,"fill":60,"stroke":61,"style":579},"115.2","128.0","12",[69,1168,1170],{"transform":1169},"translate(65.6,46.5) scale(1.000)",[73,1171],{"d":1172,"fill":76,"stroke":1173,"style":1174},"M12 3l9 5-9 5-9-5z M3 13l9 5 9-5 M3 17l9 5 9-5","var(--text,#172033)","stroke-width:1.80px;stroke-linecap:round;stroke-linejoin:round",[47,1176,1180],{"x":1177,"y":1178,"style":1179},"77.6","93.0","font-size:13.5px;font-weight:700;fill:var(--text,#172033);text-anchor:middle","Reorder tabs",[47,1182,347],{"x":1177,"y":1183,"style":1184},"112.5","font-size:11.5px;font-weight:400;fill:var(--text,#172033);text-anchor:middle",[172,1186],{"x1":1187,"y1":1188,"x2":1189,"y2":1188,"stroke":1190,"style":579},"140.2","80.0","162.2","var(--brand,#5b5cf0)",[1192,1193],"polygon",{"points":1194,"fill":1195},"166.2,80.0 157.2,85.0 157.2,75.0","#5b5cf0",[39,1197],{"x":1198,"y":573,"width":1164,"height":1165,"rx":1166,"fill":60,"stroke":61,"style":579},"171.2",[69,1200,1202],{"transform":1201},"translate(216.8,46.5) scale(1.000)",[73,1203],{"d":1204,"fill":76,"stroke":1173,"style":1174},"M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12z M12 9a3 3 0 1 0 .01 0z",[47,1206,1208],{"x":1207,"y":1178,"style":1179},"228.8","Hide helpers",[47,1210,1211],{"x":1207,"y":1183,"style":1184},"sheet_state",[172,1213],{"x1":1214,"y1":1188,"x2":1215,"y2":1188,"stroke":1190,"style":579},"291.4","313.4",[1192,1217],{"points":1218,"fill":1195},"317.4,80.0 308.4,85.0 308.4,75.0",[39,1220],{"x":1221,"y":573,"width":1164,"height":1165,"rx":1166,"fill":119,"stroke":120,"style":579},"322.4",[69,1223,1225],{"transform":1224},"translate(368.0,29.0) scale(1.000)",[73,1226],{"d":647,"fill":76,"stroke":77,"style":1174},[47,1228,1231],{"x":49,"y":1229,"style":1230},"75.5","font-size:13.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","Activate",[47,1233,1234],{"x":49,"y":97,"style":1184},"clear",[47,1236,358],{"x":49,"y":1183,"style":1184},[47,1238,1239],{"x":49,"y":133,"style":1184},"elsewhere",[172,1241],{"x1":1242,"y1":1188,"x2":1243,"y2":1188,"stroke":1190,"style":579},"442.6","464.6",[1192,1245],{"points":1246,"fill":1195},"468.6,80.0 459.6,85.0 459.6,75.0",[39,1248],{"x":1249,"y":573,"width":1164,"height":1165,"rx":1166,"fill":1250,"stroke":1190,"style":579},"473.6","#f0f4ff",[69,1252,1254],{"transform":1253},"translate(519.2,37.8) scale(1.000)",[73,1255],{"d":1256,"fill":76,"stroke":1257,"style":1174},"M4 4h16v16H4z M4 9h16 M4 14h16 M10 4v16","var(--brand-strong,#4338ca)",[47,1259,1263],{"x":1260,"y":1261,"style":1262},"531.2","84.2","font-size:13.5px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","Reset views",[47,1265,1267],{"x":1260,"y":1266,"style":1184},"103.8","selection and",[47,1269,703],{"x":1260,"y":1270,"style":1184},"121.2",[172,1272],{"x1":1273,"y1":1188,"x2":1274,"y2":1188,"stroke":1190,"style":579},"593.8","615.8",[1192,1276],{"points":1277,"fill":1195},"619.8,80.0 610.8,85.0 610.8,75.0",[39,1279],{"x":1280,"y":573,"width":1164,"height":1165,"rx":1166,"fill":1281,"stroke":638,"style":579},"624.8","#fdefd8",[69,1283,1285],{"transform":1284},"translate(670.4,46.5) scale(1.000)",[73,1286],{"d":1287,"fill":76,"stroke":1288,"style":1174},"M12 9a3 3 0 1 0 .01 0z M12 2.5v3 M12 18.5v3 M2.5 12h3 M18.5 12h3 M5.3 5.3l2.1 2.1 M16.6 16.6l2.1 2.1 M5.3 18.7l2.1-2.1 M16.6 7.4l2.1-2.1","var(--gold-ink,#7a4e06)",[47,1290,1293],{"x":1291,"y":1178,"style":1292},"682.4","font-size:13.5px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:middle","Colour tabs",[47,1295,1296],{"x":1291,"y":1183,"style":1184},"by category",[202,1298,1300],{"id":1299},"check-the-opening-state-in-a-test","Check the opening state in a test",[10,1302,1303,1304,1306],{},"Because none of these settings shows up in the data, they regress silently — a refactor drops the ",[214,1305,547],{}," call and nobody notices until a reader complains. A short check, run in the job's tests or just before the file is sent, keeps them honest:",[207,1308,1310],{"className":252,"code":1309,"language":254,"meta":212,"style":212},"def check_opening_state(path, front=\"Summary\"):\n    wb = load_workbook(path)\n    problems = []\n    if wb.active.title != front:\n        problems.append(f\"opens on {wb.active.title!r}, expected {front!r}\")\n    selected = [ws.title for ws in wb.worksheets if ws.sheet_view.tabSelected]\n    if selected != [front]:\n        problems.append(f\"selected tabs {selected} — sheets would open grouped\")\n    for ws in wb.worksheets:\n        if ws.sheet_state == \"visible\" and (ws.sheet_view.topLeftCell or \"A1\") != \"A1\":\n            problems.append(f\"{ws.title} opens scrolled to {ws.sheet_view.topLeftCell}\")\n    return problems\n\nprint(check_opening_state(\"grouped.xlsx\"))\nprint(check_opening_state(\"final.xlsx\"))\n# [] — opens on Summary, one tab selected, every visible sheet at A1\n",[214,1311,1312,1329,1339,1349,1363,1403,1428,1440,1461,1471,1507,1537,1545,1549,1561,1572],{"__ignoreMap":212},[217,1313,1314,1316,1319,1322,1324,1326],{"class":172,"line":219},[217,1315,426],{"class":261},[217,1317,1318],{"class":429}," check_opening_state",[217,1320,1321],{"class":265},"(path, front",[217,1323,288],{"class":261},[217,1325,311],{"class":226},[217,1327,1328],{"class":265},"):\n",[217,1330,1331,1334,1336],{"class":172,"line":275},[217,1332,1333],{"class":265},"    wb ",[217,1335,288],{"class":261},[217,1337,1338],{"class":265}," load_workbook(path)\n",[217,1340,1341,1344,1346],{"class":172,"line":282},[217,1342,1343],{"class":265},"    problems ",[217,1345,288],{"class":261},[217,1347,1348],{"class":265}," []\n",[217,1350,1351,1354,1357,1360],{"class":172,"line":300},[217,1352,1353],{"class":261},"    if",[217,1355,1356],{"class":265}," wb.active.title ",[217,1358,1359],{"class":261},"!=",[217,1361,1362],{"class":265}," front:\n",[217,1364,1365,1368,1371,1374,1378,1380,1383,1386,1389,1391,1394,1396,1398,1401],{"class":172,"line":317},[217,1366,1367],{"class":265},"        problems.append(",[217,1369,1370],{"class":261},"f",[217,1372,1373],{"class":226},"\"opens on ",[217,1375,1377],{"class":1376},"sSjpA","{",[217,1379,1140],{"class":265},[217,1381,1382],{"class":261},"!r",[217,1384,1385],{"class":1376},"}",[217,1387,1388],{"class":226},", expected ",[217,1390,1377],{"class":1376},[217,1392,1393],{"class":265},"front",[217,1395,1382],{"class":261},[217,1397,1385],{"class":1376},[217,1399,1400],{"class":226},"\"",[217,1402,297],{"class":265},[217,1404,1405,1408,1410,1413,1415,1417,1419,1422,1425],{"class":172,"line":327},[217,1406,1407],{"class":265},"    selected ",[217,1409,288],{"class":261},[217,1411,1412],{"class":265}," [ws.title ",[217,1414,391],{"class":261},[217,1416,394],{"class":265},[217,1418,397],{"class":261},[217,1420,1421],{"class":265}," wb.worksheets ",[217,1423,1424],{"class":261},"if",[217,1426,1427],{"class":265}," ws.sheet_view.tabSelected]\n",[217,1429,1430,1432,1435,1437],{"class":172,"line":486},[217,1431,1353],{"class":261},[217,1433,1434],{"class":265}," selected ",[217,1436,1359],{"class":261},[217,1438,1439],{"class":265}," [front]:\n",[217,1441,1442,1444,1446,1449,1451,1454,1456,1459],{"class":172,"line":499},[217,1443,1367],{"class":265},[217,1445,1370],{"class":261},[217,1447,1448],{"class":226},"\"selected tabs ",[217,1450,1377],{"class":1376},[217,1452,1453],{"class":265},"selected",[217,1455,1385],{"class":1376},[217,1457,1458],{"class":226}," — sheets would open grouped\"",[217,1460,297],{"class":265},[217,1462,1463,1465,1467,1469],{"class":172,"line":509},[217,1464,448],{"class":261},[217,1466,394],{"class":265},[217,1468,397],{"class":261},[217,1470,455],{"class":265},[217,1472,1473,1476,1479,1482,1485,1488,1491,1494,1497,1500,1502,1504],{"class":172,"line":519},[217,1474,1475],{"class":261},"        if",[217,1477,1478],{"class":265}," ws.sheet_state ",[217,1480,1481],{"class":261},"==",[217,1483,1484],{"class":226}," \"visible\"",[217,1486,1487],{"class":261}," and",[217,1489,1490],{"class":265}," (ws.sheet_view.topLeftCell ",[217,1492,1493],{"class":261},"or",[217,1495,1496],{"class":226}," \"A1\"",[217,1498,1499],{"class":265},") ",[217,1501,1359],{"class":261},[217,1503,1496],{"class":226},[217,1505,1506],{"class":265},":\n",[217,1508,1509,1512,1514,1516,1518,1521,1523,1526,1528,1531,1533,1535],{"class":172,"line":538},[217,1510,1511],{"class":265},"            problems.append(",[217,1513,1370],{"class":261},[217,1515,1400],{"class":226},[217,1517,1377],{"class":1376},[217,1519,1520],{"class":265},"ws.title",[217,1522,1385],{"class":1376},[217,1524,1525],{"class":226}," opens scrolled to ",[217,1527,1377],{"class":1376},[217,1529,1530],{"class":265},"ws.sheet_view.topLeftCell",[217,1532,1385],{"class":1376},[217,1534,1400],{"class":226},[217,1536,297],{"class":265},[217,1538,1539,1542],{"class":172,"line":853},[217,1540,1541],{"class":261},"    return",[217,1543,1544],{"class":265}," problems\n",[217,1546,1547],{"class":172,"line":862},[217,1548,279],{"emptyLinePlaceholder":278},[217,1550,1551,1553,1556,1558],{"class":172,"line":885},[217,1552,321],{"class":320},[217,1554,1555],{"class":265},"(check_opening_state(",[217,1557,379],{"class":226},[217,1559,1560],{"class":265},"))\n",[217,1562,1564,1566,1568,1570],{"class":172,"line":1563},15,[217,1565,321],{"class":320},[217,1567,1555],{"class":265},[217,1569,1103],{"class":226},[217,1571,1560],{"class":265},[217,1573,1575],{"class":172,"line":1574},16,[217,1576,1577],{"class":330},"# [] — opens on Summary, one tab selected, every visible sheet at A1\n",[10,1579,1580,1581,1585],{},"The first file opens on the right sheet but fails the grouping check: Data is still marked as selected, exactly the state Step 2 warned about, and nothing in the data would have revealed it. The finished file passes all three checks. ",[14,1582,1584],{"href":1583},"\u002Fautomating-reporting-workflows\u002Ftesting-and-packaging-excel-automation-scripts\u002Ftest-excel-output-with-pytest\u002F","Test Excel Output with pytest"," shows how to wire checks like this into a test suite.",[202,1587,1589],{"id":1588},"the-same-settings-in-xlsxwriter","The same settings in xlsxwriter",[10,1591,1592,1593,1596],{},"When the workbook is created with xlsxwriter — directly or as pandas' engine — the equivalents are worksheet methods. ",[214,1594,1595],{},"activate()"," sets the active sheet and handles the selection for you, so the grouping trap does not arise:",[207,1598,1600],{"className":252,"code":1599,"language":254,"meta":212,"style":212},"import xlsxwriter\n\nbook = xlsxwriter.Workbook(\"xw_front.xlsx\")\ndata = book.add_worksheet(\"Data\")\nsummary = book.add_worksheet(\"Summary\")\ndata.set_tab_color(\"#5B5CF0\")\nsummary.set_tab_color(\"#0F9488\")\nsummary.activate()\nsummary.set_selection(\"B2\")\nbook.close()\n\nwb = load_workbook(\"xw_front.xlsx\")\nprint(wb.active.title, [(ws.title, ws.sheet_view.tabSelected) for ws in wb.worksheets])\n",[214,1601,1602,1608,1612,1627,1640,1652,1662,1672,1677,1687,1692,1696,1708],{"__ignoreMap":212},[217,1603,1604,1606],{"class":172,"line":219},[217,1605,269],{"class":261},[217,1607,233],{"class":265},[217,1609,1610],{"class":172,"line":275},[217,1611,279],{"emptyLinePlaceholder":278},[217,1613,1614,1617,1619,1622,1625],{"class":172,"line":282},[217,1615,1616],{"class":265},"book ",[217,1618,288],{"class":261},[217,1620,1621],{"class":265}," xlsxwriter.Workbook(",[217,1623,1624],{"class":226},"\"xw_front.xlsx\"",[217,1626,297],{"class":265},[217,1628,1629,1631,1633,1636,1638],{"class":172,"line":300},[217,1630,726],{"class":265},[217,1632,288],{"class":261},[217,1634,1635],{"class":265}," book.add_worksheet(",[217,1637,733],{"class":226},[217,1639,297],{"class":265},[217,1641,1642,1644,1646,1648,1650],{"class":172,"line":317},[217,1643,786],{"class":265},[217,1645,288],{"class":261},[217,1647,1635],{"class":265},[217,1649,311],{"class":226},[217,1651,297],{"class":265},[217,1653,1654,1657,1660],{"class":172,"line":327},[217,1655,1656],{"class":265},"data.set_tab_color(",[217,1658,1659],{"class":226},"\"#5B5CF0\"",[217,1661,297],{"class":265},[217,1663,1664,1667,1670],{"class":172,"line":486},[217,1665,1666],{"class":265},"summary.set_tab_color(",[217,1668,1669],{"class":226},"\"#0F9488\"",[217,1671,297],{"class":265},[217,1673,1674],{"class":172,"line":499},[217,1675,1676],{"class":265},"summary.activate()\n",[217,1678,1679,1682,1685],{"class":172,"line":509},[217,1680,1681],{"class":265},"summary.set_selection(",[217,1683,1684],{"class":226},"\"B2\"",[217,1686,297],{"class":265},[217,1688,1689],{"class":172,"line":519},[217,1690,1691],{"class":265},"book.close()\n",[217,1693,1694],{"class":172,"line":538},[217,1695,279],{"emptyLinePlaceholder":278},[217,1697,1698,1700,1702,1704,1706],{"class":172,"line":853},[217,1699,285],{"class":265},[217,1701,288],{"class":261},[217,1703,291],{"class":265},[217,1705,1624],{"class":226},[217,1707,297],{"class":265},[217,1709,1710,1712,1715,1717,1719,1721],{"class":172,"line":862},[217,1711,321],{"class":320},[217,1713,1714],{"class":265},"(wb.active.title, [(ws.title, ws.sheet_view.tabSelected) ",[217,1716,391],{"class":261},[217,1718,394],{"class":265},[217,1720,397],{"class":261},[217,1722,1723],{"class":265}," wb.worksheets])\n",[10,1725,1726,1727,1730,1731,1734,1735,1738],{},"With pandas, call the same methods on ",[214,1728,1729],{},"writer.sheets[\"Summary\"]"," inside the ",[214,1732,1733],{},"with pd.ExcelWriter(..., engine=\"xlsxwriter\")"," block, after ",[214,1736,1737],{},"to_excel"," has created the sheet.",[202,1740,1742],{"id":1741},"common-pitfalls","Common pitfalls",[1744,1745,1746,1759,1769,1775],"ul",{},[1747,1748,1749,1758],"li",{},[1750,1751,1752,1753,1755,1756,348],"strong",{},"Assigning ",[214,1754,248],{}," without clearing ",[214,1757,358],{}," Files from Excel open grouped.",[1747,1760,1761,1764,1765,1768],{},[1750,1762,1763],{},"Using indexes after reordering."," ",[214,1766,1767],{},"wb.active = 1"," points at whatever is second now; pass the sheet object.",[1747,1770,1771,1774],{},[1750,1772,1773],{},"Hiding the active sheet last."," The active tab moves; activate after hiding.",[1747,1776,1777,1780,1781,1783],{},[1750,1778,1779],{},"Leaving the view scrolled down."," Reset ",[214,1782,703],{}," on sheets your script appended to.",[202,1785,1787],{"id":1786},"performance-notes","Performance notes",[10,1789,1790],{},"These are workbook-level settings stored in a few XML attributes; setting them costs nothing measurable, and looping over every sheet to reset views is instant even for workbooks with hundreds of sheets. The opening-state check loads the whole workbook, so for very large files run it in tests against a sample output rather than on every production run.",[202,1792,1794],{"id":1793},"conclusion","Conclusion",[10,1796,1797,1798,1800,1801,1803,1804,1806,1807,1809,1810,1813,1814,819,1816,1819,1820,1823],{},"Put the right sheet in front with an ",[214,1799,547],{}," helper that sets ",[214,1802,248],{}," and clears ",[214,1805,358],{}," on every other sheet, set each sheet's selection and ",[214,1808,703],{}," so it opens at the top, colour tabs through ",[214,1811,1812],{},"sheet_properties.tabColor",", and activate after hiding sheets. With xlsxwriter, ",[214,1815,1595],{},[214,1817,1818],{},"set_selection()"," and ",[214,1821,1822],{},"set_tab_color()"," do the same jobs.",[202,1825,1827],{"id":1826},"frequently-asked-questions","Frequently asked questions",[10,1829,1830,1836],{},[1750,1831,1832,1833,1835],{},"Why does Excel show ",[217,1834,415],{}," in the title bar after I set wb.active?","\nThe sheet that was selected when the file was last saved still has tabSelected set, and openpyxl does not clear it when you change wb.active. Two sheets are then selected together. Set tabSelected to False on every sheet except the active one.",[10,1838,1839,1842],{},[1750,1840,1841],{},"How do I make a workbook open with a particular cell selected?","\nSet activeCell and sqref on the first entry of the sheet's sheet_view.selection to the cell address, and make that sheet active. Setting topLeftCell as well scrolls the view so the cell is visible.",[10,1844,1845,1848],{},[1750,1846,1847],{},"What colour format does tabColor take?","\nAn RGB hex string such as \"1072BA\" (or with an alpha prefix, \"FF1072BA\"), or an openpyxl Color object. It is set on ws.sheet_properties.tabColor.",[10,1850,1851,1854],{},[1750,1852,1853],{},"Can the active sheet be hidden?","\nNo. A hidden sheet cannot be the one in front. When the active sheet is hidden, openpyxl moves the active tab to another visible sheet when saving, so set the active sheet after hiding sheets and check it.",[202,1856,1858],{"id":1857},"related","Related",[1744,1860,1861,1867,1874,1881],{},[1747,1862,1863,1864,1866],{},"Up one level: ",[14,1865,17],{"href":16}," — sheet- and workbook-level settings.",[1747,1868,1869,1873],{},[14,1870,1872],{"href":1871},"\u002Fautomating-reporting-workflows\u002Fbuilding-multi-sheet-excel-dashboards\u002F","Building Multi-Sheet Excel Dashboards"," — structuring a workbook with a front page.",[1747,1875,1876,1880],{},[14,1877,1879],{"href":1878},"\u002Fautomating-reporting-workflows\u002Fbuilding-multi-sheet-excel-dashboards\u002Fadd-a-table-of-contents-sheet-with-hyperlinks-in-excel\u002F","Add a Table of Contents Sheet with Hyperlinks in Excel"," — navigation between the tabs.",[1747,1882,1883,1887],{},[14,1884,1886],{"href":1885},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-workbook-properties-with-python\u002F","Set Workbook Properties Like Author and Title with Python"," — the other finishing touch.",[1889,1890,1891],"style",{},"html pre.shiki code .sMTad, html code.shiki .sMTad{--shiki-default:#6F42C1;--shiki-dark:#FFB757}html pre.shiki code .srMev, html code.shiki .srMev{--shiki-default:#032F62;--shiki-dark:#ADDCFF}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 .s-kum, html code.shiki .s-kum{--shiki-default:#D73A49;--shiki-dark:#FF9492}html pre.shiki code .skGVy, html code.shiki .skGVy{--shiki-default:#24292E;--shiki-dark:#F0F3F6}html pre.shiki code .sP0c6, html code.shiki .sP0c6{--shiki-default:#005CC5;--shiki-dark:#91CBFF}html pre.shiki code .s-wDw, html code.shiki .s-wDw{--shiki-default:#6A737D;--shiki-dark:#BDC4CC}html pre.shiki code .s_Opv, html code.shiki .s_Opv{--shiki-default:#6F42C1;--shiki-dark:#DBB7FF}html pre.shiki code .sSjpA, html code.shiki .sSjpA{--shiki-default:#005CC5;--shiki-dark:#FF9492}",{"title":212,"searchDepth":275,"depth":275,"links":1893},[1894,1895,1896,1897,1898,1899,1900,1901,1902,1903,1904,1905,1906],{"id":204,"depth":275,"text":205},{"id":242,"depth":275,"text":243},{"id":351,"depth":275,"text":352},{"id":692,"depth":275,"text":693},{"id":906,"depth":275,"text":907},{"id":1048,"depth":275,"text":1049},{"id":1299,"depth":275,"text":1300},{"id":1588,"depth":275,"text":1589},{"id":1741,"depth":275,"text":1742},{"id":1786,"depth":275,"text":1787},{"id":1793,"depth":275,"text":1794},{"id":1826,"depth":275,"text":1827},{"id":1857,"depth":275,"text":1858},"2026-09-25","Decide what a reader sees when a generated workbook opens: which sheet is in front, which cell is selected, where the view is scrolled to, and which tabs are coloured. Includes the trap that leaves two sheets grouped after wb.active changes, and the xlsxwriter equivalents.","md",[1911,1914,1916,1918],{"q":1912,"a":1913},"Why does Excel show [Group] in the title bar after I set wb.active?","The sheet that was selected when the file was last saved still has tabSelected set, and openpyxl does not clear it when you change wb.active. Two sheets are then selected together. Set tabSelected to False on every sheet except the active one.",{"q":1841,"a":1915},"Set activeCell and sqref on the first entry of the sheet's sheet_view.selection to the cell address, and make that sheet active. Setting topLeftCell as well scrolls the view so the cell is visible.",{"q":1847,"a":1917},"An RGB hex string such as \"1072BA\" (or with an alpha prefix, \"FF1072BA\"), or an openpyxl Color object. It is set on ws.sheet_properties.tabColor.",{"q":1853,"a":1919},"No. A hidden sheet cannot be the one in front. When the active sheet is hidden, openpyxl moves the active tab to another visible sheet when saving, so set the active sheet after hiding sheets and check it.","set the active sheet and sheet tab colour with openpyxl",null,{"breadcrumb":1923},[1924,1927,1930],{"name":1925,"item":1926},"Home","\u002F",{"name":1928,"item":1929},"Getting Started with Python Excel Automation","\u002Fgetting-started-with-python-excel-automation\u002F",{"name":17,"item":16},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl",{"title":1933,"description":1934},"openpyxl: Set Active Sheet, Selected Cell and Tab Color","Make an Excel workbook open on the right sheet and cell with openpyxl: wb.active, tabSelected and grouped sheets, sheet_view selection and topLeftCell, tabColor, and xlsxwriter activate and set_tab_color.","set-the-active-sheet-and-tab-colour-with-openpyxl","getting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-the-active-sheet-and-tab-colour-with-openpyxl\u002Findex",[1938,1939,1940],"report-layouts","openpyxl","sheets-and-files","how-to","OGEdCEr6aSUTQTKJk_G6KsZd6gIDgnrTPEHEYR1YEbU",[1944,1948],{"title":1945,"path":1946,"stem":1947,"children":-1},"Read Excel Rows into Dictionaries with openpyxl","\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fread-excel-rows-into-dictionaries-with-openpyxl","getting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fread-excel-rows-into-dictionaries-with-openpyxl\u002Findex",{"title":1886,"path":1949,"stem":1950,"children":-1},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-workbook-properties-with-python","getting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fset-workbook-properties-with-python\u002Findex",[1952,1954],{"path":1953,"title":1928},"\u002Fgetting-started-with-python-excel-automation",{"path":1955,"title":17},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation",[],[1958,1962,1965,1968,1971,1974],{"path":1959,"title":1960,"kind":1961},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports","Building Finance and Operations Reports in Excel with Python","guide",{"path":1963,"title":1964,"kind":1941},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports\u002Fallocate-shared-costs-across-departments","Allocate Shared Costs Across Departments with Python",{"path":1966,"title":1967,"kind":1941},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports\u002Fbuild-a-bank-reconciliation-report","Build a Bank Reconciliation Report with Python and Excel",{"path":1969,"title":1970,"kind":1941},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports\u002Fbuild-a-bill-of-materials-cost-rollup","Build a Bill of Materials Cost Rollup",{"path":1972,"title":1973,"kind":1941},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports\u002Fbuild-a-break-even-analysis-in-excel-with-python","Build a Break-Even Analysis in Excel with Python",{"path":1975,"title":1976,"kind":1977},"\u002Fautomating-reporting-workflows\u002Fbuilding-finance-and-operations-reports\u002Fbuild-a-budget-vs-actual-variance-report","Build a Budget vs Actual Variance Report in Excel with Python","compare",1790972070079]