Using variable from xlrd as input to xwlt

1,392 views
Skip to first unread message

Eric Smith

unread,
Apr 26, 2013, 12:28:24 PM4/26/13
to python...@googlegroups.com
I have a spreadsheet of which I want to transform the layout using xlrd to read and xwlt to write. I am able to assign the values based on the cells but can not write without getting this error when using a variable as input into a new cell.

Let me know if there is an easier way to assign values to variables from an existing document to use as output.


ERROR:

Traceback (most recent call last):
  File "test.py", line 54, in <module>
    row0.write(1, cellb)
  File "/Library/Python/2.7/site-packages/xlwt/Row.py", line 259, in write
    raise Exception("Unexpected data type %r" % type(label))
Exception: Unexpected data type <class 'xlrd.sheet.Cell'>


CODE:

#imports
import xlrd
from xlrd import open_workbook, empty_cell
from tempfile import TemporaryFile
from xlwt import Workbook

#User input to decide what excel document
#print 'What excel file would you like to use to use?'
#excelfile = raw_input("'filename.xls' >> ")

#xlrd open and create book (name of book)
book = open_workbook('test.xls')
sheet = book.sheet_by_index(0)

#Set variables based on excel document
try:
cell1 = sheet.cell(0,0)
except:
cell1 = xlrd.empty_cell
try:
cell2 = sheet.cell(0,1)
except:
cell2 = xlrd.empty_cell
try:
cell3 = sheet.cell(0,2)
except:
cell3 = xlrd.empty_cell
try:
cell4 = sheet.cell(1,1)
except:
cell4 = xlrd.empty_cell

#Assign cell from above to parents value
cella = cell1 
cellb = cell2
cellc = cell3
celld = cell4

#Output to excel document
book_w = Workbook()
sheet1 = book_w.add_sheet('Sheet 1')

#Column Headers
row0 = sheet1.row(0)
row0.write(0,'town')
row0.write(1, cellb) <-----ISSUE

#Sheet Name - Saved as date & time
book_w.save('output.xls')
book_w.save(TemporaryFile())



THANK YOU.

John Yeung

unread,
Apr 26, 2013, 12:37:12 PM4/26/13
to python-excel
xlwt can't use xlrd's cell objects directly.  xlwt needs values.  The value of an xlrd cell is in an attribute named 'value'.  You should be doing something like

row0.write(1, cellb.value)

John Y.

Eric Smith

unread,
Apr 26, 2013, 1:31:57 PM4/26/13
to python...@googlegroups.com
Thank you very much. That worked out. I forgot the obvious things......

Another question I have is, is there way to skip assigning a variable to a cell to use it as output?

Can I use the original sheet and a specific cell  as direct output? Might be faster than assigning a variable to each cell.

For example:

row0.write(1, sheet.cell0,1.value)

John Yeung

unread,
Apr 26, 2013, 2:42:40 PM4/26/13
to python-excel
On Fri, Apr 26, 2013 at 1:31 PM, Eric Smith <eric.j....@gmail.com> wrote:
For example:

row0.write(1, sheet.cell0,1.value)


Yes, just use the parentheses like you did before.

In general, whenever you have

name = expression
f(name)

you can simply do

f(expression)

instead.  Of course, there are sometimes good reasons to use two simpler statements instead of one more complex one.  One obvious reason to assign the value of an expression to a name is to reuse the value without reevaluating the expression.

John Y.

Eric Smith

unread,
Apr 26, 2013, 3:03:07 PM4/26/13
to python...@googlegroups.com
Again... Thank you very much for your time, especially in answering such simple questions.

I appreciate the support from this group.

-
Eric
Reply all
Reply to author
Forward
0 new messages