Writing Pandas dataframe to excel gives corrupted file

Viewed 860

I have a problem writing pandas dataframe to an excel file. When I write from a jupyter notebook it writes perfectly when I run the same code but from within a .py script via the command line, it gives corrupted file.

I tried

df.to_excel("df.xlsx") 

and

options = {}
options["strings_to_formulas"] = False
options["strings_to_urls"] = False
writer = pd.ExcelWriter("df.xlsx", engine = "xlsxwriter", options = options)
df.to_excel(writer, "Sheet1", index = False, header = False)
writer.save()

They work well in the notebook but badly in command line even though they have the same environment.

When I try to read this corrupted file back in the notebook it gives me this error

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'Duration'

"Duration" is the name of the first column in the data frame. I searched for similar questions before asking and tried all mentioned answers I found but they all didn't work.

Thanks

0 Answers
Related