Export data to excel using RhinoPython / GHPython

Hi all,

I am working on a quite straightforward python script which is supposed to create a .xls file and write something in it. I run this script in both RhinoPython and GHPython, but got following error:

Message: sequence item 0: expected bytes or byte array, str found

Traceback:
  line 86, in get_biff_record, "C:\Users\XXXXXXX\Downloads\xlwt-1.1.2\xlwt\BIFFRecords.py"
  line 622, in _Workbook__sst_rec, "C:\Users\XXXXXXX\Downloads\xlwt-1.1.2\xlwt\Workbook.py"
  line 696, in save, "C:\Users\XXXXXXX\Downloads\xlwt-1.1.2\xlwt\Workbook.py"
  line 6, in <module>, "C:\Users\XXXXXXX\AppData\Local\Temp\TempScript.py"
  line 660, in get_biff_data, "C:\Users\XXXXXXX\Downloads\xlwt-1.1.2\xlwt\Workbook.py"

Here is my script:

import xlwt

wb = xlwt.Workbook()
sheet = wb.add_sheet("TestSheet")
sheet.write(0,0,'Hello')
wb.save("Test.xls")

The last line wb.save("Test.xls") triggers error. This script runs well in other environment like cmd and Aptana Studio… I wonder is there any problem with settings or installation?

Thanks,
Dou

Hi Dou,

Is XLWT compatible with IronPython?

You might search for another library that can write Excel files and that is compatible with IronPython. Of course, you can easily write a common-delimited file (CSV) with Python - Excel reads these too…

– D

Hi Dale,

XLWT can be successfully imported. Anyway, let me try CSV, seems much easier. Thanks!

Dou

where can i find the method to install a xls library for python

Hello,

Would csv format be sufficient for you? It is very easy to write csv files using the import csv built in library in Ironpython in Rhino. I think the most popular Excel libraries only work in Cpython, not Ironpython.

You can also use clr for a dotNet experience but I have no knowledge of that…

i can’t use csv, my owner list is an excel list, that is update every time…
i would like to read the list, select one line, and copy name, adress and phone number to my layout…

I’ve previously implemented xlrd and xlwt in GHPython. Though this was quite a while back, so I’m not sure if that is still possible. But worth a shot maybe, it was definitely simpler than implementing .NET as I recall (although that might also not be accurate any more):

Hello,

I implemented something like this this weekend on Windows using VBScript to convert the Excel file into csv format in a temporary directory, then reading the csv file with python. I saved this file as XlsToCsv.vbs I am definitely interested in any alternative means of doing this

if WScript.Arguments.Count < 2 Then
    WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
    Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oExcel.DisplayAlerts = False
' activate the second worksheet
oBook.Worksheets(2).Activate 
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit

I call it in python as shown below. Maybe it would also be possible with _RunScript

subprocess.call(r'C:\...\6.0\scripts\dev\XlsToCsv.vbs '
                            + chr(34) + datafile + chr(34) + " " +
                            chr(34) + csvfile + chr(34), 
                            shell=True,
                            )