I am trying to make a reusable functions in python that would read two Excel files and save save to another.
My function looks like this:
def excel_reader(df1, file1, sheet1, df2, file2, sheet2):
df1 = pd.read_excel(file1, sheet1)
df2 = pd.read_excel(file2, sheet2)
def save_to_excel(df1, filename1, sheet1, df2, filename2, sheet2):
df1.to_excel(filename1, sheet1)
df2.to_excel(filename2, sheet2)
I am calling to the functions as:
excel_reader(df1, 'some_file1.xlsx', 'sheet_name1',
df2, 'some_file2.xlsx', 'sheet_name2')
save_to_excel(df1, 'some_file1.xlsx', 'sheet_name1',
df2, 'some_file2.xlsx', 'sheet_name2')
It do not have any errors but it do not create the Excel files that should be performed by save_to_excel function.
It reads the function parameters until df2 parameter and returns error for the last two.
I will be using pd.read_excel() quite a number of times in my code so I am trying to make it a function. I am also aware the the read_excel() reads the filenames as string and tried doing `somefile.xlsx' but still the same result.
The Excel files that will be read are on the same path of the python script.
Question: Any advice on how this would work? Is it advisable to make this a function or should I just use read_excel() repetitively?