Skip to content

Instantly share code, notes, and snippets.

@eneldoserrata
Created July 18, 2017 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eneldoserrata/623801c8f7edc9f8780d635bddd563b9 to your computer and use it in GitHub Desktop.
Save eneldoserrata/623801c8f7edc9f8780d635bddd563b9 to your computer and use it in GitHub Desktop.
Odoo Save excel file on Binary and open to process
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'})
@bltempoconsulting
Copy link

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)

@jeffery9
Copy link

decode('base64') is works for python2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment