从导入路径excel文件更新具有相同名称列表的产品价目表

时间:2014-09-23 03:54:14

标签: python excel string odoo

class data_migration(osv.osv):
      _name = 'excel.product.tmp'
      _description = 'Data Migration For Excel' 
      _columns = {
            'db_name':fields.char('DB Name'),
            'excel_file': fields.char('Excel File Path',size = 200),
            'sheet_name':fields.char('Excel Sheet Name'),
            }
def import_data(self,cr,uid,ids,context=None):

        for data2 in self.browse(cr, uid, ids, context=context):
        file_path = data2.excel_file
        sheet_name_1 = data2.sheet_name
        db_name_1 = data2.db_name
       # connection for DB
        conn_string = "dbname='"+ db_name_1+"' user='openerp' password='openpgpwd' host='localhost'"
        conn = psycopg2.connect(conn_string)
        cursor = conn.cursor()
        # read the excel file
        wb_c = openpyxl.reader.excel.load_workbook(file_path)
        s_c  = wb_c.get_sheet_by_name(name=sheet_name_1)
        d_c = s_c.calculate_dimension()
        r_c = s_c.range(d_c)
        for row in r_c:
            product_id_from_excel =row[0].value
            statement = "select id from product_template where LOWER(name)=LOWER('"+product_id_from_excel+"')"
            cursor.execute(statement)
            conn.commit()
            product_id = cursor.fetchone()
            if product_id == None:
                list_price_from_excel = row[1].value
                statement = "UPDATE product_template SET list_price='"+list_price_from_excel+"' WHERE id = '"+product_id+"'"
                cursor.execute(statement)
                conn.commit()  

data_migration()
从路径excel文件更新时,从控制台显示

错误         statement =“UPDATE product_template SET list_price ='”+ list_price_from_excel +“'WHERE id ='”+ product_id +“'”

  TypeError: coercing to Unicode: need string or buffer, NoneType found

1 个答案:

答案 0 :(得分:0)

你的循环条件是:

if product_id == None:

然后,您尝试在查询中插入product_id(其值为None)。由于None不是字符串而是NoneType - 无法将其转换为字符串,因此会出现此错误。

您还应该阅读db api faq以了解如何正确使用db api。