The code below is a function which is execute when i press import products button in Odoo. This script imports products in Odoo from a csv file. The csv file can be uploaded first and then i click the import button. What i can't do is that when i upload xlsx file and click import i get warning that the file is not valid. Can someone help me how to enable to import also from xlsx file?
@api.one
def action_import(self):
ctx = self._context
data = base64.b64decode(self.data)
file_input = cStringIO.StringIO(data)
file_input.seek(0)
row = []
result = {}
if self.delimeter:
delimeter = str(self.delimeter)
else:
delimeter = ','
fieldnames = ['supplier_default_code', 'name', 'product_brand', 'supplier', 'standard_price', 'list_price',
'internal_category', 'spare_part', 'service_part', 'default_code', 'supplierinfo_product_name',
'rrp_price', 'min_qty', 'weight', 'net_weight', 'volume', 'height', 'width', 'depth', 'delay',
'warranty_type', 'warranty', 'sale_delay', 'description', 'description_sale',
'description_delivery', 'ean']
reader = csv.DictReader(file_input, delimiter=delimeter, fieldnames=fieldnames)
try:
row.extend(reader)
except Exception:
raise exceptions.Warning(_("Not a valid file!"))
keys = row[0]
self.create_products_from_array(row)
From your description it looks like you're expecting csv.DictReader
to cope with an .xlsx
file. But that is something completely different from a .csv
file. The fact that Excel can open them both doesn't mean they are interchangeable, or somehow the same.
You could try using the module xlrd
. But it will be a completely different implementation; the module won't provide a drop-in replacement for csv.DictReader
.
The only other alternative is to preprocess the .xlsx
file and transform it into the .csv
that your code expects. You could use xlrd
for this, or you could write a VBA macro in Excel to do it; in fact there may be a dozen ways to do it.