2

I am using xlrd in appengine. I use flask

I cant read the input file and it keeps on showing the same error message

the code is

def read_rows(inputfile):
    rows = []
    wb = xlrd.open_workbook(inputfile)
    sh = wb.sheet_by_index(0)
    for rownum in range(sh.nrows):
        rows.append(sh.row_values(rownum))
    return rows

@app.route('/process_input/',methods=['POST','GET'])
def process_input():
  inputfile = request.files['file']
  rows=read_rows(request.files['file'])
  payload = json.dumps(dict(rows=rows))
  return payload

I realize that this might be caused by not uploading and saving it as a file. Any workaround on this? This would help many others as well. Any help is appreciated, thx

Update: Found a solution that I posted below. For those confused with using xlrd can refer to the open source project repo I posted. The key is passing the content of the file instead of the filename

EmFeld
  • 227
  • 4
  • 14

3 Answers3

6

Find a solution finally

here's how I do it. Instead of saving the file, I read the content of the file and let xlrd reads it

def read_rows(inputfile):
  rows = []
  wb = xlrd.open_workbook(file_contents=inputfile.read())
  sh = wb.sheet_by_index(0)
  for rownum in range(sh.nrows):
    rows.append(sh.row_values(rownum))
  return rows

worked nicely and turned the excel files into JSON-able formats. If you want to output the json simply use json.dumps().

full code example can be found at https://github.com/cjhendrix/HXLator/blob/master/gae/main.py and it features full implementation of the xlrd and how to work with the data.

Thx for the pointers

EmFeld
  • 227
  • 4
  • 14
3

Use:

wb = xlrd.open_workbook(file_contents=inputfile)

The way you are invoking open_workbook expects what you're passing in to be a filename, not a Flask FileStorage object wrapping the actual file.

Wooble
  • 87,717
  • 12
  • 108
  • 131
  • this really helped me. I need to get the content instead of the filename. Thus came inputfile.read(). Thx a lot – EmFeld Jun 11 '12 at 06:41
0

Judge from your traceback.

File "/Users/fauzanerichemmerling/Desktop/GAEHxl/gae/lib/xlrd/init.py", line 941, in biff2_8_load
    f = open(filename, open_mode)

You can try changing this line to :

f = filename
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
WooParadog
  • 645
  • 4
  • 7
  • 2
    that code is in a third-party library, and just arbitrarily changing it to accept a file instead of the filename seems likely to break things horribly. – Wooble May 06 '12 at 18:30