2

I am trying to use the XLRD library in IronPython 2.7

At the most basic operation of opening an .xls file (2003 format) I get the following error, and I am not sure how to fix it:

  workbook = xlrd.open_workbook(xlsfile)

  File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\xlrd\__init__.py", line    426, in open_workbook
  TypeError: sequence item 0: expected bytes or byte array, str found

any ideas? i would like to use xlrd if possible but seems like there may be some compatibility problems. When using Python 2.7 interpreter the file opens no problem ..

Roger Sanchez
  • 311
  • 1
  • 4
  • 13

3 Answers3

3

I fought with this for some time in xlrd 0.9.2. Neither bytes, bytearray, str, or even reading the file manually and passing it as file_contents worked.

Finally I downgraded xlrd to version 0.8.0 and... it's working. :)

David Grant
  • 3,447
  • 4
  • 29
  • 33
  • 1
    tried xlrd 0.9.3 both with XLSX & XLS (2003) and the problem persists - even by manually correcting code, it continues to give errors "like expected str, got bytes" in other places deep in the code - for me downgrading to 0.8 didn't help - seems like a clear incompatibility with IronPython – hello_earth Oct 27 '14 at 14:03
  • 1
    for those seeking for alternatives - using old Interop (because IronPython has this advantage) worked for me without any additional python code (of course there might be disadvantages to it too) http://www.ironpython.info/index.php?title=Interacting_with_Excel – hello_earth Oct 27 '14 at 14:23
1

For whatever reason it seems that open_workbook requires the filename to be in bytes, not as a string. Try:

workbook = xlrd.open_workbook(bytes(xlsfile))

That's an issue you're likely to encounter a lot: IronPython's strings are Unicode by default (like Python 3) and not byte strings like Python 2.

Jeff Hardy
  • 7,632
  • 24
  • 24
  • Thanks - when i tried that I got the error below, which leads me to believe the problem is somewhere else. The strange thing is that xlrd homepage says it is 'unicode aware' and that it is compatible with Python 3 .. File "C:\Program Files (x86)\IronPython 2.7\lib\site-packages\xlrd\__init__.py", line 394, in open_workbook TypeError: expected str, got bytes – Roger Sanchez May 16 '13 at 00:00
  • Even if xlrd supports Python 3, it might not activate that path for IronPython. However, the fact that it does should make any changes that might be needed easier. – Jeff Hardy May 16 '13 at 16:47
-1

I am trying to add xlrd to ironpython 2.7 in CODESYS and run into same problem. However, after trying version 0.8.0 it works fine.

Maluvel
  • 425
  • 1
  • 6
  • 13