there is an option in pandas to keep the format of the file, when I use df.to_excel to save the data on the file?
The only workaround that i found is:
from openpyxl import load_workbook
import pandas as pd
# df_data is a pd.DataFrame
wb = load_workbook(fout_file)
sheet = wb.active
for r, row in enumerate(dataframe_to_rows(df_data, index=False, header=False), 2):
for c in range(0, df_columns):
sheet.cell(row=r, column=c + 1).value = row[c]
wb.save(fout_file)
There a better way where i don't must copy cell by cell?
Thanks
stefano G.
@DSteman thanks for the idea, I jus tryed to use StyleForm as you advised me.
def main ():
...
...
...
# df_new_data = pd.DataFrame(columns=self.df_values.columns)
df_new_data = StyleFrame.read_excel(self.template_fout, read_style=True)
...
...
...
cr_dfnewdata = 0
for j, row_data in data.iterrows():
original_row = row_data.copy(deep=True)
# df_new_data = df_new_data.append(original_row)
cr_dfnewdata += 1
df_new_data[cr_dfnewdata] = original_row
...
...
...
compensa_row = row_data.copy(deep=True)
compensa_row[self.importo_col] = importo * -1
# compensa_row[self.qta_col] = qta * -1
compensa_row[self.cod_ribal_col] = f"{cod_ribal}-{j}"
# df_new_data = df_new_data.append(compensa_row)
cr_dfnewdata += 1
df_new_data[cr_dfnewdata] = compensa_row
...
...
...
def save_working_data(self, cod_ribalt: str, df_data):
fout_working_name = f"{self.working_dir}/working_{cod_ribalt}.xlsx"
df_data.to_excel(fout_working_name).save()
BUT i got this error:
export_df.index = [row_index.value for row_index in export_df.index] AttributeError: 'int' object has no attribute 'value'
