Python code to copy and update excel formulas dynamically

Question:

Target: I am trying to split an excel file into multiple files based on some filter given within the sheet.

Problem: An issue is arising while copying the formula columns as it is not updating the row numbers inside the formula while splitting them into multiple sheets.
For Ex: In the master file, the formula is "=LEFT(B11, FIND(" ", B11,1))" for row 11, however, this becomes the first row in the new split file but the formula is still referring to row 11 which gives "#VALUE" error in the new file.
master file

splitted file

Any ideas on how to resolve this one?

I have tried achieving this using pandas and openpyxl and failed, PFB the code.

To Load the file

wb = load_workbook(filepath)
sheets = wb.get_sheet_names()
sheet_name = wb[sheets[0]]
master_df = pd.DataFrame(sheet_name.values, index=False)
master_df.columns = master_df.iloc[0]
master_df = master_df[1:]
print(master_df)

To split amd export the file

temp_df = master_df[master_df['Filter Column'] == filter_criteria]
sp.export_file(temp_df, output_path + "/" + <"output file name">)
Asked By: Ghatothkachh

||

Answers:

def update_formula(df: pd.DataFrame, formula_col):
    '''
    Function to update formulas for each Manager
    :param df:  DataFrame for one specific manager.
    '''
    for _col in formula_col:
        col_alpha = formula_col[_col][0]
        formula = formula_col[_col][1]
        index = 2
        for ind, row in df.iterrows():
            df.at[ind, _col] = Translator(formula, origin=col_alpha + '2').translate_formula(col_alpha + str(index))
            index = index + 1

Here I am giving DataFrame and a list of columns which have formula in them as input. Later I am iterating over DataFrame and updating formula for each cell in that column using OpenpyXl Translator method.

This is the best solution I have figured yet.
Please let me know if there is a better way.

Answered By: Ghatothkachh