简体   繁体   中英

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.

Before I dig further, I wanted to ask the community: 1. Has anyone managed to use openpyxl under IronPyth 2. Are there any other libraries that work and that can create moderately formatted xlsx files?

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.

We have successfully used ClosedXML - a reasonably active and supported project - for reading and writing formatted XLSX data from .NET. Our primary use-case is centered around C# but on occasion we have used it from IronPython as well.

The project has NuGet packages and builds on Microsoft's official DocumentFormat.OpenXml SDK while being significantly easier to use.

I cannot comment on openpyxl as I haven't used it but the bug report you found and the general situation with Ctypes in IronPython makes it very likely that this combination of technologies will not work out (right now).

尝试使用 clr 模块并参考 microsoft Excel "import clr" clr.AddReferenceByName('Microsoft.Office.Interop.Excel')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM