Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
12159 Lượt xem

I have a wizard with a binary field where I upload a file and a method that opens it. 

excel_file_for_import = fields.Binary(string='import file')
file_name_for_import = fields.Char(string='file name')

@api.multi
def open_file(self):
    book = open_workbook(base64.decodestring(self.excel_file_for_import))
    ...

And the 1st line in the method throws an error:

TypeError: file() argument 1 must be encoded string without NULL bytes, not str

So what I'm doing wrong?

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

Figured it out.

inputx = StringIO.StringIO()
inputx.write(base64.decodestring(self.excel_file_for_import))
book = open_workbook(file_contents=inputx.getvalue())




Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

hello

try like below code

import xlrd
import tempfile
import binascii 
def method_name(self):
        fp = tempfile.NamedTemporaryFile(suffix=".xlsx")
        fp.write(binascii.a2b_base64(self.xls_file)) # self.xls_file is your binary field
        fp.seek(0)

        workbook = xlrd.open_workbook(fp.name)
        sheet = workbook.sheet_by_index(0)

        for row_no in range(sheet.nrows):
            if row_no <= 0:
                fields = map(lambda row:row.value.encode('utf-8'), sheet.row(row_no))
            else:
                line = (map(lambda row:isinstance(row.value, unicode) and row.value.encode('utf-8') or str(row.value), sheet.row(row_no)))
                # in above line variable, you get the line by line value of excel file rows.
Ảnh đại diện
Huỷ bỏ
Tác giả

figured it out. used StringIO()

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 11 18
6363
3
thg 6 17
9465
4
thg 12 23
18985
2
thg 7 25
4325
2
thg 12 24
7556