首页 文章

使用openpyxl在ironpyx中保存工作簿

提问于
浏览
1

我正在使用嵌入在.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应用程序中运行此代码时,我得到以下堆栈跟踪:

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

我正在使用以下代码初始化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是一个将事件的输出发送到文本框的包装器 .

为什么我能够在没有任何问题的情况下从解释器而不是引擎中进行保存?我试过不重定向输出流并发生相同的错误 .

  • IronPython版本= 2.7.0.40(文件版本2.7.4.1000)

  • openpyxl version = 1.8.5

  • python版本= 2.7.6

谢谢 .

1 回答

  • 0

    如果看起来你正在使用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 lib .

    嵌入时,您可以使用 engine.SetSearchPaths 来控制搜索路径 .

相关问题