0

Usually the xlrd library uses pathname/filename to read the file as follows

workbook = xlrd.open_workbook('some_filename.xls')

But in my flask project i am dealing with an uploaded file.So i got the file as

xlsfile = request.files['file']

So how can i use this file object in xlrd.?

nidhinpd
  • 93
  • 1
  • 4
  • 1
    `xlrd.open_workbook(xlsfile.filename)` - is this what you are after? – Marcin Mar 23 '15 at 05:56
  • 1
    iirc, `open_workbook` can take a `file_contents` argument. – KevinOrr Mar 23 '15 at 06:00
  • @marcin Actually that won't work either. >>>>>No such file or directory: u'test.xlsx' – nidhinpd Mar 23 '15 at 06:04
  • 1
    I think you need to provide full path: `xlrd.open_workbook(os.path.join(app.config['UPLOAD_FOLDER'], xlsfile.filename))`. Can you check this? – Marcin Mar 23 '15 at 06:07
  • @Marcin Thanks. Although file_contents arguement worked perfectly. workbook = xlrd.open_workbook(file_contents = xlsfile.read()) Thanks for your quick response – nidhinpd Mar 23 '15 at 06:10
  • @Marcin oh I never realized that Flask saves uploaded files to disk. Interesting – KevinOrr Mar 23 '15 at 06:13

0 Answers0