Search code examples
pythonpython-3.xexcelpandaspandas.excelwriter

How to save excel sheet to the original workbook?


I have a function like this:

def DuplicateEachRow():
        import pandas as pd
        import pathlib
        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc

        df = pd.read_excel(full_path, header=None, sheet_name='Sheet3')
        print(df)

        # duplicate the rows:
        dup_df = pd.concat([df, df], ignore_index=True)

        # using openpyxl
        with pd.ExcelWriter(new_loc) as writer:
            dup_df.to_excel(writer)

and I need to keep this same functionality, but instead of writing that one sheet to a new file. I need to edit that one particular sheet and save it back to my workbook that has other sheets.

EDIT (more explanation): I have 4 sheets in a workbook and in just one sheet (Sheet3) I need to use the functionality above and then save it back to a workbook.

This doesn't work either, specifying the sheet name when I save:

def DuplicateEachRow():
        import pandas as pd
        import pathlib
        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc

        df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')
        print(df)

        # duplicate the rows:
        dup_df = pd.concat([df, df], ignore_index=True)

        # using openpyxl
        with pd.ExcelWriter(new_loc) as writer:
            dup_df.to_excel(writer, sheet_name='GTL | GWL Disclosures')


Solution

  • To add a news sheet in the same excel you have to open the file in mode append. Have a look at the code below:

    def DuplicateEachRow():
        import pandas as pd
        import pathlib
        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc
    
        df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')
        print(df)
    
        # duplicate the rows:
        # keep the index, so you can sort the rows after
        dup_df = pd.concat([df, df])
        #sort the rows by the index so you have the duplicate one just after the initial one
        dup_df.sort_index(inplace=True)
    
        # using openpyxl
        #open the file in append mode 
        with pd.ExcelWriter(new_loc, mode='a') as writer:
            #use a new name for the new sheet
            #don't save the header (dataframe columns names) and index (dataframe row names) in the new sheet  
            dup_df.to_excel(writer, sheet_name='Sheet3', header=None, index=None)