在萤火虫中保存与openpyxl的工作簿

我遇到一个问题,使用embedded在.NET 4.0应用程序中的IronPython运行时引擎,使用openpyxl保存excel文件,但是在IronPython解释器中运行相同的代码时,我没有收到任何错误,保存成功。 从字面上看,代码就像这样简单:

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

当在.NET应用程序中运行此代码时,我得到以下堆栈跟踪:

保存为“c:\ _ kevin \ test.xlsx”时发生错误:Traceback(最近的最后一次调用):
    在__write_logs_to_excel文件“C:\ path \ to \ script \ file.py”,第582行
     wb.save(OUTFILE)
  文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ workbook.py”265行保存
     save_workbook(self,filename)
  在save_workbook文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ writer \ excel.py”,第187行
     writer.save(文件名)
  文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ writer \ excel.py”,保存在第170行
     self.write_data(存档)
  文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ writer \ excel.py”,第76行,在write_data
     shared_string_table = self._write_string_table(存档)
  在_write_string_table文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ writer \ excel.py”,第105行
     archive.writestr(ARC_SHARED_STRINGS,
  在write_string_table文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ writer \ strings.py”,第47行
     start_tag(doc,'sst',{'xmlns':
  在start_tag文件“C:\ Python27 \ Lib \ site-packages \ openpyxl \ shared \ xmltools.py”,第172行
     doc.startElementNS((名称空间,名称),名称,attr2)
   startElementNS文件“C:\ Python27 \ Lib \ xml \ sax \ saxutils.py”,第165行
     def startElementNS(self,name,qname,attrs):
  写入文件“C:\ Python27 \ Lib \ xml \ sax \ saxutils.py”,第102行
     def write(self,s):
 TypeError:预计长,得到NoneType

我用下面的代码初始化python引擎:

 _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是将事件输出发送到文本框的包装器。

为什么我可以在没有任何翻译问题的情况下保存,而不用引擎? 我试过不redirect输出stream,发生相同的错误。

  • IronPython版本= 2.7.0.40(文件版本2.7.4.1000)
  • openpyxl版本= 1.8.5
  • python版本= 2.7.6

谢谢。

如果看起来你正在使用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的stdlib稍有不同。 当在解释器下运行时,它可能会拾取IronPython stdlib,但是在你的程序中,它将拾取CPython库。

embedded时,可以使用engine.SetSearchPaths来控制searchpath。

    Interesting Posts