Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
3 Antworten
12197 Ansichten

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?

Avatar
Verwerfen
Autor Beste Antwort

Figured it out.

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




Avatar
Verwerfen
Beste Antwort

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.
Avatar
Verwerfen
Autor

figured it out. used StringIO()

Verknüpfte Beiträge Antworten Ansichten Aktivität
1
Nov. 18
6381
3
Juni 17
9496
4
Dez. 23
19033
2
Juli 25
4501
2
Dez. 24
7674