Does anyone know why the below code works if the file is csv or xls but not for xlsx?
with open("file", 'w') as ft:
for row in auto_rep[answer][:-1]:
if row != "":
ft.write(row.replace(";",",")+"\n")
ft.write(auto_rep[answer][-1].replace(";",","))
ft.close()
I would like the file to be xlsx. I’m not sure if this is supported though.
Ex: file is either
- filename.csv
- filename.xls
- filename.xlsx
I am also using the following code first to copy an xlsx file so I don’t mess with the original file.
import shutil
orig = r'C:\filepath\original.xlsx‘
targ = r'C:\filepath\file(.csv, .xls, or .xlsx)'
shutil.copyfile(orig, targ)
If the file is csv, it creates the file and the information populated to the first sheet is correct. I know csv doesn’t use multi sheets so I understand it wouldn’t load the others anyways.
If the file is xls, it creates the file but I still receive the following message:
“The file format and extension of ‘filename.xls’ don’t match. The file could be corrupted or unsafe. Unless you trust it’s source, don’t open it. Do you want to open it anyway?” … and I can click yes and it loads but only the first sheet populates.
If the file is xlsx, It creates the file but I get an error that states:
“Excel cannot open the file ‘filename.xlsx’ because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.”