-
-
Save eneldoserrata/623801c8f7edc9f8780d635bddd563b9 to your computer and use it in GitHub Desktop.
Odoo Save excel file on Binary and open to process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpyxl | |
from openerp import models, fields, api, _ | |
from tempfile import TemporaryFile | |
class ExcelReader(models.TransientModel): | |
_name="reader_excel" | |
excel_file = fields.Binary(string='Excel File') | |
def import_excel(self): | |
# Generating of the excel file to be read by openpyxl | |
file = self.excel_file.decode('base64') | |
excel_fileobj = TemporaryFile('wb+') | |
excel_fileobj.write(file) | |
excel_fileobj.seek(0) | |
# Create workbook | |
workbook = openpyxl.load_workbook(excel_fileobj, data_only=True) | |
# Get the first sheet of excel file | |
sheet = workbook[workbook.get_sheet_names()[0]] | |
# Iteration on each rows in excel | |
for row in sheet.rows: | |
# Get value | |
v1 = row[0].value | |
v2 = row[1].value | |
v3 = row[2].value | |
# Create your record | |
self.env['your_model'].creaate({'val1':v1,'val2':v2, 'val3':v3'}) |
decode('base64')
is works for python2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi in Odoo 12 this solution doesn't works
This line :
file = self.excel_file.decode('base64')
Generate this error :
LookupError: 'base64' is not a text encoding; use codecs.decode() to handle arbitrary codecs
I propose this solution
At file top:
import base64
And replace line by :
file = base64.decodebytes(self.excel_file)