[{"data":1,"prerenderedAt":2444},["ShallowReactive",2],{"doc:\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook":3,"surround:\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook":2409,"ancestors:\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook":2418,"children:\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook":2423,"related:\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook":2424},{"id":4,"title":5,"body":6,"dateModified":2375,"datePublished":2375,"description":2376,"extension":2377,"faq":2378,"keyword":2387,"kind":2388,"meta":2389,"navigation":143,"path":2397,"seo":2398,"slug":2401,"stem":2402,"topics":2403,"type":2407,"__hash__":2408},"docs\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook\u002Findex.md","Fix XlsxWriter Overwriting an Existing Workbook",{"type":7,"value":8,"toc":2361},"minimark",[9,44,49,80,90,94,302,316,547,551,554,624,633,637,918,959,1139,1147,1151,1158,1332,1335,1475,1479,1482,1806,1817,1821,1828,2194,2203,2207,2210,2214,2279,2283,2296,2300,2306,2312,2318,2324,2328,2357],[10,11,12,13,17,18,21,22,24,25,28,29,33,34,37,38,43],"p",{},"A workbook has a carefully maintained ",[14,15,16],"code",{},"Inputs"," sheet. A script is meant to add a ",[14,19,20],{},"Report"," sheet next to it. It runs without an error — and the file now contains only ",[14,23,20],{},". The same thing happens with ",[14,26,27],{},"df.to_excel(\"book.xlsx\", sheet_name=\"Report\")",", because pandas uses XlsxWriter by default when it is installed. Nothing went wrong in the library: XlsxWriter is a ",[30,31,32],"em",{},"writer",". It builds a new file from scratch and, when you call ",[14,35,36],{},"close()",", replaces whatever was at that path. It cannot read an existing workbook, so it cannot add to one. This page shows how to recognise the problem, the right tool for each kind of edit, and a guard that stops it happening by accident. It belongs to ",[39,40,42],"a",{"href":41},"\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002F","Troubleshooting Common Python Excel Errors",".",[45,46,48],"h2",{"id":47},"prerequisites","Prerequisites",[50,51,56],"pre",{"className":52,"code":53,"language":54,"meta":55,"style":55},"language-bash shiki shiki-themes github-light github-dark-high-contrast","pip install pandas openpyxl xlsxwriter\n","bash","",[14,57,58],{"__ignoreMap":55},[59,60,63,67,71,74,77],"span",{"class":61,"line":62},"line",1,[59,64,66],{"class":65},"sMTad","pip",[59,68,70],{"class":69},"srMev"," install",[59,72,73],{"class":69}," pandas",[59,75,76],{"class":69}," openpyxl",[59,78,79],{"class":69}," xlsxwriter\n",[10,81,82,83,85,86,89],{},"Each sample file starts with two sheets, ",[14,84,16],{}," and ",[14,87,88],{},"Notes",", standing in for a workbook someone maintains by hand.",[45,91,93],{"id":92},"step-1-see-the-overwrite-happen","Step 1 — See the overwrite happen",[50,95,99],{"className":96,"code":97,"language":98,"meta":55,"style":55},"language-python shiki shiki-themes github-light github-dark-high-contrast","import xlsxwriter\nimport pandas as pd\nfrom openpyxl import load_workbook\n\nbook = xlsxwriter.Workbook(\"model.xlsx\")\nbook.add_worksheet(\"Report\").write(\"A1\", \"Revenue forecast\")\nbook.close()\nprint(\"XlsxWriter:\", load_workbook(\"model.xlsx\").sheetnames)\n\npd.DataFrame({\"Month\": [\"Oct\", \"Nov\"], \"Revenue\": [120, 125]}).to_excel(\n    \"model_pandas.xlsx\", sheet_name=\"Report\", index=False)\nprint(\"to_excel:  \", load_workbook(\"model_pandas.xlsx\").sheetnames)\n","python",[14,100,101,110,124,138,145,163,186,192,213,218,257,285],{"__ignoreMap":55},[59,102,103,107],{"class":61,"line":62},[59,104,106],{"class":105},"s-kum","import",[59,108,79],{"class":109},"skGVy",[59,111,113,115,118,121],{"class":61,"line":112},2,[59,114,106],{"class":105},[59,116,117],{"class":109}," pandas ",[59,119,120],{"class":105},"as",[59,122,123],{"class":109}," pd\n",[59,125,127,130,133,135],{"class":61,"line":126},3,[59,128,129],{"class":105},"from",[59,131,132],{"class":109}," openpyxl ",[59,134,106],{"class":105},[59,136,137],{"class":109}," load_workbook\n",[59,139,141],{"class":61,"line":140},4,[59,142,144],{"emptyLinePlaceholder":143},true,"\n",[59,146,148,151,154,157,160],{"class":61,"line":147},5,[59,149,150],{"class":109},"book ",[59,152,153],{"class":105},"=",[59,155,156],{"class":109}," xlsxwriter.Workbook(",[59,158,159],{"class":69},"\"model.xlsx\"",[59,161,162],{"class":109},")\n",[59,164,166,169,172,175,178,181,184],{"class":61,"line":165},6,[59,167,168],{"class":109},"book.add_worksheet(",[59,170,171],{"class":69},"\"Report\"",[59,173,174],{"class":109},").write(",[59,176,177],{"class":69},"\"A1\"",[59,179,180],{"class":109},", ",[59,182,183],{"class":69},"\"Revenue forecast\"",[59,185,162],{"class":109},[59,187,189],{"class":61,"line":188},7,[59,190,191],{"class":109},"book.close()\n",[59,193,195,199,202,205,208,210],{"class":61,"line":194},8,[59,196,198],{"class":197},"sP0c6","print",[59,200,201],{"class":109},"(",[59,203,204],{"class":69},"\"XlsxWriter:\"",[59,206,207],{"class":109},", load_workbook(",[59,209,159],{"class":69},[59,211,212],{"class":109},").sheetnames)\n",[59,214,216],{"class":61,"line":215},9,[59,217,144],{"emptyLinePlaceholder":143},[59,219,221,224,227,230,233,235,238,241,244,246,249,251,254],{"class":61,"line":220},10,[59,222,223],{"class":109},"pd.DataFrame({",[59,225,226],{"class":69},"\"Month\"",[59,228,229],{"class":109},": [",[59,231,232],{"class":69},"\"Oct\"",[59,234,180],{"class":109},[59,236,237],{"class":69},"\"Nov\"",[59,239,240],{"class":109},"], ",[59,242,243],{"class":69},"\"Revenue\"",[59,245,229],{"class":109},[59,247,248],{"class":197},"120",[59,250,180],{"class":109},[59,252,253],{"class":197},"125",[59,255,256],{"class":109},"]}).to_excel(\n",[59,258,260,263,265,269,271,273,275,278,280,283],{"class":61,"line":259},11,[59,261,262],{"class":69},"    \"model_pandas.xlsx\"",[59,264,180],{"class":109},[59,266,268],{"class":267},"sa561","sheet_name",[59,270,153],{"class":105},[59,272,171],{"class":69},[59,274,180],{"class":109},[59,276,277],{"class":267},"index",[59,279,153],{"class":105},[59,281,282],{"class":197},"False",[59,284,162],{"class":109},[59,286,288,290,292,295,297,300],{"class":61,"line":287},12,[59,289,198],{"class":197},[59,291,201],{"class":109},[59,293,294],{"class":69},"\"to_excel:  \"",[59,296,207],{"class":109},[59,298,299],{"class":69},"\"model_pandas.xlsx\"",[59,301,212],{"class":109},[10,303,304,305,307,308,311,312,315],{},"Both files now contain only ",[14,306,20],{},". XlsxWriter never opened ",[14,309,310],{},"model.xlsx","; it created a new workbook in memory and wrote it to that name. ",[14,313,314],{},"to_excel"," with a path does the same, whichever engine it uses — writing to a path means \"create this file\". There was no warning, because overwriting a file is a normal thing for a writer to do.",[317,318,326,327,326,331,326,335,326,343,326,352,326,356,326,363,326,367,326,371,326,375,326,380,326,382,326,386,326,388,326,391,326,394,326,398,326,400,326,404,326,406,326,409,326,412,326,416,326,418,326,421,326,423,326,426,326,431,326,438,326,443,326,447,326,451,326,454,326,458,326,461,326,465,326,468,326,470,326,473,326,478,326,482,326,484,326,486,326,488,326,490,326,492,326,494,326,496,326,498,326,500,326,504,326,508,326,511,326,517,326,521,326,528,326,533,326,541],"svg",{"viewBox":319,"role":320,"ariaLabelledBy":321,"xmlns":324,"style":325},"26 0 707 188","img",[322,323],"xw-overwrite-t","xw-overwrite-d","http:\u002F\u002Fwww.w3.org\u002F2000\u002Fsvg","width:100%;max-width:707px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif","\n  ",[328,329,330],"title",{"id":322},"Writing a Report sheet to an existing file replaces it",[332,333,334],"desc",{"id":323},"Before, model.xlsx has the Inputs and Notes sheets. After XlsxWriter writes a workbook with a Report sheet to the same path, the file contains only Report.",[336,337],"rect",{"x":338,"y":339,"width":340,"height":341,"fill":342},"26","0","707","188","#ffffff",[336,344],{"x":345,"y":346,"width":347,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"42.8","40.0","32.0","24.0","#e7ebef","var(--line,#cdd5e6)","stroke-width:1px",[336,353],{"x":354,"y":346,"width":355,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"74.8","93.9",[357,358,362],"text",{"x":359,"y":360,"style":361},"121.8","57.0","font-size:11px;font-weight:600;fill:var(--muted,#5b6780);text-anchor:middle","A",[336,364],{"x":365,"y":346,"width":366,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"168.7","56.5",[357,368,370],{"x":369,"y":360,"style":361},"197.0","B",[336,372],{"x":345,"y":373,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},"64.0","28.0",[357,376,379],{"x":377,"y":378,"style":361},"58.8","83.0","1",[336,381],{"x":354,"y":373,"width":355,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,383,385],{"x":359,"y":378,"style":384},"font-size:11px;font-weight:700;fill:var(--text,#172033);text-anchor:middle","Assumption",[336,387],{"x":365,"y":373,"width":366,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,389,390],{"x":369,"y":378,"style":384},"Value",[336,392],{"x":345,"y":393,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},"92.0",[357,395,397],{"x":377,"y":396,"style":361},"111.0","2",[336,399],{"x":354,"y":393,"width":355,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,401,403],{"x":359,"y":396,"style":402},"font-size:11px;font-weight:400;fill:var(--text,#172033);text-anchor:middle","Growth",[336,405],{"x":365,"y":393,"width":366,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,407,408],{"x":369,"y":396,"style":402},"0.04",[336,410],{"x":345,"y":411,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},"120.0",[357,413,415],{"x":377,"y":414,"style":361},"139.0","3",[336,417],{"x":354,"y":411,"width":355,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,419,420],{"x":359,"y":414,"style":402},"FX EUR\u002FUSD",[336,422],{"x":365,"y":411,"width":366,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[357,424,425],{"x":369,"y":414,"style":402},"1.09",[336,427],{"x":428,"y":429,"width":430,"height":348,"rx":339,"fill":342,"stroke":350,"style":351},"50.8","148.0","61.2",[61,432],{"x1":433,"y1":434,"x2":435,"y2":434,"stroke":436,"style":437},"52.8","171.0","110.0","var(--teal,#0f9488)","stroke-width:3px",[357,439,16],{"x":440,"y":441,"style":442},"81.4","164.5","font-size:11.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle",[336,444],{"x":445,"y":429,"width":446,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"114.0","57.7",[357,448,88],{"x":449,"y":441,"style":450},"142.9","font-size:11.5px;font-weight:400;fill:var(--muted,#5b6780);text-anchor:middle",[336,452],{"x":453,"y":346,"width":347,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"517.4",[336,455],{"x":456,"y":346,"width":457,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"549.4","123.8",[357,459,362],{"x":460,"y":360,"style":361},"611.3",[336,462],{"x":463,"y":346,"width":464,"height":348,"rx":339,"fill":349,"stroke":350,"style":351},"673.2","44.0",[357,466,370],{"x":467,"y":360,"style":361},"695.2",[336,469],{"x":453,"y":373,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},[357,471,379],{"x":472,"y":378,"style":361},"533.4",[336,474],{"x":456,"y":373,"width":457,"height":374,"rx":339,"fill":475,"stroke":476,"style":477},"#fce9e9","var(--danger,#dc2626)","stroke-width:1.5px",[357,479,481],{"x":460,"y":378,"style":480},"font-size:11px;font-weight:700;fill:var(--accent-ink,#be185d);text-anchor:middle","Revenue forecast",[336,483],{"x":463,"y":373,"width":464,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[336,485],{"x":453,"y":393,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},[357,487,397],{"x":472,"y":396,"style":361},[336,489],{"x":456,"y":393,"width":457,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[336,491],{"x":463,"y":393,"width":464,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[336,493],{"x":453,"y":411,"width":347,"height":374,"rx":339,"fill":349,"stroke":350,"style":351},[357,495,415],{"x":472,"y":414,"style":361},[336,497],{"x":456,"y":411,"width":457,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[336,499],{"x":463,"y":411,"width":464,"height":374,"rx":339,"fill":342,"stroke":350,"style":351},[336,501],{"x":502,"y":429,"width":503,"height":348,"rx":339,"fill":342,"stroke":350,"style":351},"525.4","63.5",[61,505],{"x1":506,"y1":434,"x2":507,"y2":434,"stroke":436,"style":437},"527.4","586.9",[357,509,20],{"x":510,"y":441,"style":442},"557.2",[357,512,516],{"x":513,"y":514,"style":515},"134.0","29.0","font-size:12.5px;font-weight:700;fill:var(--muted,#5b6780);text-anchor:middle","model.xlsx before",[357,518,520],{"x":519,"y":514,"style":515},"617.3","model.xlsx after",[61,522],{"x1":523,"y1":524,"x2":525,"y2":524,"stroke":526,"style":527},"235.3","106.0","503.4","var(--brand,#5b5cf0)","stroke-width:2px",[529,530],"polygon",{"points":531,"fill":532},"507.4,106.0 498.4,111.0 498.4,101.0","#5b5cf0",[336,534],{"x":535,"y":536,"width":537,"height":538,"rx":539,"fill":342,"stroke":350,"style":540},"270.5","73.0","201.7","22.0","11","stroke-width:1.2px",[357,542,546],{"x":543,"y":544,"style":545},"371.4","88.0","font-size:11.5px;font-weight:400;fill:var(--brand-strong,#4338ca);text-anchor:middle;font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace","xlsxwriter.Workbook(path)",[45,548,550],{"id":549},"step-2-why-append-mode-refuses-xlsxwriter","Step 2 — Why append mode refuses XlsxWriter",[10,552,553],{},"pandas has an append mode for exactly this job, and asking for it with XlsxWriter gives the clearest explanation of the problem:",[50,555,557],{"className":96,"code":556,"language":98,"meta":55,"style":55},"try:\n    pd.ExcelWriter(\"model_append.xlsx\", engine=\"xlsxwriter\", mode=\"a\")\nexcept ValueError as exc:\n    print(\"ValueError:\", exc)\n",[14,558,559,567,597,611],{"__ignoreMap":55},[59,560,561,564],{"class":61,"line":62},[59,562,563],{"class":105},"try",[59,565,566],{"class":109},":\n",[59,568,569,572,575,577,580,582,585,587,590,592,595],{"class":61,"line":112},[59,570,571],{"class":109},"    pd.ExcelWriter(",[59,573,574],{"class":69},"\"model_append.xlsx\"",[59,576,180],{"class":109},[59,578,579],{"class":267},"engine",[59,581,153],{"class":105},[59,583,584],{"class":69},"\"xlsxwriter\"",[59,586,180],{"class":109},[59,588,589],{"class":267},"mode",[59,591,153],{"class":105},[59,593,594],{"class":69},"\"a\"",[59,596,162],{"class":109},[59,598,599,602,605,608],{"class":61,"line":126},[59,600,601],{"class":105},"except",[59,603,604],{"class":197}," ValueError",[59,606,607],{"class":105}," as",[59,609,610],{"class":109}," exc:\n",[59,612,613,616,618,621],{"class":61,"line":140},[59,614,615],{"class":197},"    print",[59,617,201],{"class":109},[59,619,620],{"class":69},"\"ValueError:\"",[59,622,623],{"class":109},", exc)\n",[10,625,626,629,630,43],{},[14,627,628],{},"Append mode is not supported with xlsxwriter!"," — appending means reading the existing workbook first, and XlsxWriter has no reader. openpyxl does, which is why append mode works only with ",[14,631,632],{},"engine=\"openpyxl\"",[45,634,636],{"id":635},"step-3-add-or-replace-a-sheet-with-openpyxl","Step 3 — Add or replace a sheet with openpyxl",[50,638,640],{"className":96,"code":639,"language":98,"meta":55,"style":55},"report = pd.DataFrame({\"Month\": [\"Oct\", \"Nov\", \"Dec\"], \"Revenue\": [120, 125, 131]})\n\nwith pd.ExcelWriter(\"model_append.xlsx\", engine=\"openpyxl\", mode=\"a\") as xw:\n    report.to_excel(xw, sheet_name=\"Report\", index=False)\nprint(load_workbook(\"model_append.xlsx\").sheetnames)\n\ntry:\n    with pd.ExcelWriter(\"model_append.xlsx\", engine=\"openpyxl\", mode=\"a\") as xw:\n        report.to_excel(xw, sheet_name=\"Report\", index=False)\nexcept ValueError as exc:\n    print(\"second run:\", exc)\n\nwith pd.ExcelWriter(\"model_append.xlsx\", engine=\"openpyxl\", mode=\"a\", if_sheet_exists=\"replace\") as xw:\n    report.to_excel(xw, sheet_name=\"Report\", index=False)\nprint(load_workbook(\"model_append.xlsx\").sheetnames)\n",[14,641,642,687,691,726,747,758,762,768,799,820,830,841,845,886,907],{"__ignoreMap":55},[59,643,644,647,649,652,654,656,658,660,662,664,667,669,671,673,675,677,679,681,684],{"class":61,"line":62},[59,645,646],{"class":109},"report ",[59,648,153],{"class":105},[59,650,651],{"class":109}," pd.DataFrame({",[59,653,226],{"class":69},[59,655,229],{"class":109},[59,657,232],{"class":69},[59,659,180],{"class":109},[59,661,237],{"class":69},[59,663,180],{"class":109},[59,665,666],{"class":69},"\"Dec\"",[59,668,240],{"class":109},[59,670,243],{"class":69},[59,672,229],{"class":109},[59,674,248],{"class":197},[59,676,180],{"class":109},[59,678,253],{"class":197},[59,680,180],{"class":109},[59,682,683],{"class":197},"131",[59,685,686],{"class":109},"]})\n",[59,688,689],{"class":61,"line":112},[59,690,144],{"emptyLinePlaceholder":143},[59,692,693,696,699,701,703,705,707,710,712,714,716,718,721,723],{"class":61,"line":126},[59,694,695],{"class":105},"with",[59,697,698],{"class":109}," pd.ExcelWriter(",[59,700,574],{"class":69},[59,702,180],{"class":109},[59,704,579],{"class":267},[59,706,153],{"class":105},[59,708,709],{"class":69},"\"openpyxl\"",[59,711,180],{"class":109},[59,713,589],{"class":267},[59,715,153],{"class":105},[59,717,594],{"class":69},[59,719,720],{"class":109},") ",[59,722,120],{"class":105},[59,724,725],{"class":109}," xw:\n",[59,727,728,731,733,735,737,739,741,743,745],{"class":61,"line":140},[59,729,730],{"class":109},"    report.to_excel(xw, ",[59,732,268],{"class":267},[59,734,153],{"class":105},[59,736,171],{"class":69},[59,738,180],{"class":109},[59,740,277],{"class":267},[59,742,153],{"class":105},[59,744,282],{"class":197},[59,746,162],{"class":109},[59,748,749,751,754,756],{"class":61,"line":147},[59,750,198],{"class":197},[59,752,753],{"class":109},"(load_workbook(",[59,755,574],{"class":69},[59,757,212],{"class":109},[59,759,760],{"class":61,"line":165},[59,761,144],{"emptyLinePlaceholder":143},[59,763,764,766],{"class":61,"line":188},[59,765,563],{"class":105},[59,767,566],{"class":109},[59,769,770,773,775,777,779,781,783,785,787,789,791,793,795,797],{"class":61,"line":194},[59,771,772],{"class":105},"    with",[59,774,698],{"class":109},[59,776,574],{"class":69},[59,778,180],{"class":109},[59,780,579],{"class":267},[59,782,153],{"class":105},[59,784,709],{"class":69},[59,786,180],{"class":109},[59,788,589],{"class":267},[59,790,153],{"class":105},[59,792,594],{"class":69},[59,794,720],{"class":109},[59,796,120],{"class":105},[59,798,725],{"class":109},[59,800,801,804,806,808,810,812,814,816,818],{"class":61,"line":215},[59,802,803],{"class":109},"        report.to_excel(xw, ",[59,805,268],{"class":267},[59,807,153],{"class":105},[59,809,171],{"class":69},[59,811,180],{"class":109},[59,813,277],{"class":267},[59,815,153],{"class":105},[59,817,282],{"class":197},[59,819,162],{"class":109},[59,821,822,824,826,828],{"class":61,"line":220},[59,823,601],{"class":105},[59,825,604],{"class":197},[59,827,607],{"class":105},[59,829,610],{"class":109},[59,831,832,834,836,839],{"class":61,"line":259},[59,833,615],{"class":197},[59,835,201],{"class":109},[59,837,838],{"class":69},"\"second run:\"",[59,840,623],{"class":109},[59,842,843],{"class":61,"line":287},[59,844,144],{"emptyLinePlaceholder":143},[59,846,848,850,852,854,856,858,860,862,864,866,868,870,872,875,877,880,882,884],{"class":61,"line":847},13,[59,849,695],{"class":105},[59,851,698],{"class":109},[59,853,574],{"class":69},[59,855,180],{"class":109},[59,857,579],{"class":267},[59,859,153],{"class":105},[59,861,709],{"class":69},[59,863,180],{"class":109},[59,865,589],{"class":267},[59,867,153],{"class":105},[59,869,594],{"class":69},[59,871,180],{"class":109},[59,873,874],{"class":267},"if_sheet_exists",[59,876,153],{"class":105},[59,878,879],{"class":69},"\"replace\"",[59,881,720],{"class":109},[59,883,120],{"class":105},[59,885,725],{"class":109},[59,887,889,891,893,895,897,899,901,903,905],{"class":61,"line":888},14,[59,890,730],{"class":109},[59,892,268],{"class":267},[59,894,153],{"class":105},[59,896,171],{"class":69},[59,898,180],{"class":109},[59,900,277],{"class":267},[59,902,153],{"class":105},[59,904,282],{"class":197},[59,906,162],{"class":109},[59,908,910,912,914,916],{"class":61,"line":909},15,[59,911,198],{"class":197},[59,913,753],{"class":109},[59,915,574],{"class":69},[59,917,212],{"class":109},[10,919,920,921,923,924,85,926,928,929,932,933,936,937,939,940,943,944,180,947,950,951,954,955,43],{},"The first run adds ",[14,922,20],{}," beside ",[14,925,16],{},[14,927,88],{},". Running the job again fails with ",[14,930,931],{},"Sheet 'Report' already exists and if_sheet_exists is set to 'error'"," — the safe default. For a job that refreshes its own sheet every day, ",[14,934,935],{},"if_sheet_exists=\"replace\""," deletes and rewrites ",[14,938,20],{}," while leaving the other sheets alone. The other options are ",[14,941,942],{},"\"new\""," (write ",[14,945,946],{},"Report1",[14,948,949],{},"Report2",", …) and ",[14,952,953],{},"\"overlay\""," (write the values into the existing sheet, keeping its formatting) — the subject of ",[39,956,958],{"href":957},"\u002Fgetting-started-with-python-excel-automation\u002Fwriting-dataframes-to-excel-with-pandas\u002Fappend-a-dataframe-to-an-existing-excel-file-with-pandas\u002F","Append a DataFrame to an Existing Excel File with pandas",[317,960,326,966,326,969,326,972,326,977,326,982,326,987,326,993,326,996,326,1000,326,1004,326,1009,326,1011,326,1021,326,1023,326,1028,326,1031,326,1035,326,1037,326,1045,326,1047,326,1052,326,1055,326,1059,326,1061,326,1066,326,1068,326,1073,326,1076,326,1080,326,1082,326,1087,326,1089,326,1094,326,1097,326,1101,326,1103,326,1108,326,1110,326,1115,326,1118,326,1122,326,1124,326,1129,326,1131],{"viewBox":961,"role":320,"ariaLabelledBy":962,"xmlns":324,"style":965},"4 0 752 276",[963,964],"xw-modes-t","xw-modes-d","width:100%;max-width:752px;height:auto;display:block;margin:1.5rem auto;font-family:Inter,ui-sans-serif,system-ui,sans-serif",[328,967,968],{"id":963},"What each writer can do with an existing workbook",[332,970,971],{"id":964},"XlsxWriter can only create new files. openpyxl in append mode can add a sheet, replace one, write a numbered new sheet or overlay values into an existing sheet, while keeping the other sheets.",[336,973],{"x":974,"y":339,"width":975,"height":976,"fill":342},"4","752","276",[336,978],{"x":979,"y":980,"width":981,"height":346,"rx":339,"fill":342,"stroke":350,"style":351},"20.0","16.0","300.0",[336,983],{"x":984,"y":980,"width":985,"height":346,"rx":339,"fill":986,"stroke":526,"style":540},"320.0","210.0","#f0f4ff",[357,988,992],{"x":989,"y":990,"style":991},"425.0","41.0","font-size:13px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","XlsxWriter",[336,994],{"x":995,"y":980,"width":985,"height":346,"rx":339,"fill":986,"stroke":526,"style":540},"530.0",[357,997,999],{"x":998,"y":990,"style":991},"635.0","openpyxl mode=\"a\"",[336,1001],{"x":979,"y":1002,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"56.0","34.0",[357,1005,1008],{"x":347,"y":1006,"style":1007},"78.0","font-size:12.5px;font-weight:700;fill:var(--text,#172033);text-anchor:start","Create a new file",[336,1010],{"x":984,"y":1002,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1013,1015],"g",{"transform":1014},"translate(416.0,64.0) scale(0.750)",[1016,1017],"path",{"d":1018,"fill":1019,"stroke":436,"style":1020},"M5 12.5l4.5 4.5L19 7","none","stroke-width:2.40px;stroke-linecap:round;stroke-linejoin:round",[336,1022],{"x":995,"y":1002,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1024,1026],{"transform":1025},"translate(626.0,64.0) scale(0.750)",[1016,1027],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1029],{"x":979,"y":1030,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"90.0",[357,1032,1034],{"x":347,"y":1033,"style":1007},"112.0","Keep the other sheets",[336,1036],{"x":984,"y":1030,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1038,1040],{"transform":1039},"translate(417.0,99.0) scale(0.667)",[1016,1041],{"d":1042,"fill":1019,"stroke":1043,"style":1044},"M6 6l12 12 M18 6L6 18","var(--accent,#f43f8f)","stroke-width:2.70px;stroke-linecap:round;stroke-linejoin:round",[336,1046],{"x":995,"y":1030,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1048,1050],{"transform":1049},"translate(626.0,98.0) scale(0.750)",[1016,1051],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1053],{"x":979,"y":1054,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"124.0",[357,1056,1058],{"x":347,"y":1057,"style":1007},"146.0","Add a sheet",[336,1060],{"x":984,"y":1054,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1062,1064],{"transform":1063},"translate(417.0,133.0) scale(0.667)",[1016,1065],{"d":1042,"fill":1019,"stroke":1043,"style":1044},[336,1067],{"x":995,"y":1054,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1069,1071],{"transform":1070},"translate(626.0,132.0) scale(0.750)",[1016,1072],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1074],{"x":979,"y":1075,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"158.0",[357,1077,1079],{"x":347,"y":1078,"style":1007},"180.0","Replace one sheet (if_sheet_exists=\"replace\")",[336,1081],{"x":984,"y":1075,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1083,1085],{"transform":1084},"translate(417.0,167.0) scale(0.667)",[1016,1086],{"d":1042,"fill":1019,"stroke":1043,"style":1044},[336,1088],{"x":995,"y":1075,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1090,1092],{"transform":1091},"translate(626.0,166.0) scale(0.750)",[1016,1093],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1095],{"x":979,"y":1096,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"192.0",[357,1098,1100],{"x":347,"y":1099,"style":1007},"214.0","Write into an existing sheet (\"overlay\")",[336,1102],{"x":984,"y":1096,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1104,1106],{"transform":1105},"translate(417.0,201.0) scale(0.667)",[1016,1107],{"d":1042,"fill":1019,"stroke":1043,"style":1044},[336,1109],{"x":995,"y":1096,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1111,1113],{"transform":1112},"translate(626.0,200.0) scale(0.750)",[1016,1114],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1116],{"x":979,"y":1117,"width":981,"height":1003,"rx":339,"fill":349,"stroke":350,"style":351},"226.0",[357,1119,1121],{"x":347,"y":1120,"style":1007},"248.0","Native charts, sparklines, rich formats",[336,1123],{"x":984,"y":1117,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1125,1127],{"transform":1126},"translate(416.0,234.0) scale(0.750)",[1016,1128],{"d":1018,"fill":1019,"stroke":436,"style":1020},[336,1130],{"x":995,"y":1117,"width":985,"height":1003,"rx":339,"fill":342,"stroke":350,"style":351},[1012,1132,1134],{"transform":1133},"translate(626.5,234.5) scale(0.708)",[1016,1135],{"d":1136,"fill":1019,"stroke":1137,"style":1138},"M12 3.5l9.5 17h-19z M12 10v5 M12 17.8v.4","var(--gold,#b4740a)","stroke-width:2.54px;stroke-linecap:round;stroke-linejoin:round",[10,1140,1141,1142,1146],{},"Note the cost: openpyxl loads and re-saves the whole workbook, and anything it does not support — images, charts it cannot read, some form controls — is lost on save, as listed in ",[39,1143,1145],{"href":1144},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fwhat-openpyxl-loses-when-saving-an-existing-workbook\u002F","What openpyxl Loses When Saving an Existing Workbook",". Test on a copy of a real workbook before trusting the round trip.",[45,1148,1150],{"id":1149},"step-4-when-you-need-xlsxwriters-features-anyway","Step 4 — When you need XlsxWriter's features anyway",[10,1152,1153,1154,1157],{},"People reach for XlsxWriter for its charts, sparklines, conditional formats and speed. To get those ",[30,1155,1156],{},"and"," keep the inputs, invert the flow: read what you need from the existing workbook, then write a complete new workbook — inputs included — with XlsxWriter, to a new name:",[50,1159,1161],{"className":96,"code":1160,"language":98,"meta":55,"style":55},"inputs = pd.read_excel(\"model_append.xlsx\", sheet_name=\"Inputs\")\nwith pd.ExcelWriter(\"model_report.xlsx\", engine=\"xlsxwriter\") as xw:\n    inputs.to_excel(xw, sheet_name=\"Inputs\", index=False)\n    report.to_excel(xw, sheet_name=\"Report\", index=False)\n    chart = xw.book.add_chart({\"type\": \"column\"})\n    chart.add_series({\"categories\": \"=Report!$A$2:$A$4\", \"values\": \"=Report!$B$2:$B$4\", \"name\": \"Revenue\"})\n    xw.sheets[\"Report\"].insert_chart(\"D2\", chart)\nprint(load_workbook(\"model_report.xlsx\").sheetnames)\n",[14,1162,1163,1186,1209,1230,1250,1272,1306,1322],{"__ignoreMap":55},[59,1164,1165,1168,1170,1173,1175,1177,1179,1181,1184],{"class":61,"line":62},[59,1166,1167],{"class":109},"inputs ",[59,1169,153],{"class":105},[59,1171,1172],{"class":109}," pd.read_excel(",[59,1174,574],{"class":69},[59,1176,180],{"class":109},[59,1178,268],{"class":267},[59,1180,153],{"class":105},[59,1182,1183],{"class":69},"\"Inputs\"",[59,1185,162],{"class":109},[59,1187,1188,1190,1192,1195,1197,1199,1201,1203,1205,1207],{"class":61,"line":112},[59,1189,695],{"class":105},[59,1191,698],{"class":109},[59,1193,1194],{"class":69},"\"model_report.xlsx\"",[59,1196,180],{"class":109},[59,1198,579],{"class":267},[59,1200,153],{"class":105},[59,1202,584],{"class":69},[59,1204,720],{"class":109},[59,1206,120],{"class":105},[59,1208,725],{"class":109},[59,1210,1211,1214,1216,1218,1220,1222,1224,1226,1228],{"class":61,"line":126},[59,1212,1213],{"class":109},"    inputs.to_excel(xw, ",[59,1215,268],{"class":267},[59,1217,153],{"class":105},[59,1219,1183],{"class":69},[59,1221,180],{"class":109},[59,1223,277],{"class":267},[59,1225,153],{"class":105},[59,1227,282],{"class":197},[59,1229,162],{"class":109},[59,1231,1232,1234,1236,1238,1240,1242,1244,1246,1248],{"class":61,"line":140},[59,1233,730],{"class":109},[59,1235,268],{"class":267},[59,1237,153],{"class":105},[59,1239,171],{"class":69},[59,1241,180],{"class":109},[59,1243,277],{"class":267},[59,1245,153],{"class":105},[59,1247,282],{"class":197},[59,1249,162],{"class":109},[59,1251,1252,1255,1257,1260,1263,1266,1269],{"class":61,"line":147},[59,1253,1254],{"class":109},"    chart ",[59,1256,153],{"class":105},[59,1258,1259],{"class":109}," xw.book.add_chart({",[59,1261,1262],{"class":69},"\"type\"",[59,1264,1265],{"class":109},": ",[59,1267,1268],{"class":69},"\"column\"",[59,1270,1271],{"class":109},"})\n",[59,1273,1274,1277,1280,1282,1285,1287,1290,1292,1295,1297,1300,1302,1304],{"class":61,"line":165},[59,1275,1276],{"class":109},"    chart.add_series({",[59,1278,1279],{"class":69},"\"categories\"",[59,1281,1265],{"class":109},[59,1283,1284],{"class":69},"\"=Report!$A$2:$A$4\"",[59,1286,180],{"class":109},[59,1288,1289],{"class":69},"\"values\"",[59,1291,1265],{"class":109},[59,1293,1294],{"class":69},"\"=Report!$B$2:$B$4\"",[59,1296,180],{"class":109},[59,1298,1299],{"class":69},"\"name\"",[59,1301,1265],{"class":109},[59,1303,243],{"class":69},[59,1305,1271],{"class":109},[59,1307,1308,1311,1313,1316,1319],{"class":61,"line":188},[59,1309,1310],{"class":109},"    xw.sheets[",[59,1312,171],{"class":69},[59,1314,1315],{"class":109},"].insert_chart(",[59,1317,1318],{"class":69},"\"D2\"",[59,1320,1321],{"class":109},", chart)\n",[59,1323,1324,1326,1328,1330],{"class":61,"line":194},[59,1325,198],{"class":197},[59,1327,753],{"class":109},[59,1329,1194],{"class":69},[59,1331,212],{"class":109},[10,1333,1334],{},"The source workbook stays untouched, and the output is a fresh file built entirely by XlsxWriter. This is the cleanest arrangement for reports: inputs are maintained in one file by people, outputs are generated into another by the script, and nothing generated ever writes back over something maintained.",[317,1336,326,1341,326,1344,326,1347,326,1350,326,1356,326,1364,326,1369,326,1373,326,1377,326,1380,326,1383,326,1390,326,1395,326,1401,326,1408,326,1413,326,1418,326,1420,326,1423,326,1427,326,1430,326,1433,326,1439,326,1442,326,1445,326,1447,326,1450,326,1454,326,1457,326,1461,326,1468,326,1472],{"viewBox":1337,"role":320,"ariaLabelledBy":1338,"xmlns":324,"style":965},"4 0 752 272",[1339,1340],"xw-choose-t","xw-choose-d",[328,1342,1343],{"id":1339},"Which approach to use",[332,1345,1346],{"id":1340},"To add or replace a sheet in a workbook people maintain, use pandas with openpyxl in append mode. To produce a report with XlsxWriter features, read the inputs and write a new file. To fill a formatted template, load it with openpyxl and save under a new name.",[336,1348],{"x":974,"y":339,"width":975,"height":1349,"fill":342},"272",[336,1351],{"x":1352,"y":980,"width":1353,"height":1354,"rx":1355,"fill":986,"stroke":526,"style":527},"211.1","337.7","75.5","12",[1012,1357,1359],{"transform":1358},"translate(368.0,29.0) scale(1.000)",[1016,1360],{"d":1361,"fill":1019,"stroke":1362,"style":1363},"M6 3h9l4 4v14H6z M15 3v4h4 M9.5 11.5l4.5 6 M14 11.5l-4.5 6","var(--brand-strong,#4338ca)","stroke-width:1.80px;stroke-linecap:round;stroke-linejoin:round",[357,1365,1368],{"x":1366,"y":1354,"style":1367},"380.0","font-size:13.5px;font-weight:700;fill:var(--brand-strong,#4338ca);text-anchor:middle","What should happen to the existing file?",[61,1370],{"x1":1366,"y1":1371,"x2":1366,"y2":1372,"stroke":526,"style":527},"91.5","111.5",[61,1374],{"x1":1375,"y1":1372,"x2":1376,"y2":1372,"stroke":526,"style":527},"134.7","625.3",[61,1378],{"x1":1375,"y1":1372,"x2":1375,"y2":1379,"stroke":526,"style":527},"157.5",[529,1381],{"points":1382,"fill":532},"134.7,161.5 129.7,152.5 139.7,152.5",[336,1384],{"x":1385,"y":1386,"width":1387,"height":1388,"rx":1389,"fill":342,"stroke":350,"style":540},"68.4","121.5","132.5","21.0","10",[357,1391,1394],{"x":1375,"y":1392,"style":1393},"136.0","font-size:11.5px;font-weight:700;fill:var(--muted,#5b6780);text-anchor:middle","keep it, add a sheet",[336,1396],{"x":979,"y":1397,"width":1398,"height":1399,"rx":1355,"fill":1400,"stroke":436,"style":527},"163.5","229.3","93.0","#d9f4f1",[1012,1402,1404],{"transform":1403},"translate(122.7,176.5) scale(1.000)",[1016,1405],{"d":1406,"fill":1019,"stroke":1407,"style":1363},"M12 3l9 5-9 5-9-5z M3 13l9 5 9-5 M3 17l9 5 9-5","var(--teal-ink,#0b6157)",[357,1409,1412],{"x":1375,"y":1410,"style":1411},"223.0","font-size:13.5px;font-weight:700;fill:var(--teal-ink,#0b6157);text-anchor:middle","openpyxl append mode",[357,1414,1417],{"x":1375,"y":1415,"style":1416},"242.5","font-size:11.5px;font-weight:400;fill:var(--text,#172033);text-anchor:middle","mode=\"a\", if_sheet_exists",[61,1419],{"x1":1366,"y1":1372,"x2":1366,"y2":1379,"stroke":526,"style":527},[529,1421],{"points":1422,"fill":532},"380.0,161.5 375.0,152.5 385.0,152.5",[336,1424],{"x":1425,"y":1386,"width":1426,"height":1388,"rx":1389,"fill":342,"stroke":350,"style":540},"316.4","127.2",[357,1428,1429],{"x":1366,"y":1392,"style":1393},"leave it untouched",[336,1431],{"x":1432,"y":1397,"width":1398,"height":1399,"rx":1355,"fill":986,"stroke":526,"style":527},"265.3",[1012,1434,1436],{"transform":1435},"translate(368.0,176.5) scale(1.000)",[1016,1437],{"d":1438,"fill":1019,"stroke":1362,"style":1363},"M6 3h9l4 4v14H6z M15 3v4h4",[357,1440,1441],{"x":1366,"y":1410,"style":1367},"Write a new file",[357,1443,1444],{"x":1366,"y":1415,"style":1416},"XlsxWriter for charts and speed",[61,1446],{"x1":1376,"y1":1372,"x2":1376,"y2":1379,"stroke":526,"style":527},[529,1448],{"points":1449,"fill":532},"625.3,161.5 620.3,152.5 630.3,152.5",[336,1451],{"x":1452,"y":1386,"width":1453,"height":1388,"rx":1389,"fill":342,"stroke":350,"style":540},"580.5","89.7",[357,1455,1456],{"x":1376,"y":1392,"style":1393},"fill its layout",[336,1458],{"x":1459,"y":1397,"width":1398,"height":1399,"rx":1355,"fill":1460,"stroke":1137,"style":527},"510.7","#fdefd8",[1012,1462,1464],{"transform":1463},"translate(613.3,176.5) scale(1.000)",[1016,1465],{"d":1466,"fill":1019,"stroke":1467,"style":1363},"M4 4h16v16H4z M4 9h16 M4 14h16 M10 4v16","var(--gold-ink,#7a4e06)",[357,1469,1471],{"x":1376,"y":1410,"style":1470},"font-size:13.5px;font-weight:700;fill:var(--gold-ink,#7a4e06);text-anchor:middle","Load the template",[357,1473,1474],{"x":1376,"y":1415,"style":1416},"openpyxl, save as new name",[45,1476,1478],{"id":1477},"step-5-guard-against-overwriting-by-accident","Step 5 — Guard against overwriting by accident",[10,1480,1481],{},"The overwrite in Step 1 was silent. A tiny wrapper makes it loud, and writes atomically so a failed run never leaves a half-written file in place:",[50,1483,1485],{"className":96,"code":1484,"language":98,"meta":55,"style":55},"import os\nimport tempfile\nfrom pathlib import Path\n\ndef safe_output_path(path, overwrite=False):\n    path = Path(path)\n    if path.exists() and not overwrite:\n        raise FileExistsError(f\"{path} already exists; pass overwrite=True or choose a new name\")\n    return path\n\ndef write_new_workbook(path, frames, overwrite=False):\n    target = safe_output_path(path, overwrite)\n    fd, tmp = tempfile.mkstemp(suffix=\".xlsx\", dir=target.parent)\n    os.close(fd)\n    with pd.ExcelWriter(tmp, engine=\"xlsxwriter\") as xw:\n        for name, df in frames.items():\n            df.to_excel(xw, sheet_name=name, index=False)\n    os.replace(tmp, target)\n    return target\n\ntry:\n    write_new_workbook(\"model_append.xlsx\", {\"Report\": report})\nexcept FileExistsError as exc:\n    print(\"refused:\", exc)\nprint(\"written:\", write_new_workbook(\"report_2026-10.xlsx\", {\"Report\": report}))\n",[14,1486,1487,1494,1501,1513,1517,1536,1546,1562,1592,1600,1604,1620,1630,1658,1663,1682,1697,1718,1724,1732,1737,1744,1760,1771,1783],{"__ignoreMap":55},[59,1488,1489,1491],{"class":61,"line":62},[59,1490,106],{"class":105},[59,1492,1493],{"class":109}," os\n",[59,1495,1496,1498],{"class":61,"line":112},[59,1497,106],{"class":105},[59,1499,1500],{"class":109}," tempfile\n",[59,1502,1503,1505,1508,1510],{"class":61,"line":126},[59,1504,129],{"class":105},[59,1506,1507],{"class":109}," pathlib ",[59,1509,106],{"class":105},[59,1511,1512],{"class":109}," Path\n",[59,1514,1515],{"class":61,"line":140},[59,1516,144],{"emptyLinePlaceholder":143},[59,1518,1519,1522,1526,1529,1531,1533],{"class":61,"line":147},[59,1520,1521],{"class":105},"def",[59,1523,1525],{"class":1524},"s_Opv"," safe_output_path",[59,1527,1528],{"class":109},"(path, overwrite",[59,1530,153],{"class":105},[59,1532,282],{"class":197},[59,1534,1535],{"class":109},"):\n",[59,1537,1538,1541,1543],{"class":61,"line":165},[59,1539,1540],{"class":109},"    path ",[59,1542,153],{"class":105},[59,1544,1545],{"class":109}," Path(path)\n",[59,1547,1548,1551,1554,1556,1559],{"class":61,"line":188},[59,1549,1550],{"class":105},"    if",[59,1552,1553],{"class":109}," path.exists() ",[59,1555,1156],{"class":105},[59,1557,1558],{"class":105}," not",[59,1560,1561],{"class":109}," overwrite:\n",[59,1563,1564,1567,1570,1572,1575,1578,1582,1584,1587,1590],{"class":61,"line":194},[59,1565,1566],{"class":105},"        raise",[59,1568,1569],{"class":197}," FileExistsError",[59,1571,201],{"class":109},[59,1573,1574],{"class":105},"f",[59,1576,1577],{"class":69},"\"",[59,1579,1581],{"class":1580},"sSjpA","{",[59,1583,1016],{"class":109},[59,1585,1586],{"class":1580},"}",[59,1588,1589],{"class":69}," already exists; pass overwrite=True or choose a new name\"",[59,1591,162],{"class":109},[59,1593,1594,1597],{"class":61,"line":215},[59,1595,1596],{"class":105},"    return",[59,1598,1599],{"class":109}," path\n",[59,1601,1602],{"class":61,"line":220},[59,1603,144],{"emptyLinePlaceholder":143},[59,1605,1606,1608,1611,1614,1616,1618],{"class":61,"line":259},[59,1607,1521],{"class":105},[59,1609,1610],{"class":1524}," write_new_workbook",[59,1612,1613],{"class":109},"(path, frames, overwrite",[59,1615,153],{"class":105},[59,1617,282],{"class":197},[59,1619,1535],{"class":109},[59,1621,1622,1625,1627],{"class":61,"line":287},[59,1623,1624],{"class":109},"    target ",[59,1626,153],{"class":105},[59,1628,1629],{"class":109}," safe_output_path(path, overwrite)\n",[59,1631,1632,1635,1637,1640,1643,1645,1648,1650,1653,1655],{"class":61,"line":847},[59,1633,1634],{"class":109},"    fd, tmp ",[59,1636,153],{"class":105},[59,1638,1639],{"class":109}," tempfile.mkstemp(",[59,1641,1642],{"class":267},"suffix",[59,1644,153],{"class":105},[59,1646,1647],{"class":69},"\".xlsx\"",[59,1649,180],{"class":109},[59,1651,1652],{"class":267},"dir",[59,1654,153],{"class":105},[59,1656,1657],{"class":109},"target.parent)\n",[59,1659,1660],{"class":61,"line":888},[59,1661,1662],{"class":109},"    os.close(fd)\n",[59,1664,1665,1667,1670,1672,1674,1676,1678,1680],{"class":61,"line":909},[59,1666,772],{"class":105},[59,1668,1669],{"class":109}," pd.ExcelWriter(tmp, ",[59,1671,579],{"class":267},[59,1673,153],{"class":105},[59,1675,584],{"class":69},[59,1677,720],{"class":109},[59,1679,120],{"class":105},[59,1681,725],{"class":109},[59,1683,1685,1688,1691,1694],{"class":61,"line":1684},16,[59,1686,1687],{"class":105},"        for",[59,1689,1690],{"class":109}," name, df ",[59,1692,1693],{"class":105},"in",[59,1695,1696],{"class":109}," frames.items():\n",[59,1698,1700,1703,1705,1707,1710,1712,1714,1716],{"class":61,"line":1699},17,[59,1701,1702],{"class":109},"            df.to_excel(xw, ",[59,1704,268],{"class":267},[59,1706,153],{"class":105},[59,1708,1709],{"class":109},"name, ",[59,1711,277],{"class":267},[59,1713,153],{"class":105},[59,1715,282],{"class":197},[59,1717,162],{"class":109},[59,1719,1721],{"class":61,"line":1720},18,[59,1722,1723],{"class":109},"    os.replace(tmp, target)\n",[59,1725,1727,1729],{"class":61,"line":1726},19,[59,1728,1596],{"class":105},[59,1730,1731],{"class":109}," target\n",[59,1733,1735],{"class":61,"line":1734},20,[59,1736,144],{"emptyLinePlaceholder":143},[59,1738,1740,1742],{"class":61,"line":1739},21,[59,1741,563],{"class":105},[59,1743,566],{"class":109},[59,1745,1747,1750,1752,1755,1757],{"class":61,"line":1746},22,[59,1748,1749],{"class":109},"    write_new_workbook(",[59,1751,574],{"class":69},[59,1753,1754],{"class":109},", {",[59,1756,171],{"class":69},[59,1758,1759],{"class":109},": report})\n",[59,1761,1763,1765,1767,1769],{"class":61,"line":1762},23,[59,1764,601],{"class":105},[59,1766,1569],{"class":197},[59,1768,607],{"class":105},[59,1770,610],{"class":109},[59,1772,1774,1776,1778,1781],{"class":61,"line":1773},24,[59,1775,615],{"class":197},[59,1777,201],{"class":109},[59,1779,1780],{"class":69},"\"refused:\"",[59,1782,623],{"class":109},[59,1784,1786,1788,1790,1793,1796,1799,1801,1803],{"class":61,"line":1785},25,[59,1787,198],{"class":197},[59,1789,201],{"class":109},[59,1791,1792],{"class":69},"\"written:\"",[59,1794,1795],{"class":109},", write_new_workbook(",[59,1797,1798],{"class":69},"\"report_2026-10.xlsx\"",[59,1800,1754],{"class":109},[59,1802,171],{"class":69},[59,1804,1805],{"class":109},": report}))\n",[10,1807,1808,1809,1812,1813,43],{},"The guard refuses to replace an existing file unless the caller says so explicitly, and the temporary-file-then-",[14,1810,1811],{},"os.replace"," step means the target is either the old file or the complete new one, never something in between. Atomic writes are covered in depth in ",[39,1814,1816],{"href":1815},"\u002Fautomating-reporting-workflows\u002Ferror-handling-and-logging-in-excel-automation\u002Fwrite-excel-files-atomically-in-python\u002F","Write Excel Files Atomically in Python",[45,1818,1820],{"id":1819},"step-6-keep-a-backup-before-any-job-touches-a-maintained-file","Step 6 — Keep a backup before any job touches a maintained file",[10,1822,1823,1824,1827],{},"If a workbook has already been overwritten, recovery depends on where it lived: OneDrive and SharePoint keep a version history (right-click the file › ",[30,1825,1826],{},"Version history","), Windows File History and macOS Time Machine keep snapshots if they are enabled, and a file under version control can be checked out again. A local folder with none of these has no undo. For jobs that do modify maintained workbooks in place — the append mode from Step 3 — take a timestamped copy first and prune old copies:",[50,1829,1831],{"className":96,"code":1830,"language":98,"meta":55,"style":55},"import shutil\nfrom datetime import datetime\n\ndef backup(path, folder=\"backups\", keep=10):\n    src = Path(path)\n    dest_dir = src.parent \u002F folder\n    dest_dir.mkdir(exist_ok=True)\n    stamp = datetime.now().strftime(\"%Y%m%d-%H%M%S-%f\")\n    copy = dest_dir \u002F f\"{src.stem}.{stamp}{src.suffix}\"\n    shutil.copy2(src, copy)\n    for old in sorted(dest_dir.glob(f\"{src.stem}.*{src.suffix}\"))[:-keep]:\n        old.unlink()\n    return copy\n\nfor _ in range(3):\n    saved = backup(\"model_append.xlsx\", keep=2)\nwith pd.ExcelWriter(\"model_append.xlsx\", engine=\"openpyxl\", mode=\"a\", if_sheet_exists=\"replace\") as xw:\n    report.to_excel(xw, sheet_name=\"Report\", index=False)\nprint(\"latest backup:\", saved.name, \"| kept:\", len(list(Path(\"backups\").glob(\"model_append.*.xlsx\"))))\n",[14,1832,1833,1840,1852,1856,1880,1889,1905,1920,1946,1988,1993,2039,2044,2051,2055,2074,2097,2135,2155],{"__ignoreMap":55},[59,1834,1835,1837],{"class":61,"line":62},[59,1836,106],{"class":105},[59,1838,1839],{"class":109}," shutil\n",[59,1841,1842,1844,1847,1849],{"class":61,"line":112},[59,1843,129],{"class":105},[59,1845,1846],{"class":109}," datetime ",[59,1848,106],{"class":105},[59,1850,1851],{"class":109}," datetime\n",[59,1853,1854],{"class":61,"line":126},[59,1855,144],{"emptyLinePlaceholder":143},[59,1857,1858,1860,1863,1866,1868,1871,1874,1876,1878],{"class":61,"line":140},[59,1859,1521],{"class":105},[59,1861,1862],{"class":1524}," backup",[59,1864,1865],{"class":109},"(path, folder",[59,1867,153],{"class":105},[59,1869,1870],{"class":69},"\"backups\"",[59,1872,1873],{"class":109},", keep",[59,1875,153],{"class":105},[59,1877,1389],{"class":197},[59,1879,1535],{"class":109},[59,1881,1882,1885,1887],{"class":61,"line":147},[59,1883,1884],{"class":109},"    src ",[59,1886,153],{"class":105},[59,1888,1545],{"class":109},[59,1890,1891,1894,1896,1899,1902],{"class":61,"line":165},[59,1892,1893],{"class":109},"    dest_dir ",[59,1895,153],{"class":105},[59,1897,1898],{"class":109}," src.parent ",[59,1900,1901],{"class":105},"\u002F",[59,1903,1904],{"class":109}," folder\n",[59,1906,1907,1910,1913,1915,1918],{"class":61,"line":188},[59,1908,1909],{"class":109},"    dest_dir.mkdir(",[59,1911,1912],{"class":267},"exist_ok",[59,1914,153],{"class":105},[59,1916,1917],{"class":197},"True",[59,1919,162],{"class":109},[59,1921,1922,1925,1927,1930,1933,1936,1939,1942,1944],{"class":61,"line":194},[59,1923,1924],{"class":109},"    stamp ",[59,1926,153],{"class":105},[59,1928,1929],{"class":109}," datetime.now().strftime(",[59,1931,1932],{"class":69},"\"%Y%m",[59,1934,1935],{"class":1580},"%d",[59,1937,1938],{"class":69},"-%H%M%S-",[59,1940,1941],{"class":1580},"%f",[59,1943,1577],{"class":69},[59,1945,162],{"class":109},[59,1947,1948,1951,1953,1956,1958,1961,1963,1965,1968,1970,1972,1974,1977,1980,1983,1985],{"class":61,"line":215},[59,1949,1950],{"class":109},"    copy ",[59,1952,153],{"class":105},[59,1954,1955],{"class":109}," dest_dir ",[59,1957,1901],{"class":105},[59,1959,1960],{"class":105}," f",[59,1962,1577],{"class":69},[59,1964,1581],{"class":1580},[59,1966,1967],{"class":109},"src.stem",[59,1969,1586],{"class":1580},[59,1971,43],{"class":69},[59,1973,1581],{"class":1580},[59,1975,1976],{"class":109},"stamp",[59,1978,1979],{"class":1580},"}{",[59,1981,1982],{"class":109},"src.suffix",[59,1984,1586],{"class":1580},[59,1986,1987],{"class":69},"\"\n",[59,1989,1990],{"class":61,"line":220},[59,1991,1992],{"class":109},"    shutil.copy2(src, copy)\n",[59,1994,1995,1998,2001,2003,2006,2009,2011,2013,2015,2017,2019,2022,2024,2026,2028,2030,2033,2036],{"class":61,"line":259},[59,1996,1997],{"class":105},"    for",[59,1999,2000],{"class":109}," old ",[59,2002,1693],{"class":105},[59,2004,2005],{"class":197}," sorted",[59,2007,2008],{"class":109},"(dest_dir.glob(",[59,2010,1574],{"class":105},[59,2012,1577],{"class":69},[59,2014,1581],{"class":1580},[59,2016,1967],{"class":109},[59,2018,1586],{"class":1580},[59,2020,2021],{"class":69},".*",[59,2023,1581],{"class":1580},[59,2025,1982],{"class":109},[59,2027,1586],{"class":1580},[59,2029,1577],{"class":69},[59,2031,2032],{"class":109},"))[:",[59,2034,2035],{"class":105},"-",[59,2037,2038],{"class":109},"keep]:\n",[59,2040,2041],{"class":61,"line":287},[59,2042,2043],{"class":109},"        old.unlink()\n",[59,2045,2046,2048],{"class":61,"line":847},[59,2047,1596],{"class":105},[59,2049,2050],{"class":109}," copy\n",[59,2052,2053],{"class":61,"line":888},[59,2054,144],{"emptyLinePlaceholder":143},[59,2056,2057,2060,2063,2065,2068,2070,2072],{"class":61,"line":909},[59,2058,2059],{"class":105},"for",[59,2061,2062],{"class":109}," _ ",[59,2064,1693],{"class":105},[59,2066,2067],{"class":197}," range",[59,2069,201],{"class":109},[59,2071,415],{"class":197},[59,2073,1535],{"class":109},[59,2075,2076,2079,2081,2084,2086,2088,2091,2093,2095],{"class":61,"line":1684},[59,2077,2078],{"class":109},"    saved ",[59,2080,153],{"class":105},[59,2082,2083],{"class":109}," backup(",[59,2085,574],{"class":69},[59,2087,180],{"class":109},[59,2089,2090],{"class":267},"keep",[59,2092,153],{"class":105},[59,2094,397],{"class":197},[59,2096,162],{"class":109},[59,2098,2099,2101,2103,2105,2107,2109,2111,2113,2115,2117,2119,2121,2123,2125,2127,2129,2131,2133],{"class":61,"line":1699},[59,2100,695],{"class":105},[59,2102,698],{"class":109},[59,2104,574],{"class":69},[59,2106,180],{"class":109},[59,2108,579],{"class":267},[59,2110,153],{"class":105},[59,2112,709],{"class":69},[59,2114,180],{"class":109},[59,2116,589],{"class":267},[59,2118,153],{"class":105},[59,2120,594],{"class":69},[59,2122,180],{"class":109},[59,2124,874],{"class":267},[59,2126,153],{"class":105},[59,2128,879],{"class":69},[59,2130,720],{"class":109},[59,2132,120],{"class":105},[59,2134,725],{"class":109},[59,2136,2137,2139,2141,2143,2145,2147,2149,2151,2153],{"class":61,"line":1720},[59,2138,730],{"class":109},[59,2140,268],{"class":267},[59,2142,153],{"class":105},[59,2144,171],{"class":69},[59,2146,180],{"class":109},[59,2148,277],{"class":267},[59,2150,153],{"class":105},[59,2152,282],{"class":197},[59,2154,162],{"class":109},[59,2156,2157,2159,2161,2164,2167,2170,2172,2175,2177,2180,2183,2185,2188,2191],{"class":61,"line":1726},[59,2158,198],{"class":197},[59,2160,201],{"class":109},[59,2162,2163],{"class":69},"\"latest backup:\"",[59,2165,2166],{"class":109},", saved.name, ",[59,2168,2169],{"class":69},"\"| kept:\"",[59,2171,180],{"class":109},[59,2173,2174],{"class":197},"len",[59,2176,201],{"class":109},[59,2178,2179],{"class":197},"list",[59,2181,2182],{"class":109},"(Path(",[59,2184,1870],{"class":69},[59,2186,2187],{"class":109},").glob(",[59,2189,2190],{"class":69},"\"model_append.*.xlsx\"",[59,2192,2193],{"class":109},"))))\n",[10,2195,2196,2199,2200,2202],{},[14,2197,2198],{},"shutil.copy2"," keeps the original timestamps, the time stamp down to microseconds keeps copies from colliding when a job runs twice in a second, and sorting the names (which start with the same stem and a sortable time stamp) puts the oldest first, so slicing off all but the last ",[14,2201,2090],{}," copies prunes the rest. Ten copies of a workbook are usually a few megabytes — cheap insurance for a file someone spent months building.",[45,2204,2206],{"id":2205},"performance-notes","Performance notes",[10,2208,2209],{},"Append mode is slower than writing a fresh file, because openpyxl must load and re-serialise every sheet, not just the one you add. For a workbook with a few small input sheets the difference is negligible; for one with a 200,000-row data sheet, each append run pays the cost of reading and rewriting those rows. If that becomes noticeable, it is a sign the data belongs in its own file, with the maintained workbook referring to the generated one rather than containing it.",[45,2211,2213],{"id":2212},"common-pitfalls","Common pitfalls",[2215,2216,2217,2232,2238,2250,2263,2269],"ul",{},[2218,2219,2220,2227,2228,2231],"li",{},[2221,2222,2223,2226],"strong",{},[14,2224,2225],{},"to_excel(path)"," on an existing workbook."," It always creates a new file; use an ",[14,2229,2230],{},"ExcelWriter"," in append mode.",[2218,2233,2234,2237],{},[2221,2235,2236],{},"Expecting a warning."," Neither XlsxWriter nor pandas warns before replacing a file.",[2218,2239,2240,2243,2244,2247,2248,43],{},[2221,2241,2242],{},"Append mode with the wrong engine."," ",[14,2245,2246],{},"mode=\"a\""," needs ",[14,2249,632],{},[2218,2251,2252,2255,2256,2259,2260,2262],{},[2221,2253,2254],{},"Re-running an append job."," The default ",[14,2257,2258],{},"if_sheet_exists=\"error\""," fails on the second run; choose ",[14,2261,879],{}," deliberately.",[2218,2264,2265,2268],{},[2221,2266,2267],{},"Assuming openpyxl's round trip is lossless."," It drops images and unsupported objects; check a real file.",[2218,2270,2271,2274,2275,43],{},[2221,2272,2273],{},"The file is open in Excel."," On Windows, writing to a workbook someone has open fails with a permission error rather than overwriting it — the one case where the operating system protects you; see ",[39,2276,2278],{"href":2277},"\u002Fautomating-reporting-workflows\u002Ferror-handling-and-logging-in-excel-automation\u002Fhandle-permission-denied-when-writing-excel-in-python\u002F","Handle Permission Denied When Writing Excel in Python",[45,2280,2282],{"id":2281},"conclusion","Conclusion",[10,2284,2285,2286,2288,2289,2292,2293,2295],{},"XlsxWriter only writes new files, so pointing it — or ",[14,2287,314],{}," with a path — at an existing workbook replaces the workbook. To add or refresh a sheet while keeping the others, use ",[14,2290,2291],{},"pd.ExcelWriter(path, engine=\"openpyxl\", mode=\"a\")"," with an explicit ",[14,2294,874],{},". When you need XlsxWriter's features, read the inputs and generate a separate output file. And wrap output paths in a guard that refuses to overwrite and writes atomically, so a mistake is an error message rather than a lost workbook.",[45,2297,2299],{"id":2298},"frequently-asked-questions","Frequently asked questions",[10,2301,2302,2305],{},[2221,2303,2304],{},"Can XlsxWriter modify an existing Excel file?","\nNo. XlsxWriter can only create new files. Creating a Workbook with the name of an existing file replaces that file when the workbook is closed.",[10,2307,2308,2311],{},[2221,2309,2310],{},"Why did pandas to_excel delete my other sheets?","\nWriting to a path creates a new file. To add a sheet to an existing workbook, use pd.ExcelWriter(path, engine=\"openpyxl\", mode=\"a\") and call to_excel on the writer.",[10,2313,2314,2317],{},[2221,2315,2316],{},"What does \"Append mode is not supported with xlsxwriter!\" mean?","\nAppend mode has to read the existing workbook first, and XlsxWriter has no reader. Use engine=\"openpyxl\" for mode=\"a\".",[10,2319,2320,2323],{},[2221,2321,2322],{},"How do I replace one sheet but keep the rest of the workbook?","\nUse pd.ExcelWriter(path, engine=\"openpyxl\", mode=\"a\", if_sheet_exists=\"replace\"). The named sheet is deleted and rewritten, and the other sheets are kept.",[45,2325,2327],{"id":2326},"related","Related",[2215,2329,2330,2336,2343,2350],{},[2218,2331,2332,2333,2335],{},"Up one level: ",[39,2334,42],{"href":41}," — the errors behind common surprises.",[2218,2337,2338,2342],{},[39,2339,2341],{"href":2340},"\u002Fgetting-started-with-python-excel-automation\u002Fwriting-dataframes-to-excel-with-pandas\u002Fopenpyxl-vs-xlsxwriter-vs-pandas-excelwriter\u002F","openpyxl vs xlsxwriter vs pandas ExcelWriter"," — which writer does what.",[2218,2344,2345,2349],{},[39,2346,2348],{"href":2347},"\u002Fautomating-reporting-workflows\u002Fbuilding-multi-sheet-excel-dashboards\u002Fwrite-multiple-dataframes-to-one-excel-file\u002F","Write Multiple DataFrames to One Excel File"," — several sheets in one pass.",[2218,2351,2352,2356],{},[39,2353,2355],{"href":2354},"\u002Fautomating-reporting-workflows\u002Fgenerating-excel-reports-from-templates\u002Ffill-excel-template-with-python-openpyxl\u002F","Fill an Excel Template with Python and openpyxl"," — editing a formatted workbook safely.",[2358,2359,2360],"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 .sa561, html code.shiki .sa561{--shiki-default:#E36209;--shiki-dark:#FFB757}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":55,"searchDepth":112,"depth":112,"links":2362},[2363,2364,2365,2366,2367,2368,2369,2370,2371,2372,2373,2374],{"id":47,"depth":112,"text":48},{"id":92,"depth":112,"text":93},{"id":549,"depth":112,"text":550},{"id":635,"depth":112,"text":636},{"id":1149,"depth":112,"text":1150},{"id":1477,"depth":112,"text":1478},{"id":1819,"depth":112,"text":1820},{"id":2205,"depth":112,"text":2206},{"id":2212,"depth":112,"text":2213},{"id":2281,"depth":112,"text":2282},{"id":2298,"depth":112,"text":2299},{"id":2326,"depth":112,"text":2327},"2026-10-02","You pointed XlsxWriter — or pandas with its default engine — at an existing workbook to add a sheet, and every other sheet vanished. XlsxWriter only creates new files and never reads old ones. See why, what \"Append mode is not supported with xlsxwriter!\" means, how to add or replace a sheet with openpyxl's append mode and if_sheet_exists, and how to guard against overwriting a file by accident.","md",[2379,2381,2383,2385],{"q":2304,"a":2380},"No. XlsxWriter can only create new files. Creating a Workbook with the name of an existing file replaces that file when the workbook is closed.",{"q":2310,"a":2382},"Writing to a path creates a new file. To add a sheet to an existing workbook, use pd.ExcelWriter(path, engine=\"openpyxl\", mode=\"a\") and call to_excel on the writer.",{"q":2316,"a":2384},"Append mode has to read the existing workbook first, and XlsxWriter has no reader. Use engine=\"openpyxl\" for mode=\"a\".",{"q":2322,"a":2386},"Use pd.ExcelWriter(path, engine=\"openpyxl\", mode=\"a\", if_sheet_exists=\"replace\"). The named sheet is deleted and rewritten, and the other sheets are kept.","fix xlsxwriter overwriting an existing workbook",null,{"breadcrumb":2390},[2391,2393,2396],{"name":2392,"item":1901},"Home",{"name":2394,"item":2395},"Getting Started with Python Excel Automation","\u002Fgetting-started-with-python-excel-automation\u002F",{"name":42,"item":41},"\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook",{"title":2399,"description":2400},"Fix XlsxWriter or pandas Overwriting an Existing Excel File","XlsxWriter cannot modify existing files: Workbook(path) replaces them at close, to_excel overwrites, and mode=\"a\" raises \"Append mode is not supported with xlsxwriter!\". Add sheets with openpyxl mode=\"a\" and if_sheet_exists, keep inputs separate, and guard paths.","fix-xlsxwriter-overwriting-an-existing-workbook","getting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-overwriting-an-existing-workbook\u002Findex",[2404,2405,2406],"troubleshooting","xlsxwriter","writing-excel","how-to","t-kynDoLQmUaM9eJdtezPcCW6pH5yxVi5qfB6A69xH4",[2410,2414],{"title":2411,"path":2412,"stem":2413,"children":-1},"Fix xlsxwriter \"Exception caught in workbook destructor\"","\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-exception-caught-in-workbook-destructor","getting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors\u002Ffix-xlsxwriter-exception-caught-in-workbook-destructor\u002Findex",{"title":2415,"path":2416,"stem":2417,"children":-1},"Using openpyxl for Excel File Manipulation","\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation","getting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Findex",[2419,2421],{"path":2420,"title":2394},"\u002Fgetting-started-with-python-excel-automation",{"path":2422,"title":42},"\u002Fgetting-started-with-python-excel-automation\u002Ftroubleshooting-common-python-excel-errors",[],[2425,2428,2431,2434,2437,2440],{"path":2426,"title":2278,"kind":2427},"\u002Fautomating-reporting-workflows\u002Ferror-handling-and-logging-in-excel-automation\u002Fhandle-permission-denied-when-writing-excel-in-python","fix",{"path":2429,"title":2430,"kind":2407},"\u002Fgetting-started-with-python-excel-automation\u002Fusing-openpyxl-for-excel-file-manipulation\u002Fwhat-openpyxl-loses-when-saving-an-existing-workbook","What openpyxl Loses When You Save an Existing Workbook",{"path":2432,"title":2433,"kind":2427},"\u002Fadvanced-data-transformation-and-cleaning\u002Fworking-with-dates-and-times-in-excel-data\u002Ffix-excel-serial-numbers-showing-instead-of-dates","Fix Excel Serial Numbers Showing Instead of Dates",{"path":2435,"title":2436,"kind":2407},"\u002Fadvanced-data-transformation-and-cleaning\u002Fworking-with-dates-and-times-in-excel-data\u002Fhandle-daylight-saving-time-in-excel-timestamps","Handle Daylight Saving Time in Excel Timestamps",{"path":2438,"title":2439,"kind":2407},"\u002Fautomating-reporting-workflows\u002Fautomating-excel-online-with-microsoft-graph\u002Fhandle-microsoft-graph-throttling-for-excel","Handle Microsoft Graph Throttling in Excel Automation",{"path":2441,"title":2442,"kind":2443},"\u002Fautomating-reporting-workflows\u002Ferror-handling-and-logging-in-excel-automation","Error Handling and Logging in Excel Automation","guide",1790972067841]