콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
24582 화면

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?

아바타
취소
베스트 답변

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



 

아바타
취소
작성자

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.

작성자

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.

작성자

OK. Great

super, thanks !

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

관련 게시물 답글 화면 활동
1
7월 16
15610
6
9월 22
29327
7
6월 20
21636
2
3월 17
5333
1
3월 22
8188