Search code examples
pythonexcelironpythonopenpyxl

Writing xlsx files in Iron Python


I am trying to create an xlsx file in Iron Python (v2.7.5).
I have installed the latest version of openpyxl (v2.2.1) and tested a minimalistic example, almost literally taken from openpyxl documentation:

from openpyxl import Workbook
wb = Workbook()
wb.save('empty.xlsx')

I is working as expected in CPython (creating an empty workbook), but in Iron Python it is just throwing exceptions.

File "test_openpyxl.py", line 15, in <module>
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\workbook\workbook.py", line 298, in save
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 196, in save_workbook
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 179, in save
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\writer\excel.py", line 67, in write_data
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\workbook\properties.py", line 103, in write_properties
File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\openpyxl\utils\datetime.py", line 33, in datetime_to_W3CDTF

TypeError: expected datetime, got Object_1$1

I guess the error is caused by some .NET object popping unexpectedly, but I am confused by Object_1 thing, so could not find what object it was.

I have also tried older versions of the packages, and only with v1.8.0 I was able to get some results:

  • openpyxl-2.1.0 runs without exceptions (only with a deprecation warning), but Excel complains on corrupt content in the created file.enter code here
  • openpyxl-2.0.5 throws another exception (already reported here)
  • openpyxl-1.8.6 throws "ImportError: cannot import PatternFill from openpyxl.styles"
  • openpyxl-1.8.0 prints out a warning ("UserWarning: Unable to import 'xml.etree.cElementree'. Falling back on 'xml.etree.Elementree'"), which is after some thought is quite reasonable. So this is probably the only version that works.

Is it possible to use openpyxl under IronPyth?


Solution

  • I think the short answer is that you can't use openpyxl with IronPython. We use descriptors for typing and IronPython seems to struggle with them.