Search code examples
pythonodooxlsxodoo-10

odoo import .xlsx sale_order, purchase_order


what I'm trying to do here is importing an .xlsx to sale_order the code below explains how i did it using csv.DictReader(). in order to get that to work on .xlsx file I found XLS to Dict Reader using xlrd, and I have no idea how to implement that.

ps: I'm new with python and odoo 10

file_data = fields.Binary('Archive', required=True,)

def import_button(self):
    file_path = tempfile.gettempdir()+'/file.csv'
    data = self.file_data
    f = open(file_path,'wb')
    f.write(data.decode('base64'))
    f.close() 
    archive = csv.DictReader(open(file_path),delimiter=';')

    archive_lines = []
    for line in archive:
        archive_lines.append(line)

Edit: i did use that function XLSDictReader :

> archive = XLSDictReader(open(file_path))

and I got this error 'import.purchase.order' object has no attribute 'fileno'


Solution

  • always with xlrd, thank you Mr. @Datanovice for your help

    def import_button(self):
        file_path = tempfile.gettempdir()+'/file.xls'
        data = self.file_data
        f = open(file_path,'wb')
        f.write(data.decode('base64'))
        f.close() 
    
        workbook = xlrd.open_workbook(file_path)
        workbook = xlrd.open_workbook(file_path, on_demand = True)
        worksheet = workbook.sheet_by_index(0)
        first_row = [] # The row where we stock the name of the column
        for col in range(worksheet.ncols):
            first_row.append( worksheet.cell_value(0,col) )
        # transform the workbook to a list of dictionaries
        archive_lines = []
        for row in range(1, worksheet.nrows):
            elm = {}
            for col in range(worksheet.ncols):
                elm[first_row[col]]=worksheet.cell_value(row,col)
    
            archive_lines.append(elm)