How do you read excel files with xlrd on Appengine

前端 未结 3 1315
旧时难觅i
旧时难觅i 2021-02-06 19:23

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(inp         


        
相关标签:
3条回答
  • 2021-02-06 19:40

    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.

    0 讨论(0)
  • 2021-02-06 19:44

    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

    0 讨论(0)
  • 2021-02-06 19:54

    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
    
    0 讨论(0)
提交回复
热议问题