使用python脚本将图像导入odoo 11时发生错误

时间:2018-07-31 12:45:34

标签: python-3.x odoo-11

我正在尝试使用python脚本将图像导入odoo 11。之前,我曾经在装有Windows 7的笔记本电脑上工作,此脚本工作正常。但是现在,我已将笔记本电脑升级到Windows 10,并尝试运行相同的脚本,但遇到的错误很少。

这是我的脚本

import csv
from pprint import pprint
import xmlrpc.client as xmlrpclib  



class OpenERPXMLRPC():
#def __init__(self, host="0.0.0.0", port="8088", db="demo",
#             user="admin",
#             password="admin"):
def __init__(self, host="205.147.98.219", port="", db="BUILDSTATION_ROMFORD",
             user="tina.santhosh@gmail.com",
             password="buildstation1234*"):
    common_url = "http://%s:%s/xmlrpc/common" % (host, port)
    object_url = "http://%s:%s/xmlrpc/object" % (host, port)
    com_sock = xmlrpclib.ServerProxy(common_url)
    uid = com_sock.login(db, user, password)
    if uid:
        self.uid = uid
        self.password = password
        self.db = db
    else:
        print("Error in Authentication")

    self.sock = xmlrpclib.ServerProxy(object_url)

def execute(self, model, method, *args):
    res = self.sock.execute(self.db, self.uid, self.password, model,
                            method, *args)
    return res

oe = OpenERPXMLRPC(db="BUILDSTATION_ROMFORD")
application = csv.reader(open('C:\\Users\\Asus\\Desktop\\test1.csv'))
for rec in application:
fields = rec
break
all_datas = []
count = 1

for rec in application:
all_datas.append(rec)

count = 0
all_error = []
for rec in all_datas:
count += 1
print(rec)

product_id = oe.execute(
       'product.template',
       'search',
       [('name','=', rec[0])])
print("product_name--", product_id)
with open(rec[1], 'rb') as image:
    image_base64 = image.read().encode("base64")

    vals = {
    'name': rec[0],
    'image_medium': image_base64
    }

    oe.execute(
       'product.template',
       'write',
       product_id,
       vals)

我创建了一个名为test1.csv的单独文件,其中仅上传了产品名称和图像位置。 这是我遇到的错误,

C:\Users\Asus>python c:\users\asus\desktop\final_import.py
['Airbrick Black', 'E:\\compressed images for odoo\\building         materials\\airblocks\\ti0014.jpg']
product_name-- [4071]
Traceback (most recent call last):
File "c:\users\asus\desktop\final_import.py", line 55, in <module>
image_base64 = image.read().encode("base64")
AttributeError: 'bytes' object has no attribute 'encode'

这里的任何帮助将不胜感激。

谢谢

蒂娜

0 个答案:

没有答案