title: Remove and replace Pandas DataFrame in Excel sheet
body: <p>I am storing a <code>pandas DataFrame</code> in an Excel sheet. When I re-run my code, I want the sheet completely overwritten. This is important because my code writes to the same file a few different times, i.e., loading and saving certain sheets at different moments, not wanting to disturb the sheets not currently being changed. Because of this, if a new iteration of the code produces fewer rows or columns, the old data will still be there. For example, if iteration #1 produces 500 rows but iteration #2 only produces 499, that 500th row will still show up in my Excel file.</p>
  <p>I'm aware I could loop through all the cells and set their values to <code>None</code>, but I thought it would be more efficient to <code>remove</code> a given sheet completely, <code>create_sheet</code> with the same sheet name, and then save my <code>DataFrame</code> to the new sheet. The code below is a MRE of what I'm trying to do. It successfully removes the sheet, creates a new one, and saves the file, but the <code>to_excel</code> doesn't seem to be executing. The resulting Excel file has the 'test' sheet, but it is blank.</p>
  <pre><code>import pandas as pd
  import numpy as np
  import openpyxl
  from openpyxl import load_workbook
  from openpyxl import Workbook
  
  df_data = {'A': np.random.randint(1, 50, 20),
              'B': np.random.randint(1, 50, 20),
              'C': np.random.randint(1, 50, 20),
              'D': np.random.randint(1, 50, 20)}
  
  df = pd.DataFrame(data=df_data)
  
  fn = 'test.xlsx'
  sheet = 'test'
  df.to_excel(fn, sheet_name=sheet)
  
  df2 = pd.read_excel(fn, sheet_name=sheet, index_col=0)
  df2.drop(columns=['A'], inplace=True)
  
  book = load_workbook(fn)
  writer = pd.ExcelWriter(fn, engine='openpyxl')
  writer.book = book
  writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
  s = book[sheet]
  book.remove(s)
  book.create_sheet(sheet, 0)
  
  #THIS CODE WILL ACTUALLY WRITE TO THE SHEET, BUT df2 WILL NOT
  #s2 = book[sheet]
  #s2['A1'] = 'This will write to the sheet'
  
  df2.to_excel(writer, sheet_name=sheet)
  writer.save()
  </code></pre>
  <p>Note that my commented code will write to the proper sheet if uncommented. It seems to just be the <code>to_excel</code> line that doesn't work.</p>