17

I am trying to use xlwt to create a output file (in .xlsx format) with multiple tabs. The version number of my Python is 2.7, and I am using Aptana Studio 3 as IDE.

I've used the xlwt package before, with the same environment, to carry out the same task. It worked well. But this time, it works well at first, then suddenly, the output file became faulty that cannot be opened by MS Excel.

Here is a clue that might be helpful. My Aptana Studio 3 decide to open .xlsx in its own editor rather than start MS Excel. Although this happens before the problem, I am wondering if it is related.

The file looks normal when it is opened in Aptana3, but when I closed it, and open it with MS Excel, an error pops up:"Excel cannot open the file "output.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."

May I know how I can get over this? Any suggestions are welcome. Thanks.

ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • Are you saying that the `.xlsx` files work fine the first time you open them but later become corrupt? If so, Python and `xlwt` can't have anything to do with the problem. – senshin Feb 07 '14 at 15:27
  • @senshin I am not sure what is going on. The .xlsx file worked fine when I open it with MS Excel and editor of Aptana 3. But after few runs of my application, the file can only be open with Aptana 3, and it looks normal. But, if I open it with MS Excel, an error window pops up and says that "Excel cannot open the file "output.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." – ChangeMyName Feb 07 '14 at 15:41
  • As far as I know, `xlwt` doesn't support `.xlsx` files (yet?), only `.xls`? – Steven Feb 07 '14 at 15:49
  • @Steven I think `xlwt` supports `.xlsx`. It has compatible interfaces to add tabs, set cell syles, etc. And I also used `xlwt` to create a `.xlsx` file successfully before. – ChangeMyName Feb 07 '14 at 16:09
  • @ChangeMyName: you can create excel files, yes, but only save them in MS Excel 97/2000/XP/2003 compatible .xls files, not the more recent .xlsx format. (try renaming your problem file to .xls to see if that is actually what is happening) – Steven Feb 07 '14 at 16:16

1 Answers1

26

The xlwt module cannot create .xlsx files. It is a writer for .xls files.

The warning is due to a feature in newer versions of Excel called "extension hardening" which means that the file extension has to match the file type.

If you change the output file extension in your program to .xls the warning should go away and Excel will read the file.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108