Question

I'm running into an issue saving an excel file using openpyxl using the IronPython runtime engine embedded in a .NET 4.0 application, however when running the same code within the IronPython interpreter, I don't receive any errors and the save is successful. Literally, the code is as simple as this:

import sys
sys.path.append(r'c:\python27\lib\site-packages')
import openpyxl
wb=openpyxl.Workbook()
wb.save(r'c:\save\to\somewhere.xlsx')

when running this code within the .NET app, I get the following stack trace:

an error occurred saving to "c:\_kevin\test.xlsx": Traceback (most recent call last):
    File "C:\path\to\script\file.py", line 582, in __write_logs_to_excel
    wb.save(outfile)
  File "C:\Python27\Lib\site-packages\openpyxl\workbook.py", line 265, in save
    save_workbook(self, filename)
  File "C:\Python27\Lib\site-packages\openpyxl\writer\excel.py", line 187, in save_workbook
    writer.save(filename)
  File "C:\Python27\Lib\site-packages\openpyxl\writer\excel.py", line 170, in save
    self.write_data(archive)
  File "C:\Python27\Lib\site-packages\openpyxl\writer\excel.py", line 76, in write_data
    shared_string_table = self._write_string_table(archive)
  File "C:\Python27\Lib\site-packages\openpyxl\writer\excel.py", line 105, in _write_string_table
    archive.writestr(ARC_SHARED_STRINGS,
  File "C:\Python27\Lib\site-packages\openpyxl\writer\strings.py", line 47, in write_string_table
    start_tag(doc, 'sst', {'xmlns':
  File "C:\Python27\Lib\site-packages\openpyxl\shared\xmltools.py", line 172, in start_tag
    doc.startElementNS((namespace, name), name, attr2)
  File "C:\Python27\Lib\xml\sax\saxutils.py", line 165, in startElementNS
    def startElementNS(self, name, qname, attrs):
  File "C:\Python27\Lib\xml\sax\saxutils.py", line 102, in write
    def write(self, s):
TypeError: expected long, got NoneType

I'm initializing the python engine with the following code:

_pythonEngine = Python.CreateEngine(engineDict);
_memStream = new System.IO.MemoryStream();
_streamWriter = new util.EventRaisingStreamWriter(_memStream);               
_pythonEngine.Runtime.IO.SetErrorOutput(_memStream, _streamWriter);
_pythonEngine.Runtime.IO.SetOutput(_memStream, _streamWriter);

_streamwriter is a wrapper to send output on events to a textbox.

Why would I be able to save without any issue from the interpreter and not with the engine? I've tried not redirecting the output streams and same error occurred.

  • IronPython version = 2.7.0.40 (file version 2.7.4.1000)
  • openpyxl version = 1.8.5
  • python version = 2.7.6

Thanks.

Was it helpful?

Solution

If looks like you're using part of the CPython stdlib:

File "C:\Python27\Lib\xml\sax\saxutils.py", line 165, in startElementNS
    def startElementNS(self, name, qname, attrs):
  File "C:\Python27\Lib\xml\sax\saxutils.py", line 102, in write
    def write(self, s):
TypeError: expected long, got NoneType

IronPython's stdlib is slightly different. When running under the interpreter it's probably picking up the IronPython stdlib, but in your program it's picking up the CPython lib.

When embedding, you can use engine.SetSearchPaths to control the search path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top