2

I am writing a dataframe to a range of Excel file in a certain tab, but after saving the file, I see that the Excel file has become unusable. Could anyone suggest a solution?

import openpyxl as pyx
df3_xmax= df3.iloc[0]
wb = pyx.load_workbook(dst)
xl_writer = pd.ExcelWriter(dst, engine='openpyxl')
xl_writer.book = wb
xl_writer.sheets = {ws.title:ws for ws in wb.worksheets}
df3_xmax.to_excel(xl_writer, 'shname', index=False, header=False, startcol=3, startrow=7)

xl_writer.save()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
FabioSpaghetti
  • 790
  • 1
  • 9
  • 35
  • What is wrong with the Excel file? Any specific errors? – Nick Feb 08 '19 at 16:27
  • No nothing wrong, first it opens up without problem but after running this code it says the file does not have the right extension, seems like it missed the .xlsm extension during the save process – FabioSpaghetti Feb 08 '19 at 16:32
  • Should it theoretically work ? – FabioSpaghetti Feb 08 '19 at 19:04
  • It probably saves then as `.xlsx` I am guessing. Can you confirm that? – Nick Feb 08 '19 at 19:08
  • yes, but the original file should be .xlsm, but even if it saves it as .xlsx Excel must be able to open it that in my case is not true – FabioSpaghetti Feb 08 '19 at 19:12
  • https://stackoverflow.com/questions/39292179/how-to-convert-xlsm-macro-enabled-excel-file-to-xlsx-using-python looks like you can't just rename the extension and keep the same functionality. that's probably why you're having errors after it saves. if you post your full code I can try to reproduce it on my end. – Nick Feb 08 '19 at 19:27
  • Dear Nick, please consider any def you think for any range of cells, just simply use two .xlsx and .xlsm files. though what I need is to have the .xlsm file at the end – FabioSpaghetti Feb 08 '19 at 21:52

0 Answers0