Excel won't open a modifed workbook

1,196 views
Skip to first unread message

jjjjjjj

unread,
Mar 10, 2016, 6:52:39 AM3/10/16
to openpyxl-users

I have a problem and I really don't know how to solve it.

So, I have a workbook created in excel. I am trying to do some very basic tasks:

1. load the workbook
2. add some data
3. save the modified workbook.

Everything goes great and the data is successfully added.
However, when I try to open the modified file with excel, a message appears:

"Excel can not open this file
The file might have been damaged or modified from its original format."

What's more the file won't open in any other program (libreoffice for example).
I am using python 3.5 and openpyxl 2.3.2

Here is a sample code:

from openpyxl import *
 ws = wb.active
ws['A3'] = 'aaa'

in: print(ws['A3'].value)
out: aaa

 wb.save('cos.xlsx')


I'd really appreciate some help.




Thomas Nygårds

unread,
Mar 10, 2016, 7:15:52 AM3/10/16
to openpyxl-users
Please post a minimal working example.
Your code below cannot run...

from openpyxl import *
ws
= wb.active
ws
['A3'] = 'aaa'

print(ws['A3'].value)

wb
.save('cos.xlsx')

jjjjjjj

unread,
Mar 10, 2016, 8:21:41 AM3/10/16
to openpyxl-users
Oops sorry, frustration got better of me.

Here is the minimal working example:

from openpyxl import *
wb
= load_workbook('test.xlsx')

ws
= wb.active
ws
['A3'] = 'aaa'
print(ws['A3'].value)

wb
.save('tt.xlsx')

The file "test.xlsx" and "tt.xlsx" are attached below.

Oh, and thank you very much for replying so fast!
test.xlsx
tt.xlsx

Charlie Clark

unread,
Mar 10, 2016, 8:49:15 AM3/10/16
to openpyx...@googlegroups.com
Am .03.2016, 14:21 Uhr, schrieb jjjjjjj <jasw...@gmail.com>:

> Oops sorry, frustration got better of me.

I have no problem opening "tt.xlsx" in Excel 2011 for Mac. Which version
of Excel are you using? It may be worth noting that Excel 2007 is no
longer supported.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

jjjjjjj

unread,
Mar 10, 2016, 1:26:55 PM3/10/16
to openpyxl-users
Uhm, okay this seems to be the problem. I was using Excel 2007.
After reading your reply I've downloaded a newer version of LibreOffice and it opens the file, so I suppose a newer version of Excel will too.
Thanks for your help and sorry for such a silly question.

Jan

Charlie Clark

unread,
Mar 10, 2016, 1:37:37 PM3/10/16
to openpyx...@googlegroups.com
Am .03.2016, 19:26 Uhr, schrieb jjjjjjj <jasw...@gmail.com>:

> Uhm, okay this seems to be the problem. I was using Excel 2007.
> After reading your reply I've downloaded a newer version of LibreOffice
> and it opens the file, so I suppose a newer version of Excel will too.

> Thanks for your help and sorry for such a silly question.

Glad to know it's working. It's not such a silly question. The important
thing to know is that Excel 2007 was released before the standard was
finalised. As it's also not been supported by Microsoft for many years
(because it was a rush job), there's little incentive to add workarounds
to maintain compatibility.

Thomas Nygårds

unread,
Mar 11, 2016, 3:10:35 AM3/11/16
to openpyxl-users
I have tested your script in
Python 2.7.1 and 2.7.9 with openpyxl 2.3.1
I get is this output (?)
...site-packages\openpyxl\reader\worksheet.py:320: UserWarning: Unknown extension is not supported and will be removed
  warn
(msg)
aaa

but the file opens in my Excel 2007 without error and the cell A3 contains aaa

Regards
Thomas

Charlie Clark

unread,
Mar 11, 2016, 3:20:54 AM3/11/16
to openpyx...@googlegroups.com
Am .03.2016, 09:10 Uhr, schrieb Thomas Nygårds <thomas...@gmail.com>:

> but the file opens in my Excel 2007 without error and the cell A3
> contains

Thanks for checking this. I suspect that might be related to whether lxml
is installed or not. Some applications take exception to the way some of
the namespaces are handled.

In any case, it's important to note that there will be times when Excel
2007 will complain and we won't be doing anything to fix this. Note this
applies solely to files created or edited by openpyxl. There shouldn't be
any problems reading Excel 2007 files.

At some point I'd like to add support for the official purl namespace so
that we can handle the strict implementation of the specification. Though
I'm not sure how this will play with some of the extensions that Microsoft
has added since 2006, some of which I'd quite like to support. I'm also
not sure how much is to be gained by it: it's already okay in the
transitional spec to store datetime values using the "d" cell type. But
only MS Excel can actually read them and you still have to format them.

jjjjjjj

unread,
Mar 11, 2016, 3:53:12 AM3/11/16
to openpyxl-users
Thomas thanks for your contribution.
I've seen this warning before and I thought it was due to the fact, that the file was created using Excel 2013 but then I made some changes using Excel 2007.
(Is this possible?)
However, it wouldn't show up in python 3 and since the file still won't open I didn't pay much attention to it.


It's not such a silly question.

What I meant was, that I should have tried to open the file using more recent software, or have a more careful look at the documentation :)

Charlie Clark

unread,
Mar 11, 2016, 4:17:20 AM3/11/16
to openpyx...@googlegroups.com
Am .03.2016, 09:53 Uhr, schrieb jjjjjjj <jasw...@gmail.com>:

> Thomas thanks for your contribution.
> I've seen this warning before and I thought it was due to the fact, that
> the file was created using Excel 2013 but then I made some changes using
> Excel 2007.

> (Is this possible?)

I'd be surprised in Excel 2007 can open a file that was created in Excel
2013 but there are all these compatibility settings. For simple things it
might not matter. Excel 2013 is more likely to be kind when editing an
Excel 2007 file and maintain compatibility. openpyxl strips out the
compatibility shims, which is what you seen in the warnings, partly
because we haven't got round to a sensible way of handling the extensions
to the format and partly because they generally contain nothing really
useful.

For example, your worksheet file contains:

<extLst>
<ext
xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main"
uri="http://schemas.microsoft.com/office/mac/excel/2008/main">
<mx:PLV Mode="0" OnePage="0" WScale="0"/>
</ext>
</extLst>

Difficult to see what purpose this might usefully serve.

It wouldn't be so bad if the error reports from Excel were more specific.
However, instead of raising validation errors – after all what's the point
a of schema otherwise? – they say that this bit that bit of the file
couldn't be read. Even this is often inaccurate. :-(

Thomas Nygårds

unread,
Mar 11, 2016, 6:59:46 AM3/11/16
to openpyxl-users
Thanks for checking this. I suspect that might be related to whether lxml  
is installed or not. Some applications take exception to the way some of  
the namespaces are handled.

FYI I have not got lxml installed.

I'd be surprised in Excel 2007 can open a file that was created in Excel  
2013 but there are all these compatibility settings.
 
I can usually open files created on Excel 2013 with Excel 2007. I do not think I have experienced any problems there (yet).



On Friday, March 11, 2016 at 9:20:54 AM UTC+1, Charlie Clark wrote:

Ted Vojnovich

unread,
Mar 14, 2016, 4:17:15 PM3/14/16
to openpyxl-users
so most of the office stuff saves the file in the original formal (open 2007  file in 2013, it will save as 2007 unless you tell it differently)

The workbook open problem I had was that in trying to play around with column width, I had a type, so python stopped...that left the file corrupted
Had to rebuild my test file (<<5 min).  Otherwise things work here.
Reply all
Reply to author
Forward
0 new messages