Corrupt xlsx file on save

513 views
Skip to first unread message

Nmhor Kmhor

unread,
Apr 13, 2022, 2:50:02 PM4/13/22
to openpyxl-users
I have been using openpyxl 3.0.9 from some time.  Yesterday, I open a workbook, set a few cells and save it.  Since yesterday, the saved workbook cannot be opened by Excel.  I have not changed my software or the version of openpyxl.  I don't believe my version of Excel has changed (Microsoft® Excel® for Microsoft 365 MSO (16.0.14326.20850) 64-bit).  The problem occurs with different workbooks including ones that worked before yesterday.  When I look inside the XLSX archive, every file in the openpyxl saved XLSX is omitting a good deal that was in the original.  I created a very simple test case that fails, a workbook with three named ranges.  I open the workbook, set the three named cells to 5, 7, and 9 respectively and then save.  The saved workbook cannot be opened in Excel.  I have attached both.  [N.B. openpyxl is issuing the warning "... \site-packages\openpyxl\comments\shape_writer.py:74: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead." but it has been doing this for some time and still working.]
Test_openpyxl.xlsx
openpyxl_output.xlsx

Charlie Clark

unread,
Apr 14, 2022, 4:38:08 AM4/14/22
to openpyxl-users
The warning is unrelated to any problems, it just comes from somewhat slightly clumsy code related to VML.

It's better to submit a bug report for this because this makes keeping track much easier.

Charlie

--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Sengelsweg 34
Düsseldorf
D- 40489
Tel: +49-203-3925-0390
Mobile: +49-178-782-6226

Nmhor Kmhor

unread,
Apr 14, 2022, 3:54:51 PM4/14/22
to openpyxl-users
Thanks Charlie.  In creating a simple sample to submit a bug, I discovered the cause.  I had keep_vba=True when loading the workbook.  I've had that flag set for some time.  I don't know why it started failing now (on workbooks with no VBA).  For my immediate use case, all of the workbooks I am writing are guaranteed not to have macros so I removed the flag.

Jason Cobble

unread,
Apr 15, 2022, 2:02:44 AM4/15/22
to openpyx...@googlegroups.com
Please remove from group.

On Wed, Apr 13, 2022, 2:50 PM Nmhor Kmhor <nkm...@gmail.com> wrote:
I have been using openpyxl 3.0.9 from some time.  Yesterday, I open a workbook, set a few cells and save it.  Since yesterday, the saved workbook cannot be opened by Excel.  I have not changed my software or the version of openpyxl.  I don't believe my version of Excel has changed (Microsoft® Excel® for Microsoft 365 MSO (16.0.14326.20850) 64-bit).  The problem occurs with different workbooks including ones that worked before yesterday.  When I look inside the XLSX archive, every file in the openpyxl saved XLSX is omitting a good deal that was in the original.  I created a very simple test case that fails, a workbook with three named ranges.  I open the workbook, set the three named cells to 5, 7, and 9 respectively and then save.  The saved workbook cannot be opened in Excel.  I have attached both.  [N.B. openpyxl is issuing the warning "... \site-packages\openpyxl\comments\shape_writer.py:74: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead." but it has been doing this for some time and still working.]

--
You received this message because you are subscribed to the Google Groups "openpyxl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openpyxl-user...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/openpyxl-users/cbb62fe0-e82c-4145-9240-cf4072bee387n%40googlegroups.com.

Charlie Clark

unread,
Apr 16, 2022, 10:05:58 AM4/16/22
to openpyxl-users

On 14 Apr 2022, at 21:54, Nmhor Kmhor wrote:

Thanks Charlie. In creating a simple sample to submit a bug, I discovered

the cause. I had keep_vba=True when loading the workbook. I've had that

flag set for some time. I don't know why it started failing now (on

workbooks with no VBA). For my immediate use case, all of the workbooks I

am writing are guaranteed not to have macros so I removed the flag.

For reasons best known to themselves, Microsoft requires a separate content type for workbooks with macros and this is set by the flag. You must have this and ".xlsm" in the file name otherwise Excel won't be open the file.

Things will change in openpyxl 3.2 which has completely rewritten support for VBA and will let you decide by file extension.

Reply all
Reply to author
Forward
0 new messages