'Is it possible to delete old records from an excel spreadsheet after exporting dataframe to excel?

I have a dataframe that I have successfully exported into excel, which writes over the data in a specific sheet from columns A to G. My question is, sometimes there is rows left over from the old report that I would like to delete, for example say that today the report exported 50 new rows of data, yet the sheet has 55 rows of data. Once I run the script the first 50 will be replaced no problem, yet the remaining 5 from the previous day remain. How could I go about removing these 5 or "n" number of rows from columns A to G (since there is functions and other data in columns H - onward) after exporting the dataframe. Any help or documentation would really help. Thank you.

import pandas as pd
import openpyxl
from openpyxl import load_workbook
import string

def abn_report(file_name):
    #file_name should have the following stucture ABN_data.xls, it MUST be an .xls file 
    df = pd.read_excel(file_name,skiprows = 1)
    df1 = df.drop(df.columns[[1,4,5,7,8,9,12,13,15,17]], axis = 1)


    #Filter Visit
    indexes = df1[(df1.loc[:,'Visit Status'] == 'Cancel') | (df1.loc[:,'Visit Status'] == 'Pre-Reg') | (df1.loc[:,'Visit Status'] == 'History')].index 
    df1.drop(indexes,inplace=True)

    #Filter MD Check
    indexes1 = df1[(df1.loc[:,'Med Chk Cd'] == 'P') | (df1.loc[:,'Med Chk Cd'] == 'A') | (df1.loc[:,'Med Chk Cd'] == 'L') | (df1.loc[:,'Med Chk Cd'] == 'Q')].index 
    df1.drop(indexes1,inplace=True)

    #Filter Admt Svc
    exclude_words = ['UROLOGY']

    indexes2 = df1[(df1.loc[:,'Admit Service'].str.contains("CARDIOLOGY CONSUL")) |(df1.loc[:,'Admit Service'].str.contains("CARE LINK")) |(df1.loc[:,'Admit Service'].str.contains("DOVE WOMEN")) | (df1.loc[:,'Admit Service'].str.contains("RESPIRATORY THERAPY")) | (df1.loc[:,'Admit Service'].str.contains("VALLEY CARDIOLO")) |(df1.loc[:,'Admit Service'] == 'OUTPATIENT REHAB') |(df1.loc[:,'Admit Service'].str.contains('OUTPATIENT INFUSION'))| (df1.loc[:,'Admit Service'].str.contains("RESPIRATORY THERAPY")) |(df1.loc[:,'Admit Service'].str.contains('RENAL')) | (df1.loc[:,'Admit Service'].replace(exclude_words,'',regex=True).str.contains("CLINIC")) | (df1.loc[:,'Admit Service'] == ("RADIOLOGY (O)"))].index
    df1.drop(indexes2, inplace=True)

    #Drop Med Chk Column
    df2 = df1.drop(df1.columns[7],axis=1)


    #Rewrites the data into the formatted pivot table
    book = load_workbook('test_3.xlsx')
    writer = pd.ExcelWriter('test_3.xlsx', engine='openpyxl') 
    writer.book = book

    writer.sheets = dict((ws.title, ws) for ws in book.worksheets)

    df2.to_excel(writer, "data",index=False)

    writer.save()
    return


abn_report('PCA_Status_ID_Report.xls')


Solution 1:[1]

Can you just create a new file and add the old tabs to that file? So don't overwrite anything, just make a new file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dan