openerp 产品图片的批量写入

  • Write a short python script which loops over the image files, encode with base64 and write to OpenERP with XMLRPC

1 产品图片是需要转为 base64编码的

2:如果需要批量导入,可以吧图片名字和 产品编码关联,然后代码批量写入即可以

 

下面是一个写入产品图片的例子,最好由xmlrpc方法写入,直接写入数据库,不会触发中图和小图的计算

=================

  • #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import psycopg2
    import sys
    import base64
    import oerplib

    oerp = oerplib.OERP('localhost', protocol='xmlrpc', port=8069)
    oerp.login('user', 'passwprd', 'dbname')
    pp = oerp.get('product.product')

    picture_file='/tmp/yks_logo.png'
    product_id=113
    f = open(picture_file, 'rb')
    binary = f.read()
    image = base64.encodestring(binary)

    pp.write(product_id, {'image': image} )

 

==============

odoo14 的例子, 注意需要安装适合python3的oerplib   

pip install  OERPLib-py3
import oerplib  #需要安装  pip install  OERPLib-py3
import base64


oerp = oerplib.OERP('100.100.100', protocol='xmlrpc', port=8069)
#oerp = oerplib.OERP('usaves.net', protocol='xmlrpc+ssl', port=443) # https 方式
oerp.login('admin', 'admin', 'odoo') 
pp = oerp.get('product.product')
picture_file='12345.png'
product_id=20367
f = open(picture_file, 'rb')
binary_data = f.read()
image_str = base64.b64encode(binary_data).decode()
pp.write(product_id, {'image_variant': image_str}) print('===test end=')

#####################
def test_img_upload():
oerp = oerplib.OERP('18.234.149.31', protocol='xmlrpc', port=80)
oerp.login('admin', 'admin', 'odoo8065')

pp = oerp.get('product.product')
picture_file='12345.png'
product_id=20367

f = open(picture_file, 'rb')
binary_data = f.read()
image_str = base64.b64encode(binary_data).decode()
pp.write(product_id, {'image_variant': image_str})
print('===test end=')


test_img_upload()

  

 

posted on 2014-08-18 20:07  Odoo在中国  阅读(636)  评论(0编辑  收藏  举报

导航