This question has been flagged
2 Replies
13687 Views

I developed a custom import option for update the one2many table, for example I have a binary field for selecting xls or csv, then I have one button for import, this function will need to reterive the csv or xls data to create one2many records. In this code .csv is working fine but can't import .xls file. How to import .xls file.

    def records_import(self, cr, uid, ids, context=None):
supplier_obj = self.pool.get('res.partner')
for price in self.browse(cr, uid, ids, context=context):
fileName, fileExtension = os.path.splitext(price.file_to_import_fname)
if fileExtension != '.csv':
raise osv.except_osv(_("Warning !"), _("Change the file type as CSV"))
price_file = unicode(base64.decodestring(price.import_payment), 'windows-1252', 'strict').split('\n')
line_count = 0
ss = len(price_file)
for line in price_file:
line_count += 1
if line_count > 1:
if line_count < ss -1:
line_contents = line.split(',')
if not line_contents:
break
Name = line_contents[0].strip()
Amount = line_contents[1].strip()
supplier_id = supplier_obj.search(cr, uid, [('name', '=', Name)])
if supplier_id == []:
supplier = {
'name':Name
}
new_supplier = supplier_obj.create(cr, uid, supplier)
else:
for supp in supplier_obj.browse(cr, uid, supplier_id):
new_supplier = supp.id
price_line_exist = self.pool.get('sample.lines').search(cr, uid, [('name', '=', new_supplier),('payment_id', '=', price.id),('amount', '=', Amount)])
if price_line_exist == []:
price_upload_dict = {
'name' : new_supplier,
'amount': Amount
}
self.pool.get('sample.lines').create(cr, uid, price_upload_dict)
self.write(cr, uid, price.id,{'upload':True},context=context)
return {}
Avatar
Discard
Best Answer

You need to use a xls/xlsx library to allow you to read that files formats and better use xlrd that read both.

Install this package

https://pypi.python.org/pypi/xlrd


Here some examples:

https://github.com/python-excel/xlrd/tree/master/xlrd/examples

https://github.com/python-excel/tutorial/tree/master/students/xlrd

You need to know from where(rows and cols) you will read the data

Avatar
Discard
Best Answer

Hi Axel,

I am a beginner so can you help how I could install this library to our local v9 instance? Any howto link would do where I could learn installing libraries. 

Thanks in advance,

Peter

Avatar
Discard

Hi Péter Mikó

In python the most used install method of libraries is using pip that is used to install libraries from

https://pypi.python.org/pypi/

if the library does not install from pypi, then you need to check the library docs about how to install it. To install anything from pypi you could issue the command

pip install library_name

to install for example https://pypi.python.org/pypi/xlrd

pip install xlrd

there are a lot of resources out there of how to install libraries in python, also if you have an already running odoo v9 instance, you already had installed some libraries for that