Why are no new sheets created although the code manages to run?

  Kiến thức lập trình
#Load master worksheet to paste the newly splitted fixings
master_file = "output.xlsm"
master_si = load_workbook(master_file, keep_vba=True)

#Load nicer_ws, then read to become data frame !!!
nicer_split = pd.read_excel(old_murex, sheet_name='nicer_WS')
print('Initial nicer_split: ')
print(nicer_split)

# Ensure 'Dates' is a string (or datetime if preferred)
nicer_split['Dates'] = nicer_split['Dates'].astype(str)

# Split the DataFrame into multiple DataFrames based on 'Dates'
dfads = {datete: group for datete, group in nicer_split.groupby('Dates')}

# Access individual DataFrames
for date, dataframe in dfads.items():
    print(f"DataFrame for Dates {date}:")
    print(dataframe)
    print("n")


#writing the new dfs into the excel sheet
with pd.ExcelWriter(master_file, engine='openpyxl', mode='a') as writer:
    for date, dataframe in dfads.items():
        dataframe.to_excel(writer, sheet_name=f'{date}', startcol=2, index=False)


#save it into the master SI file
master_si.save(master_file)
print("Dataframes have been written!")

The dataframe as some dates (sensitive data), i want to split the dataframe into smaller dataframes of each date. I have managed to do this part over here, but when i try to run the bottom part of the code (the with statement there), it manages to run , but no new sheets were made in the excel workbook.

A side problem, when the code runs for the first time after restarting the jupyter kernel, it picks up an empty data frame. But when I go to the excel file and make minor tweaks (eg. change sheet tab colour, adjust column size, etc), the code is able to pick up the excel sheet and do the splitting. Not sure what seems to be the error, thanks in advance!

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT