This question has been flagged
1 Reply
21443 Views

hi,

Let say I have uploaded a file abc.xls on binary field in the form view. I want to read the content of this file on a button click through python. Is there any way to do this?

Avatar
Discard
Best Answer

It's possible, but you need to know what file you will be reading to know what libs to use. You'll need to decode it from base64 and probably save it into a StringIO.

You can make a method that you then execute on button click and it reads the file from field. 

from xlrd import open_workbook
import base64
import StringIO

class ExcelImport(models.TransientModel): _name = 'excell.import.wizard' excel_file_for_import = fields.Binary(string='File for upload') def import_data_form_file(self): try: inputx = StringIO.StringIO() inputx.write(base64.decodestring(self.excel_file_for_import)) book = open_workbook(file_contents=inputx.getvalue()) except TypeError as e: raise ValidationError(u'ERROR: {}'.format(e)) sheet = book.sheets()[0] for i in range(sheet.nrows): if i == 0: continue if i == 1: name = sheet.cell(i, 0).value vat = sheet.cell(i, 5).value



 

Avatar
Discard
Author

It worked. Thanks brother that was a great help

No problem... when I wrote the answer I was programming this, so it was just copy&paste from my code.

Author

Great. Thanks for the help. Another question: is it possible that we can know the physical address of the file on computer, the file which is attached in the binary field. I want to read the file through pandas but it is not working on field name or something like that.

There's no physical location. The file is saved in the database. I never used pandas, but you probably need to get the file in binary and decode it from base64.

Author

OK. Great

super, thanks !

in addition, for python3, need to replace StringIO by io.StringIO