如何将 PIL.Jpegimageplugin.Jpegimagefile 类型的图像存储到 Odoo 中

问题描述 投票:0回答:1

我正在从 Excel 中检索数据,其中一列是一堆图像,主要是 jpeg,我正在使用 openpyxl 和 openpyxl_image_loader 来读取 excel 并检索数据

这就是我读取数据的方式

def get_data_from_excel(file):
    data = defaultdict(list)  
    try:  
        wb = openpyxl.load_workbook(filename=BytesIO(base64.b64decode(file)))  
        ws = wb.active  
        image_loader = SheetImageLoader(ws)  
        # first row will be keys in dict  
        keys = [cell.value for cell in ws[1]]  
        i = 0
        for row in ws.iter_rows(min_row=2, max_col=ws.max_column, max_row=ws.max_row):  
            temp = {}  
            for key, cell in zip(keys, row):  
                if key == 'Photo':  
                    try:  
                        image = image_loader.get(cell.coordinate)  
                          
                        temp[key] = base64.b64decode(image.tobytes())
                        continue  
                    except Exception as e:  
                        print(e)  
                        temp[key] = False  
                        break           
                temp[key] = cell.value  
            data[i].append(temp)
            i+=1  
            
      
    except Exception as e:  
        raise ValidationError(_('Please upload a valid Excel file! ', e))  
    finally:  
        return data

通过id获取模型后尝试将数据设置到字段中

def set_photo(self, model, photo):  
    try:  
        model.image_1920 = photo   
    except Exception as e:  
        print(f'Error while setting photo for model {model}, error: {e}')

我得到的错误

该文件无法解码为图像文件。请尝试使用不同的文件。

我尝试使用 Pillow

open()
功能,但出现此异常

UnidentifiedImageError('无法识别图像文件<_io.BytesIO object at 0x7f3ddcb0b2c0>')

有关图像属性的更多详细信息

使用guess_mimetype

print(guess_mimetype(base64.b64decode(image.tobytes())))

输出

应用程序/八位字节流

我希望得到任何帮助或解释,提前致谢

python-3.x odoo openpyxl
1个回答
0
投票

我设法让它工作,我必须改变我将

JpegImageFile
转换为base64

的方式
image = image_loader.get(cell.coordinate)
buffer = BytesIO()
image.save(buffer, format=image.format)
image_bytes = buffer.getvalue()
image_base64 = base64.b64encode(image_bytes)

最终代码

def get_data_from_excel(file):
    data = defaultdict(list)  
    try:  
        wb = openpyxl.load_workbook(filename=BytesIO(base64.b64decode(file)))  
        ws = wb.active  
        image_loader = SheetImageLoader(ws)  
        # first row will be keys in dict  
        keys = [cell.value for cell in ws[1]]  
        i = 0
        for row in ws.iter_rows(min_row=2, max_col=ws.max_column, max_row=ws.max_row):  
            temp = {}  
            for key, cell in zip(keys, row):  
                if key == 'Photo':  
                    try:  
                        image = image_loader.get(cell.coordinate)  
                        buffer = BytesIO()  
                        image.save(buffer, format=image.format)  
                        image_bytes = buffer.getvalue()  
                        image_base64 = base64.b64encode(image_bytes)  
                          
                        temp[key] = image_base64
                        continue  
                    except Exception as e:  
                        print(e)  
                        temp[key] = False  
                        break           
                temp[key] = cell.value  
            data[i].append(temp)
            i+=1  
            
      
    except Exception as e:  
        raise ValidationError(_('Please upload a valid Excel file! ', e))  
    finally:  
        return data

不确定这是否会对任何人有帮助,但我将我的答案留在这里以防万一

© www.soinside.com 2019 - 2024. All rights reserved.